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/(web) Set the alias of zh-CN to zh-Hans #13412

Closed
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
6 changes: 6 additions & 0 deletions web/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ export const langs = [
{ name: 'Development (keys only)', code: 'dev', loader: () => Promise.resolve({}) },
];

// browser is zh-CN, but weblate use zh-Hans
export const langAliasesMap: { [key: string]: string } = {
'zh-CN' : 'zh-Hans',
'zh-TW' : 'zh-Hant'
};

export enum ImmichProduct {
Client = 'immich-client',
Server = 'immich-server',
Expand Down
8 changes: 7 additions & 1 deletion web/src/lib/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('i18n', () => {
});

describe('getClosestAvailableLocale', () => {
const allLocales = ['ar', 'bg', 'en', 'en-US', 'en-DE', 'zh-Hans', 'zh-Hans-HK'];
const allLocales = ['ar', 'bg', 'en', 'en-US', 'en-DE', 'zh-Hans', 'zh-Hans-HK', 'zh-HK', 'zh-Hant'];

it('returns undefined on mismatch', () => {
expect(getClosestAvailableLocale([], allLocales)).toBeUndefined();
Expand All @@ -47,5 +47,11 @@ describe('i18n', () => {
expect(getClosestAvailableLocale(['zh'], allLocales)).toBeUndefined();
expect(getClosestAvailableLocale(['de', 'zh', 'en-US'], allLocales)).toBe('en-US');
});

it('returns the locale for a alias match', () => {
expect(getClosestAvailableLocale(['zh-CN'], allLocales)).toBe('zh-Hans');
expect(getClosestAvailableLocale(['zh-TW'], allLocales)).toBe('zh-Hant');
});

});
});
5 changes: 3 additions & 2 deletions web/src/lib/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { langs } from '$lib/constants';
import { langAliasesMap, langs } from '$lib/constants';
import { locale, t, waitLocale } from 'svelte-i18n';
import { get, type Unsubscriber } from 'svelte/store';

Expand All @@ -23,7 +23,8 @@ function getSubLocales(refLocale: string) {

export function getClosestAvailableLocale(locales: readonly string[], allLocales: readonly string[]) {
const allLocalesSet = new Set(allLocales);
return locales.find((locale) => getSubLocales(locale).some((subLocale) => allLocalesSet.has(subLocale)));
const expandedLocales = locales.flatMap((locale) => [locale, langAliasesMap[locale] ?? null]).filter(Boolean);
return expandedLocales.find((locale) => getSubLocales(locale).some((subLocale) => allLocalesSet.has(subLocale)));
}

export const langCodes = langs.map((lang) => lang.code);
Expand Down
Loading