Skip to content

Commit

Permalink
fix: fix en locale always loaded instead of user defined custom locale
Browse files Browse the repository at this point in the history
fix #315
  • Loading branch information
wa0x6e committed Feb 18, 2023
1 parent 5bccc66 commit 7c8189a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/helpers/DateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class DateHelper {
if (typeof userLocale === 'string' && userLocale !== DEFAULT_LOCALE) {
const locale =
(window as any)[`dayjs_locale_${userLocale}`] ||
(await this.loadLocale());
(await this.loadLocale(userLocale));
dayjs.locale(userLocale);
this.locale = locale;
}
Expand Down Expand Up @@ -158,17 +158,18 @@ export default class DateHelper {
}

// this function will work cross-browser for loading scripts asynchronously
loadLocale(): Promise<any> {
// eslint-disable-next-line class-methods-use-this
loadLocale(userLocale: string): Promise<any> {
return new Promise((resolve, reject) => {
const s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = `https://cdn.jsdelivr.net/npm/dayjs@1/locale/${this.locale}.js`;
s.src = `https://cdn.jsdelivr.net/npm/dayjs@1/locale/${userLocale}.js`;
s.onerror = (err) => {
reject(err);
};
s.onload = () => {
resolve((window as any)[`dayjs_locale_${this.locale}`]);
resolve((window as any)[`dayjs_locale_${userLocale}`]);
};
document.head.appendChild(s);
});
Expand Down

0 comments on commit 7c8189a

Please sign in to comment.