Fix Accept-Language detection (#6144)

fixes #6129

Bug: #6129
Bug: #6144
Follows-Up: #3882
Change-Id: Ie3fcec84948fde36fa12448d5eabaf05f79f1283
This commit is contained in:
Winston Sung 2024-02-10 18:27:02 +08:00 committed by GitHub
parent 9560f71f2c
commit e718acaaf8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -181,42 +181,89 @@ window.html10n = (function(window, document, undefined) {
return return
} }
// Issue #6129: Fix exceptions caused by browsers
// Also for fallback, see BCP 47 RFC 4647 section 3.4
// NOTE: this output the all lowercase form
function getBcp47LangCode(browserLang) {
var bcp47Lang = browserLang.toLowerCase();
// Browser => BCP 47
var langCodeMap = {
'zh-cn': 'zh-hans-cn',
'zh-hk': 'zh-hant-hk',
'zh-mo': 'zh-hant-mo',
'zh-my': 'zh-hans-my',
'zh-sg': 'zh-hans-sg',
'zh-tw': 'zh-hant-tw',
};
return langCodeMap[bcp47Lang] ?? bcp47Lang;
}
// Issue #6129: Fix exceptions
// NOTE: translatewiki.net use all lowercase form by default ('en-gb' insted of 'en-GB')
function getJsonLangCode(bcp47Lang) {
var jsonLang = bcp47Lang.toLowerCase();
// BCP 47 => JSON
var langCodeMap = {
'sr-cyrl': 'sr-ec',
'sr-latn': 'sr-el',
'zh-hant-hk': 'zh-hk',
};
return langCodeMap[jsonLang] ?? jsonLang;
}
var bcp47LangCode = getBcp47LangTag(lang);
var jsonLangCode = getJsonLangCode(bcp47LangCode);
// Check if lang exists // Check if lang exists
if (!data[lang]) { if (!data[jsonLangCode]) {
// lang not found // lang not found
// This may be due to formatting (expected 'ru' but browser sent 'ru-RU') // This may be due to formatting (expected 'ru' but browser sent 'ru-RU')
// Set err msg before mutating lang (we may need this later) // Set err msg before mutating lang (we may need this later)
var msg = 'Couldn\'t find translations for ' + lang; var msg = 'Couldn\'t find translations for ' + lang +
'(lowercase BCP 47 lang tag ' + bcp47LangCode +
', JSON lang code ' + jsonLangCode + ')';
// Check for '-' ('ROOT-VARIANT') // Check for '-' (BCP 47 'ROOT-SCRIPT-REGION-VARIANT') and fallback until found data or ROOT
if (lang.indexOf('-') > -1) { // - 'ROOT-SCRIPT-REGION': 'zh-Hans-CN'
// ROOT-VARIANT formatting detected // - 'ROOT-SCRIPT': 'zh-Hans'
lang = lang.split('-')[0]; // set lang to ROOT lang // - 'ROOT-REGION': 'en-GB'
// - 'ROOT-VARIANT': 'be-tarask'
while (!data[jsonLangCode] && bcp47LangCode.lastIndexOf('-') > -1) {
// ROOT-SCRIPT-REGION-VARIANT formatting detected
bcp47LangCode = bcp47LangCode.substring(0, bcp47LangCode.lastIndexOf('-')); // set lang to ROOT lang
jsonLangCode = getJsonLangCode(bcp47LangCode);
} }
// Check if ROOT lang exists (e.g 'ru') // Check if already found data or ROOT lang exists (e.g 'ru')
if (!data[lang]) { if (!data[jsonLangCode]) {
// ROOT lang not found. (e.g 'zh') // ROOT lang not found. (e.g 'zh')
// Loop through langs data. Maybe we have a variant? e.g (zh-hans) // Loop through langs data. Maybe we have a variant? e.g (zh-hans)
var l; // langs item. Declare outside of loop var l; // langs item. Declare outside of loop
for (l in data) { for (l in data) {
// Is not ROOT? // Is not ROOT?
// And index of ROOT equals 0? // And is variant of ROOT?
// (NOTE: index of ROOT equals 0 would cause unexpected ISO 639-1 vs. 639-3 issues,
// so append dash into query string)
// And is known lang? // And is known lang?
if (lang != l && l.indexOf(lang) === 0 && data[l]) { if (bcp47LangCode != l && l.indexOf(lang + '-') === 0 && data[l]) {
lang = l; // set lang to ROOT-VARIANT (e.g 'zh-hans') bcp47LangCode = l; // set lang to ROOT-SCRIPT (e.g 'zh-hans')
jsonLangCode = getJsonLangCode(bcp47LangCode);
break; break;
} }
} }
// Did we find a variant? If not, return err. // Did we find a variant? If not, return err.
if (lang != l) { if (bcp47LangCode != l) {
return cb(new Error(msg)); return cb(new Error(msg));
} }
} }
} }
lang = jsonLangCode;
if ('string' == typeof data[lang]) { if ('string' == typeof data[lang]) {
// Import rule // Import rule