forked from ableplayer/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation.js
More file actions
130 lines (117 loc) · 4.19 KB
/
translation.js
File metadata and controls
130 lines (117 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(function ($) {
AblePlayer.prototype.getSupportedLangs = function() {
// returns an array of languages for which AblePlayer has translation tables
var langs = ['ca','cs','da','de','en','es','fr','he','id','it','ja','ms','nb','nl','pl','pt','pt-br','sv','tr','zh-tw'];
return langs;
};
AblePlayer.prototype.getTranslationText = function() {
// determine language, then get labels and prompts from corresponding translation var
var deferred, thisObj, supportedLangs, docLang, translationFile, i, similarLangFound;
deferred = $.Deferred();
thisObj = this;
supportedLangs = this.getSupportedLangs(); // returns an array
if (this.lang) { // a data-lang attribute is included on the media element
if ($.inArray(this.lang,supportedLangs) === -1) {
// the specified language is not supported
if (this.lang.indexOf('-') == 2) {
// this is a localized lang attribute (e.g., fr-CA)
// try the parent language, given the first two characters
// if parent lang is supported. Use that, else null.
this.lang = ($.inArray(this.lang.substring(0,2),supportedLangs) !== -1) ? this.lang.substring(0,2) : null;
} else {
// this is not a localized language.
// but maybe there's a similar localized language supported
// that has the same parent?
similarLangFound = false;
i = 0;
while (i < supportedLangs.length) {
if (supportedLangs[i].substring(0,2) == this.lang) {
this.lang = supportedLangs[i];
similarLangFound = true;
}
i++;
}
if (!similarLangFound) {
// language requested via data-lang is not supported
this.lang = null;
}
}
}
}
if (!this.lang) {
// try the language of the web page, if specified
if ($('body').attr('lang')) {
docLang = $('body').attr('lang').toLowerCase();
} else if ($('html').attr('lang')) {
docLang = $('html').attr('lang').toLowerCase();
} else {
docLang = null;
}
if (docLang) {
if ($.inArray(docLang,supportedLangs) !== -1) {
// the document language is supported
this.lang = docLang;
} else {
// the document language is not supported
if (docLang.indexOf('-') == 2) {
// this is a localized lang attribute (e.g., fr-CA)
// try the parent language, given the first two characters
if ($.inArray(docLang.substring(0,2),supportedLangs) !== -1) {
// the parent language is supported. use that.
this.lang = docLang.substring(0,2);
}
}
}
}
}
if (!this.lang) {
// No supported language has been specified by any means
// Fallback to English
this.lang = 'en';
}
if (!this.searchLang) {
this.searchLang = this.lang;
}
translationFile = this.rootPath + 'translations/' + this.lang + '.json';
$.getJSON(translationFile, function(data) {
// success!
thisObj.tt = data;
deferred.resolve();
})
.fail(function() {
console.log( "Error: Translation files need to be updated to JSON.",translationFile);
translationFile = thisObj.rootPath + 'translations/' + thisObj.lang + '.js';
$.getJSON(translationFile, function(data) {
// success!
thisObj.tt = data;
deferred.resolve();
})
.fail( function() {
console.log( "Critical Error: Unable to load translation file:",translationFile);
thisObj.provideFallback();
deferred.fail();
});
})
return deferred.promise();
};
AblePlayer.prototype.getSampleDescriptionText = function() {
// Create an array of sample description text in all languages
// This needs to be readily available for testing different voices
// in the Description Preferences dialog
var thisObj, supportedLangs, i, thisLang, translationFile, thisText, translation;
supportedLangs = this.getSupportedLangs();
thisObj = this;
this.sampleText = [];
for (i=0; i < supportedLangs.length; i++) {
translationFile = this.rootPath + 'translations/' + supportedLangs[i] + '.json';
$.getJSON(translationFile, thisLang, (function(thisLang) {
return function(data) {
thisText = data.sampleDescriptionText;
translation = {'lang':thisLang, 'text': thisText};
thisObj.sampleText.push(translation);
};
}(supportedLangs[i])) // pass lang to callback function
);
}
};
})(jQuery);