Skip to content

MOBILE-4653 lang: Load dayjs locale #4403

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 src/core/features/settings/pages/general/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default class CoreSettingsGeneralPage {
try {
await CoreLang.changeCurrentLanguage(this.selectedLanguage);
} finally {
const langName = this.languages.find((lang) => lang.code == this.selectedLanguage)?.name;
const langName = this.languages.find((lang) => lang.code === this.selectedLanguage)?.name;

const buttons: AlertButton[] = [
{
Expand Down
32 changes: 30 additions & 2 deletions src/core/services/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ export class CoreLangProvider {
* @returns Promise resolved when the change is finished.
*/
async changeCurrentLanguage(language: string): Promise<void> {
// Use british english when parent english is loaded.
dayjs.locale(language === 'en' ? 'en-gb' : language);
await this.loadDayJSLocale(language);

const previousLanguage = this.currentLanguage ?? this.getDefaultLanguage();

Expand Down Expand Up @@ -218,6 +217,35 @@ export class CoreLangProvider {
}
}

/**
* Load the locale for DayJS.
*
* @param locale Locale to load.
*/
protected async loadDayJSLocale(locale: string): Promise<void> {
// Use british english when parent english is loaded.
locale = locale === 'en' ? 'en-gb' : locale;

try {
await import('dayjs/locale/' + locale);
dayjs.locale(locale);
} catch {
if (locale === 'en' || locale === 'en-gb') {
return;
}
const parentLang = await this.getParentLanguageForLang(locale);
const parentLangUsingHyphen = locale.substring(0, locale.indexOf('-'));

if (parentLangUsingHyphen && parentLang === 'en' || parentLang === undefined) {
await this.loadDayJSLocale(parentLangUsingHyphen);

return;
}

await this.loadDayJSLocale(parentLang ?? 'en');
}
}

/**
* Clear current custom strings.
*/
Expand Down
Loading