html10n: do a lax match between the Accept-Language header and available locales
Before this change, we simply generated an error. For example: - if the browser sent 'ru-RU', but Etherpad has 'ru' available, select 'ru'; - if the browser sent 'zh', but we have 'zh-hans' available, use 'zh-hans'. Fixes #3882.pull/3941/head
parent
e8cd83286d
commit
a471dbeebf
|
@ -179,18 +179,40 @@ window.html10n = (function(window, document, undefined) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// dat alng ain't here, man!
|
// Check if lang exists
|
||||||
if (!data[lang]) {
|
if (!data[lang]) {
|
||||||
var msg = 'Couldn\'t find translations for '+lang
|
// lang not found
|
||||||
, l
|
// This may be due to formatting (expected 'ru' but browser sent 'ru-RU')
|
||||||
if(~lang.indexOf('-')) lang = lang.split('-')[0] // then let's try related langs
|
// Set err msg before mutating lang (we may need this later)
|
||||||
for(l in data) {
|
var msg = 'Couldn\'t find translations for ' + lang;
|
||||||
if(lang != l && l.indexOf(lang) === 0 && data[l]) {
|
|
||||||
lang = l
|
// Check for '-' ('ROOT-VARIANT')
|
||||||
|
if (lang.indexOf('-') > -1) {
|
||||||
|
// ROOT-VARIANT formatting detected
|
||||||
|
lang = lang.split('-')[0]; // set lang to ROOT lang
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if ROOT lang exists (e.g 'ru')
|
||||||
|
if (!data[lang]) {
|
||||||
|
// ROOT lang not found. (e.g 'zh')
|
||||||
|
// Loop through langs data. Maybe we have a variant? e.g (zh-hans)
|
||||||
|
var l; // langs item. Declare outside of loop
|
||||||
|
|
||||||
|
for (l in data) {
|
||||||
|
// Is not ROOT?
|
||||||
|
// And index of ROOT equals 0?
|
||||||
|
// And is known lang?
|
||||||
|
if (lang != l && l.indexOf(lang) === 0 && data[l]) {
|
||||||
|
lang = l; // set lang to ROOT-VARIANT (e.g 'zh-hans')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(lang != l) return cb(new Error(msg))
|
|
||||||
|
// Did we find a variant? If not, return err.
|
||||||
|
if (lang != l) {
|
||||||
|
return cb(new Error(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('string' == typeof data[lang]) {
|
if ('string' == typeof data[lang]) {
|
||||||
|
|
Loading…
Reference in New Issue