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
Luke Williams 2020-04-19 21:06:20 +02:00 committed by muxator
parent e8cd83286d
commit a471dbeebf
1 changed files with 31 additions and 9 deletions

View File

@ -179,18 +179,40 @@ window.html10n = (function(window, document, undefined) {
return
}
// dat alng ain't here, man!
// Check if lang exists
if (!data[lang]) {
var msg = 'Couldn\'t find translations for '+lang
, l
if(~lang.indexOf('-')) lang = lang.split('-')[0] // then let's try related langs
// lang not found
// This may be due to formatting (expected 'ru' but browser sent 'ru-RU')
// Set err msg before mutating lang (we may need this later)
var msg = 'Couldn\'t find translations for ' + lang;
// 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
lang = l; // set lang to ROOT-VARIANT (e.g 'zh-hans')
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]) {