diff --git a/__test__/locale.spec.ts b/__test__/locale.spec.ts index 1ea0a112..b3387181 100644 --- a/__test__/locale.spec.ts +++ b/__test__/locale.spec.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest' import { resolve } from 'node:path' -import { readdirSync } from 'node:fs' +import { readdirSync, readFileSync } from 'node:fs' import en from '../locales/en-US.json' function getKeys(obj: any, path = '', result: string[] = []) { @@ -22,7 +22,7 @@ const defaultKeys = getKeys(en); describe("locale files should include all keys", () => { localesOtherThanEnglish.forEach((locale) => { it(`for ${locale}`, () => { - expect(getKeys(require(`../locales/${locale}`))).toEqual(defaultKeys) + expect(getKeys(JSON.parse(readFileSync(resolve(__dirname, "../locales", locale)) as unknown as string))).toEqual(defaultKeys) }) }) }) \ No newline at end of file diff --git a/locales/zh-CN.json b/locales/zh-Hans.json similarity index 100% rename from locales/zh-CN.json rename to locales/zh-Hans.json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5872afb5..83e08f8a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -58,7 +58,7 @@ importers: version: 2.4.2 vitest: specifier: ^1.0.1 - version: 1.0.1(@types/node@18.19.2)(jsdom@23.0.1) + version: 1.0.1(@types/node@18.19.2) zx: specifier: ^7.2.3 version: 7.2.3 @@ -2548,6 +2548,18 @@ packages: ms: 2.1.2 dev: true + /debug@4.3.4: + resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} + engines: {node: '>=6.0'} + peerDependencies: + supports-color: '*' + peerDependenciesMeta: + supports-color: + optional: true + dependencies: + ms: 2.1.2 + dev: true + /debug@4.3.4(supports-color@8.1.1): resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} engines: {node: '>=6.0'} @@ -4275,7 +4287,7 @@ packages: dependencies: chalk: 5.3.0 commander: 11.1.0 - debug: 4.3.4(supports-color@8.1.1) + debug: 4.3.4 execa: 8.0.1 lilconfig: 3.0.0 listr2: 8.0.0 @@ -6048,7 +6060,7 @@ packages: hasBin: true dependencies: cac: 6.7.14 - debug: 4.3.4(supports-color@8.1.1) + debug: 4.3.4 pathe: 1.1.1 picocolors: 1.0.0 vite: 5.0.5(@types/node@18.19.2) @@ -6153,6 +6165,63 @@ packages: fsevents: 2.3.3 dev: true + /vitest@1.0.1(@types/node@18.19.2): + resolution: {integrity: sha512-MHsOj079S28hDsvdDvyD1pRj4dcS51EC5Vbe0xvOYX+WryP8soiK2dm8oULi+oA/8Xa/h6GoJEMTmcmBy5YM+Q==} + engines: {node: ^18.0.0 || >=20.0.0} + hasBin: true + peerDependencies: + '@edge-runtime/vm': '*' + '@types/node': ^18.0.0 || >=20.0.0 + '@vitest/browser': ^1.0.0 + '@vitest/ui': ^1.0.0 + happy-dom: '*' + jsdom: '*' + peerDependenciesMeta: + '@edge-runtime/vm': + optional: true + '@types/node': + optional: true + '@vitest/browser': + optional: true + '@vitest/ui': + optional: true + happy-dom: + optional: true + jsdom: + optional: true + dependencies: + '@types/node': 18.19.2 + '@vitest/expect': 1.0.1 + '@vitest/runner': 1.0.1 + '@vitest/snapshot': 1.0.1 + '@vitest/spy': 1.0.1 + '@vitest/utils': 1.0.1 + acorn-walk: 8.3.0 + cac: 6.7.14 + chai: 4.3.10 + debug: 4.3.4 + execa: 8.0.1 + local-pkg: 0.5.0 + magic-string: 0.30.5 + pathe: 1.1.1 + picocolors: 1.0.0 + std-env: 3.6.0 + strip-literal: 1.3.0 + tinybench: 2.5.1 + tinypool: 0.8.1 + vite: 5.0.5(@types/node@18.19.2) + vite-node: 1.0.1(@types/node@18.19.2) + why-is-node-running: 2.2.2 + transitivePeerDependencies: + - less + - lightningcss + - sass + - stylus + - sugarss + - supports-color + - terser + dev: true + /vitest@1.0.1(@types/node@18.19.2)(jsdom@23.0.1): resolution: {integrity: sha512-MHsOj079S28hDsvdDvyD1pRj4dcS51EC5Vbe0xvOYX+WryP8soiK2dm8oULi+oA/8Xa/h6GoJEMTmcmBy5YM+Q==} engines: {node: ^18.0.0 || >=20.0.0} diff --git a/utils/getLanguage.ts b/utils/getLanguage.ts index 4f7ec42a..15cda588 100644 --- a/utils/getLanguage.ts +++ b/utils/getLanguage.ts @@ -43,6 +43,20 @@ interface Language { } } +function linkLocale(locale: string): string { + let linkedLocale: string + switch (locale) { + case 'zh-CN': + case 'zh-SG': + linkedLocale = 'zh-Hans' + break + default: + linkedLocale = locale + } + + return linkedLocale +} + function getLocale() { const shellLocale = process.env.LC_ALL || // POSIX locale environment variables @@ -51,9 +65,7 @@ function getLocale() { Intl.DateTimeFormat().resolvedOptions().locale || // Built-in ECMA-402 support 'en-US' // Default fallback - const locale = shellLocale.split('.')[0].replace('_', '-') - - return locale + return linkLocale(shellLocale.split('.')[0].replace('_', '-')) } export default function getLanguage() { @@ -66,9 +78,11 @@ export default function getLanguage() { const languageFilePath = path.resolve(localesRoot, `${locale}.json`) const doesLanguageExist = fs.existsSync(languageFilePath) - const lang: Language = doesLanguageExist - ? require(languageFilePath) - : require(path.resolve(localesRoot, 'en-US.json')) + const lang = ( + doesLanguageExist + ? JSON.parse(fs.readFileSync(languageFilePath) as unknown as string) + : JSON.parse(fs.readFileSync(path.resolve(localesRoot, 'en-US.json')) as unknown as string) + ) as Language return lang }