-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX Set current locale based on actual defined dictionaries #1523
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,9 @@ | |
class i18n { | ||
constructor() { | ||
this.defaultLocale = 'en_US'; | ||
this.currentLocale = this.detectLocale(); | ||
// Give a null current locale initially to avoid "locale is undefined" errors | ||
this.currentLocale = null; | ||
this.autoDetectLocale = true; | ||
this.lang = {}; | ||
} | ||
|
||
|
@@ -28,6 +30,7 @@ class i18n { | |
*/ | ||
setLocale(locale) { | ||
this.currentLocale = locale; | ||
this.autoDetectLocale = false; | ||
} | ||
|
||
/** | ||
|
@@ -90,6 +93,11 @@ class i18n { | |
for (let entity in dict) { | ||
this.lang[locale][entity] = dict[entity]; | ||
} | ||
|
||
// Re-set current locale in case the new dictionary provides a better match than the old locale. | ||
if (this.autoDetectLocale) { | ||
this.currentLocale = this.detectLocale(); | ||
} | ||
Comment on lines
+97
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dictionaries are added by separate We have no way of knowing ahead of time if more dictionaries will be loaded, so we can't reasonably defer until all of them are loaded. |
||
} | ||
|
||
/** | ||
|
@@ -174,10 +182,9 @@ class i18n { | |
* Detect document language settings by looking at <meta> tags. | ||
* If no match is found, returns this.defaultLocale. | ||
* | ||
* @todo get by <html lang=''> - needs modification of SSViewer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's already doing this as the first step |
||
* | ||
* @return {String} - Locale in mixed lowercase/uppercase format suitable | ||
* for usage in i18n.lang arrays (e.g. 'en_US'). | ||
* for usage in i18n.lang arrays (e.g. 'en_US') or in 2-character lowercase | ||
* format (e.g. 'en') if no mixed format is available. | ||
*/ | ||
detectLocale() { | ||
// Get by <html> tag | ||
|
@@ -207,14 +214,13 @@ class i18n { | |
|
||
// Get locale (e.g. 'en_US') from common name (e.g. 'en') | ||
// by looking at i18n.lang tables | ||
let detectedLocale = null; | ||
if (rawLocale.length === 2) { | ||
for (let compareLocale in i18n.lang) { | ||
for (let compareLocale in this.lang) { | ||
Comment on lines
-212
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dictionaries are added to this instance of |
||
if (compareLocale.substr(0, 2).toLowerCase() === rawLocale.toLowerCase()) { | ||
return compareLocale; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Parse raw locale | ||
const rawLocaleParts = rawLocale.match(/([^-|_]*)[-|_](.*)/); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone explicitly sets a locale, we don't want to override their decision whenever a new dictionary is added.