Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/js/i18n.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions client/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}

Expand All @@ -28,6 +30,7 @@ class i18n {
*/
setLocale(locale) {
this.currentLocale = locale;
this.autoDetectLocale = false;
Copy link
Member Author

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.

}

/**
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dictionaries are added by separate .js files. This is the most reliable way to ensure that the current locale is detected according to all dictionaries that will be loaded in, allowing for slow network speeds. i.e. at the time each dictionary is loaded in, the most-correct current locale will be set.

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.

}

/**
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dictionaries are added to this instance of i18n, not as a static property on the class.

if (compareLocale.substr(0, 2).toLowerCase() === rawLocale.toLowerCase()) {
return compareLocale;
}
}
}
}

// Parse raw locale
const rawLocaleParts = rawLocale.match(/([^-|_]*)[-|_](.*)/);
Expand Down