Skip to content

Commit 4d5dd30

Browse files
fix: Set moment locale according to chosen language (#5414)
1 parent 07a0cc8 commit 4d5dd30

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

app/services/l10n.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import classic from 'ember-classic-decorator';
22
import { inject as service } from '@ember/service';
33
import computed from 'ember-computed';
44
import L10n from 'ember-l10n/services/l10n';
5+
import moment from 'moment';
6+
import { getScript } from 'open-event-frontend/utils/loader';
57

68
@classic
79
export default class L10nService extends L10n {
@@ -38,7 +40,7 @@ export default class L10nService extends L10n {
3840

3941
switchLanguage(locale) {
4042
this.setLocale(locale);
41-
this.cookies.write(this.localStorageKey, locale);
43+
this.cookies.write(this.localStorageKey, locale, { path: '/' });
4244
if (!this.fastboot.isFastBoot) {
4345
location.reload();
4446
}
@@ -48,12 +50,20 @@ export default class L10nService extends L10n {
4850
super.init(...arguments);
4951
const currentLocale = this.cookies.read(this.localStorageKey);
5052
const detectedLocale = this.detectLocale();
53+
54+
let locale = 'en';
5155
if (currentLocale) {
52-
this.setLocale(currentLocale);
56+
locale = currentLocale;
5357
} else if (detectedLocale) {
54-
this.setLocale(detectedLocale);
55-
} else {
56-
this.setLocale('en');
58+
locale = detectedLocale;
59+
}
60+
61+
this.setLocale(locale);
62+
if (locale !== 'en') {
63+
getScript(`/assets/moment-locales/${currentLocale}.js`)
64+
.then(() => {
65+
moment.locale(currentLocale);
66+
});
5767
}
5868
}
5969
}

app/utils/loader.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const getScript = (url: string): Promise<void> => new Promise((resolve, reject) => {
2+
const script = document.createElement('script');
3+
script.src = url;
4+
script.async = true;
5+
6+
script.onerror = reject;
7+
8+
script.onload = (script as any).onreadystatechange = function() {
9+
const loadState = this.readyState;
10+
11+
if (loadState && loadState !== 'loaded' && loadState !== 'complete') {return}
12+
13+
script.onload = (script as any).onreadystatechange = null;
14+
15+
resolve();
16+
};
17+
18+
document.head.appendChild(script);
19+
});

config/environment.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ module.exports = function(environment) {
3434
},
3535

3636
moment: {
37-
includeTimezone: 'subset'
38-
/* ,
39-
localeOutputPath : 'assets/moment-locales'*/
37+
includeTimezone : 'subset',
38+
localeOutputPath : 'assets/moment-locales'
4039
},
4140

4241
pace: {

ember-cli-build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = function(defaults) {
4545
fingerprint: {
4646
enabled : env === 'production',
4747
generateAssetMap : true,
48-
exclude : ['package.json'],
48+
exclude : ['package.json', 'assets/moment-locales'],
4949
extensions : ['js', 'css', 'png', 'jpg', 'gif', 'map', 'svg', 'json']
5050
},
5151
sourcemaps: {

0 commit comments

Comments
 (0)