diff --git a/.i18nrc.js b/.i18nrc.js index e4216b4d045b..8d70ad011acc 100644 --- a/.i18nrc.js +++ b/.i18nrc.js @@ -1,20 +1,20 @@ const { defineConfig } = require('@lobehub/i18n-cli'); module.exports = defineConfig({ - entry: 'locales/zh_CN', - entryLocale: 'zh_CN', + entry: 'locales/zh-CN', + entryLocale: 'zh-CN', output: 'locales', outputLocales: [ - 'zh_TW', - 'en_US', - 'ru_RU', - 'ja_JP', - 'ko_KR', - 'fr_FR', - 'tr_TR', - 'es_ES', - 'pt_BR', - 'de_DE', + 'zh-TW', + 'en-US', + 'ru-RU', + 'ja-JP', + 'ko-KR', + 'fr-FR', + 'tr-TR', + 'es-ES', + 'pt-BR', + 'de-DE', ], temperature: 0, modelName: 'gpt-3.5-turbo-1106', diff --git a/locales/de_DE/chat.json b/locales/de-DE/chat.json similarity index 100% rename from locales/de_DE/chat.json rename to locales/de-DE/chat.json diff --git a/locales/de_DE/common.json b/locales/de-DE/common.json similarity index 100% rename from locales/de_DE/common.json rename to locales/de-DE/common.json diff --git a/locales/de_DE/empty.json b/locales/de-DE/empty.json similarity index 100% rename from locales/de_DE/empty.json rename to locales/de-DE/empty.json diff --git a/locales/de_DE/error.json b/locales/de-DE/error.json similarity index 100% rename from locales/de_DE/error.json rename to locales/de-DE/error.json diff --git a/locales/de_DE/market.json b/locales/de-DE/market.json similarity index 100% rename from locales/de_DE/market.json rename to locales/de-DE/market.json diff --git a/locales/de_DE/migration.json b/locales/de-DE/migration.json similarity index 100% rename from locales/de_DE/migration.json rename to locales/de-DE/migration.json diff --git a/locales/de_DE/plugin.json b/locales/de-DE/plugin.json similarity index 100% rename from locales/de_DE/plugin.json rename to locales/de-DE/plugin.json diff --git a/locales/de_DE/setting.json b/locales/de-DE/setting.json similarity index 100% rename from locales/de_DE/setting.json rename to locales/de-DE/setting.json diff --git a/locales/de_DE/welcome.json b/locales/de-DE/welcome.json similarity index 100% rename from locales/de_DE/welcome.json rename to locales/de-DE/welcome.json diff --git a/locales/en_US/chat.json b/locales/en-US/chat.json similarity index 100% rename from locales/en_US/chat.json rename to locales/en-US/chat.json diff --git a/locales/en_US/common.json b/locales/en-US/common.json similarity index 100% rename from locales/en_US/common.json rename to locales/en-US/common.json diff --git a/locales/en_US/empty.json b/locales/en-US/empty.json similarity index 100% rename from locales/en_US/empty.json rename to locales/en-US/empty.json diff --git a/locales/en_US/error.json b/locales/en-US/error.json similarity index 100% rename from locales/en_US/error.json rename to locales/en-US/error.json diff --git a/locales/en_US/market.json b/locales/en-US/market.json similarity index 100% rename from locales/en_US/market.json rename to locales/en-US/market.json diff --git a/locales/en_US/migration.json b/locales/en-US/migration.json similarity index 100% rename from locales/en_US/migration.json rename to locales/en-US/migration.json diff --git a/locales/en_US/plugin.json b/locales/en-US/plugin.json similarity index 100% rename from locales/en_US/plugin.json rename to locales/en-US/plugin.json diff --git a/locales/en_US/setting.json b/locales/en-US/setting.json similarity index 100% rename from locales/en_US/setting.json rename to locales/en-US/setting.json diff --git a/locales/en_US/welcome.json b/locales/en-US/welcome.json similarity index 100% rename from locales/en_US/welcome.json rename to locales/en-US/welcome.json diff --git a/locales/es_ES/chat.json b/locales/es-ES/chat.json similarity index 100% rename from locales/es_ES/chat.json rename to locales/es-ES/chat.json diff --git a/locales/es_ES/common.json b/locales/es-ES/common.json similarity index 100% rename from locales/es_ES/common.json rename to locales/es-ES/common.json diff --git a/locales/es_ES/empty.json b/locales/es-ES/empty.json similarity index 100% rename from locales/es_ES/empty.json rename to locales/es-ES/empty.json diff --git a/locales/es_ES/error.json b/locales/es-ES/error.json similarity index 100% rename from locales/es_ES/error.json rename to locales/es-ES/error.json diff --git a/locales/es_ES/market.json b/locales/es-ES/market.json similarity index 100% rename from locales/es_ES/market.json rename to locales/es-ES/market.json diff --git a/locales/es_ES/migration.json b/locales/es-ES/migration.json similarity index 100% rename from locales/es_ES/migration.json rename to locales/es-ES/migration.json diff --git a/locales/es_ES/plugin.json b/locales/es-ES/plugin.json similarity index 100% rename from locales/es_ES/plugin.json rename to locales/es-ES/plugin.json diff --git a/locales/es_ES/setting.json b/locales/es-ES/setting.json similarity index 100% rename from locales/es_ES/setting.json rename to locales/es-ES/setting.json diff --git a/locales/es_ES/welcome.json b/locales/es-ES/welcome.json similarity index 100% rename from locales/es_ES/welcome.json rename to locales/es-ES/welcome.json diff --git a/locales/fr_FR/chat.json b/locales/fr-FR/chat.json similarity index 100% rename from locales/fr_FR/chat.json rename to locales/fr-FR/chat.json diff --git a/locales/fr_FR/common.json b/locales/fr-FR/common.json similarity index 100% rename from locales/fr_FR/common.json rename to locales/fr-FR/common.json diff --git a/locales/fr_FR/empty.json b/locales/fr-FR/empty.json similarity index 100% rename from locales/fr_FR/empty.json rename to locales/fr-FR/empty.json diff --git a/locales/fr_FR/error.json b/locales/fr-FR/error.json similarity index 100% rename from locales/fr_FR/error.json rename to locales/fr-FR/error.json diff --git a/locales/fr_FR/market.json b/locales/fr-FR/market.json similarity index 100% rename from locales/fr_FR/market.json rename to locales/fr-FR/market.json diff --git a/locales/fr_FR/migration.json b/locales/fr-FR/migration.json similarity index 100% rename from locales/fr_FR/migration.json rename to locales/fr-FR/migration.json diff --git a/locales/fr_FR/plugin.json b/locales/fr-FR/plugin.json similarity index 100% rename from locales/fr_FR/plugin.json rename to locales/fr-FR/plugin.json diff --git a/locales/fr_FR/setting.json b/locales/fr-FR/setting.json similarity index 100% rename from locales/fr_FR/setting.json rename to locales/fr-FR/setting.json diff --git a/locales/fr_FR/welcome.json b/locales/fr-FR/welcome.json similarity index 100% rename from locales/fr_FR/welcome.json rename to locales/fr-FR/welcome.json diff --git a/locales/ja_JP/chat.json b/locales/ja-JP/chat.json similarity index 100% rename from locales/ja_JP/chat.json rename to locales/ja-JP/chat.json diff --git a/locales/ja_JP/common.json b/locales/ja-JP/common.json similarity index 100% rename from locales/ja_JP/common.json rename to locales/ja-JP/common.json diff --git a/locales/ja_JP/empty.json b/locales/ja-JP/empty.json similarity index 100% rename from locales/ja_JP/empty.json rename to locales/ja-JP/empty.json diff --git a/locales/ja_JP/error.json b/locales/ja-JP/error.json similarity index 100% rename from locales/ja_JP/error.json rename to locales/ja-JP/error.json diff --git a/locales/ja_JP/market.json b/locales/ja-JP/market.json similarity index 100% rename from locales/ja_JP/market.json rename to locales/ja-JP/market.json diff --git a/locales/ja_JP/migration.json b/locales/ja-JP/migration.json similarity index 100% rename from locales/ja_JP/migration.json rename to locales/ja-JP/migration.json diff --git a/locales/ja_JP/plugin.json b/locales/ja-JP/plugin.json similarity index 100% rename from locales/ja_JP/plugin.json rename to locales/ja-JP/plugin.json diff --git a/locales/ja_JP/setting.json b/locales/ja-JP/setting.json similarity index 100% rename from locales/ja_JP/setting.json rename to locales/ja-JP/setting.json diff --git a/locales/ja_JP/welcome.json b/locales/ja-JP/welcome.json similarity index 100% rename from locales/ja_JP/welcome.json rename to locales/ja-JP/welcome.json diff --git a/locales/ko_KR/chat.json b/locales/ko-KR/chat.json similarity index 100% rename from locales/ko_KR/chat.json rename to locales/ko-KR/chat.json diff --git a/locales/ko_KR/common.json b/locales/ko-KR/common.json similarity index 100% rename from locales/ko_KR/common.json rename to locales/ko-KR/common.json diff --git a/locales/ko_KR/empty.json b/locales/ko-KR/empty.json similarity index 100% rename from locales/ko_KR/empty.json rename to locales/ko-KR/empty.json diff --git a/locales/ko_KR/error.json b/locales/ko-KR/error.json similarity index 100% rename from locales/ko_KR/error.json rename to locales/ko-KR/error.json diff --git a/locales/ko_KR/market.json b/locales/ko-KR/market.json similarity index 100% rename from locales/ko_KR/market.json rename to locales/ko-KR/market.json diff --git a/locales/ko_KR/migration.json b/locales/ko-KR/migration.json similarity index 100% rename from locales/ko_KR/migration.json rename to locales/ko-KR/migration.json diff --git a/locales/ko_KR/plugin.json b/locales/ko-KR/plugin.json similarity index 100% rename from locales/ko_KR/plugin.json rename to locales/ko-KR/plugin.json diff --git a/locales/ko_KR/setting.json b/locales/ko-KR/setting.json similarity index 100% rename from locales/ko_KR/setting.json rename to locales/ko-KR/setting.json diff --git a/locales/ko_KR/welcome.json b/locales/ko-KR/welcome.json similarity index 100% rename from locales/ko_KR/welcome.json rename to locales/ko-KR/welcome.json diff --git a/locales/pt_BR/chat.json b/locales/pt-BR/chat.json similarity index 100% rename from locales/pt_BR/chat.json rename to locales/pt-BR/chat.json diff --git a/locales/pt_BR/common.json b/locales/pt-BR/common.json similarity index 100% rename from locales/pt_BR/common.json rename to locales/pt-BR/common.json diff --git a/locales/pt_BR/empty.json b/locales/pt-BR/empty.json similarity index 100% rename from locales/pt_BR/empty.json rename to locales/pt-BR/empty.json diff --git a/locales/pt_BR/error.json b/locales/pt-BR/error.json similarity index 100% rename from locales/pt_BR/error.json rename to locales/pt-BR/error.json diff --git a/locales/pt_BR/market.json b/locales/pt-BR/market.json similarity index 100% rename from locales/pt_BR/market.json rename to locales/pt-BR/market.json diff --git a/locales/pt_BR/migration.json b/locales/pt-BR/migration.json similarity index 100% rename from locales/pt_BR/migration.json rename to locales/pt-BR/migration.json diff --git a/locales/pt_BR/plugin.json b/locales/pt-BR/plugin.json similarity index 100% rename from locales/pt_BR/plugin.json rename to locales/pt-BR/plugin.json diff --git a/locales/pt_BR/setting.json b/locales/pt-BR/setting.json similarity index 100% rename from locales/pt_BR/setting.json rename to locales/pt-BR/setting.json diff --git a/locales/pt_BR/welcome.json b/locales/pt-BR/welcome.json similarity index 100% rename from locales/pt_BR/welcome.json rename to locales/pt-BR/welcome.json diff --git a/locales/ru_RU/chat.json b/locales/ru-RU/chat.json similarity index 100% rename from locales/ru_RU/chat.json rename to locales/ru-RU/chat.json diff --git a/locales/ru_RU/common.json b/locales/ru-RU/common.json similarity index 100% rename from locales/ru_RU/common.json rename to locales/ru-RU/common.json diff --git a/locales/ru_RU/empty.json b/locales/ru-RU/empty.json similarity index 100% rename from locales/ru_RU/empty.json rename to locales/ru-RU/empty.json diff --git a/locales/ru_RU/error.json b/locales/ru-RU/error.json similarity index 100% rename from locales/ru_RU/error.json rename to locales/ru-RU/error.json diff --git a/locales/ru_RU/market.json b/locales/ru-RU/market.json similarity index 100% rename from locales/ru_RU/market.json rename to locales/ru-RU/market.json diff --git a/locales/ru_RU/migration.json b/locales/ru-RU/migration.json similarity index 100% rename from locales/ru_RU/migration.json rename to locales/ru-RU/migration.json diff --git a/locales/ru_RU/plugin.json b/locales/ru-RU/plugin.json similarity index 100% rename from locales/ru_RU/plugin.json rename to locales/ru-RU/plugin.json diff --git a/locales/ru_RU/setting.json b/locales/ru-RU/setting.json similarity index 99% rename from locales/ru_RU/setting.json rename to locales/ru-RU/setting.json index fb2371cc39f0..8abed14f58fc 100644 --- a/locales/ru_RU/setting.json +++ b/locales/ru-RU/setting.json @@ -303,4 +303,4 @@ }, "title": "Дополнительные инструменты" } - } +} diff --git a/locales/ru_RU/welcome.json b/locales/ru-RU/welcome.json similarity index 100% rename from locales/ru_RU/welcome.json rename to locales/ru-RU/welcome.json diff --git a/locales/tr_TR/chat.json b/locales/tr-TR/chat.json similarity index 100% rename from locales/tr_TR/chat.json rename to locales/tr-TR/chat.json diff --git a/locales/tr_TR/common.json b/locales/tr-TR/common.json similarity index 100% rename from locales/tr_TR/common.json rename to locales/tr-TR/common.json diff --git a/locales/tr_TR/empty.json b/locales/tr-TR/empty.json similarity index 100% rename from locales/tr_TR/empty.json rename to locales/tr-TR/empty.json diff --git a/locales/tr_TR/error.json b/locales/tr-TR/error.json similarity index 100% rename from locales/tr_TR/error.json rename to locales/tr-TR/error.json diff --git a/locales/tr_TR/market.json b/locales/tr-TR/market.json similarity index 100% rename from locales/tr_TR/market.json rename to locales/tr-TR/market.json diff --git a/locales/tr_TR/migration.json b/locales/tr-TR/migration.json similarity index 100% rename from locales/tr_TR/migration.json rename to locales/tr-TR/migration.json diff --git a/locales/tr_TR/plugin.json b/locales/tr-TR/plugin.json similarity index 100% rename from locales/tr_TR/plugin.json rename to locales/tr-TR/plugin.json diff --git a/locales/tr_TR/setting.json b/locales/tr-TR/setting.json similarity index 100% rename from locales/tr_TR/setting.json rename to locales/tr-TR/setting.json diff --git a/locales/tr_TR/welcome.json b/locales/tr-TR/welcome.json similarity index 100% rename from locales/tr_TR/welcome.json rename to locales/tr-TR/welcome.json diff --git a/locales/zh_CN/chat.json b/locales/zh-CN/chat.json similarity index 100% rename from locales/zh_CN/chat.json rename to locales/zh-CN/chat.json diff --git a/locales/zh_CN/common.json b/locales/zh-CN/common.json similarity index 100% rename from locales/zh_CN/common.json rename to locales/zh-CN/common.json diff --git a/locales/zh_CN/empty.json b/locales/zh-CN/empty.json similarity index 100% rename from locales/zh_CN/empty.json rename to locales/zh-CN/empty.json diff --git a/locales/zh_CN/error.json b/locales/zh-CN/error.json similarity index 100% rename from locales/zh_CN/error.json rename to locales/zh-CN/error.json diff --git a/locales/zh_CN/market.json b/locales/zh-CN/market.json similarity index 100% rename from locales/zh_CN/market.json rename to locales/zh-CN/market.json diff --git a/locales/zh_CN/migration.json b/locales/zh-CN/migration.json similarity index 100% rename from locales/zh_CN/migration.json rename to locales/zh-CN/migration.json diff --git a/locales/zh_CN/plugin.json b/locales/zh-CN/plugin.json similarity index 100% rename from locales/zh_CN/plugin.json rename to locales/zh-CN/plugin.json diff --git a/locales/zh_CN/setting.json b/locales/zh-CN/setting.json similarity index 100% rename from locales/zh_CN/setting.json rename to locales/zh-CN/setting.json diff --git a/locales/zh_CN/welcome.json b/locales/zh-CN/welcome.json similarity index 100% rename from locales/zh_CN/welcome.json rename to locales/zh-CN/welcome.json diff --git a/locales/zh_TW/chat.json b/locales/zh-TW/chat.json similarity index 100% rename from locales/zh_TW/chat.json rename to locales/zh-TW/chat.json diff --git a/locales/zh_TW/common.json b/locales/zh-TW/common.json similarity index 100% rename from locales/zh_TW/common.json rename to locales/zh-TW/common.json diff --git a/locales/zh_TW/empty.json b/locales/zh-TW/empty.json similarity index 100% rename from locales/zh_TW/empty.json rename to locales/zh-TW/empty.json diff --git a/locales/zh_TW/error.json b/locales/zh-TW/error.json similarity index 100% rename from locales/zh_TW/error.json rename to locales/zh-TW/error.json diff --git a/locales/zh_TW/market.json b/locales/zh-TW/market.json similarity index 100% rename from locales/zh_TW/market.json rename to locales/zh-TW/market.json diff --git a/locales/zh_TW/migration.json b/locales/zh-TW/migration.json similarity index 100% rename from locales/zh_TW/migration.json rename to locales/zh-TW/migration.json diff --git a/locales/zh_TW/plugin.json b/locales/zh-TW/plugin.json similarity index 100% rename from locales/zh_TW/plugin.json rename to locales/zh-TW/plugin.json diff --git a/locales/zh_TW/setting.json b/locales/zh-TW/setting.json similarity index 100% rename from locales/zh_TW/setting.json rename to locales/zh-TW/setting.json diff --git a/locales/zh_TW/welcome.json b/locales/zh-TW/welcome.json similarity index 100% rename from locales/zh_TW/welcome.json rename to locales/zh-TW/welcome.json diff --git a/scripts/i18nWorkflow/const.ts b/scripts/i18nWorkflow/const.ts index 07c1d266b19a..4142fcf09c8f 100644 --- a/scripts/i18nWorkflow/const.ts +++ b/scripts/i18nWorkflow/const.ts @@ -13,7 +13,6 @@ export const entryLocaleJsonFilepath = (file: string) => resolve(localesDir, i18nConfig.entryLocale, file); export const outputLocaleJsonFilepath = (locale: string, file: string) => resolve(localesDir, locale, file); -export const srcLocalesResources = resolve(root, srcLocalesDir, 'resources'); -export const localesResourcesFilepath = (locale: string) => resolve(srcLocalesResources, locale); +export const srcDefaultLocales = resolve(root, srcLocalesDir, 'default'); export { default as i18nConfig } from '../../.i18nrc'; diff --git a/scripts/i18nWorkflow/genDefaultLocale.ts b/scripts/i18nWorkflow/genDefaultLocale.ts index cd441fddc221..2d672a59275b 100644 --- a/scripts/i18nWorkflow/genDefaultLocale.ts +++ b/scripts/i18nWorkflow/genDefaultLocale.ts @@ -1,13 +1,13 @@ import { consola } from 'consola'; import { colors } from 'consola/utils'; -import { entryLocaleJsonFilepath, i18nConfig, localesResourcesFilepath } from './const'; +import { entryLocaleJsonFilepath, i18nConfig, srcDefaultLocales } from './const'; import { tagWhite, writeJSON } from './utils'; export const genDefaultLocale = () => { consola.info(`Default locale is ${i18nConfig.entryLocale}...`); - const resources = require(localesResourcesFilepath(i18nConfig.entryLocale)); + const resources = require(srcDefaultLocales); const data = Object.entries(resources.default); consola.start(`Generate default locale json, found ${data.length} namespaces...`); diff --git a/scripts/i18nWorkflow/genDiff.ts b/scripts/i18nWorkflow/genDiff.ts index c2727b0f6488..4071a467f43e 100644 --- a/scripts/i18nWorkflow/genDiff.ts +++ b/scripts/i18nWorkflow/genDiff.ts @@ -7,15 +7,15 @@ import { existsSync } from 'node:fs'; import { entryLocaleJsonFilepath, i18nConfig, - localesResourcesFilepath, outputLocaleJsonFilepath, + srcDefaultLocales, } from './const'; import { readJSON, tagWhite, writeJSON } from './utils'; export const genDiff = () => { consola.start(`Diff between Dev/Prod local...`); - const resources = require(localesResourcesFilepath(i18nConfig.entryLocale)); + const resources = require(srcDefaultLocales); const data = Object.entries(resources.default); for (const [ns, devJSON] of data) { diff --git a/scripts/i18nWorkflow/genResources.ts b/scripts/i18nWorkflow/genResources.ts deleted file mode 100644 index 437b866e2d4b..000000000000 --- a/scripts/i18nWorkflow/genResources.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { consola } from 'consola'; -import { colors } from 'consola/utils'; -import { writeFileSync } from 'node:fs'; - -import { i18nConfig, localeDirJsonList, localesResourcesFilepath } from './const'; -import { genResourcesContent, genToc, tagBlue, tagGreen } from './utils'; - -const locales = [i18nConfig.entryLocale, ...i18nConfig.outputLocales]; - -export const genResources = () => { - consola.start(`Generate locale resources and types, found ${locales.length} locales...`); - - for (const locale of locales) { - const files = localeDirJsonList(locale); - const toc = genToc(files, locale); - - const filepath = localesResourcesFilepath(`${locale}.ts`); - writeFileSync(filepath, toc); - consola.success(tagBlue(locale), colors.gray(filepath)); - } - - const indexFilepath = localesResourcesFilepath(`index.ts`); - writeFileSync(indexFilepath, genResourcesContent(locales)); - - consola.success(tagGreen('INDEX'), colors.gray(indexFilepath)); -}; diff --git a/scripts/i18nWorkflow/index.ts b/scripts/i18nWorkflow/index.ts index 8bd0c35eb78c..395750fb43e7 100644 --- a/scripts/i18nWorkflow/index.ts +++ b/scripts/i18nWorkflow/index.ts @@ -1,6 +1,5 @@ import { genDefaultLocale } from './genDefaultLocale'; import { genDiff } from './genDiff'; -import { genResources } from './genResources'; import { split } from './utils'; split('DIFF ANALYSIS'); @@ -9,7 +8,4 @@ genDiff(); split('GENERATE DEFAULT LOCALE'); genDefaultLocale(); -split('GENERATE RESOURCE & TOC'); -genResources(); - split('GENERATE I18N FILES'); diff --git a/scripts/i18nWorkflow/utils.ts b/scripts/i18nWorkflow/utils.ts index 4f3c17a88b73..95c2d2a997f6 100644 --- a/scripts/i18nWorkflow/utils.ts +++ b/scripts/i18nWorkflow/utils.ts @@ -1,11 +1,9 @@ import { consola } from 'consola'; import { colors } from 'consola/utils'; -import { tocForResources } from 'i18next-resources-for-ts'; import { readFileSync, writeFileSync } from 'node:fs'; import { resolve } from 'node:path'; import i18nConfig from '../../.i18nrc'; -import { srcLocalesResources } from './const'; export const readJSON = (filePath: string) => { const data = readFileSync(filePath, 'utf8'); @@ -48,15 +46,6 @@ export const genNamespaceList = (files: string[], locale: string) => { })); }; -export const genToc = (files: string[], locale: string) => { - const ns = genNamespaceList(files, locale); - let toc = tocForResources(ns, srcLocalesResources).replaceAll('\\', '/'); - if (locale === i18nConfig.entryLocale) { - toc = toc.replaceAll('.json', '').replaceAll('../../../locales/zh_CN', '../default'); - } - return toc; -}; - export const tagBlue = (text: string) => colors.bgBlueBright(colors.black(` ${text} `)); export const tagYellow = (text: string) => colors.bgYellowBright(colors.black(` ${text} `)); export const tagGreen = (text: string) => colors.bgGreenBright(colors.black(` ${text} `)); diff --git a/src/locales/create.ts b/src/locales/create.ts index 0c722fb9b017..9228717f5f17 100644 --- a/src/locales/create.ts +++ b/src/locales/create.ts @@ -1,43 +1,46 @@ import i18n from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; +import resourcesToBackend from 'i18next-resources-to-backend'; import { initReactI18next } from 'react-i18next'; import { getClientConfig } from '@/config/client'; import { DEFAULT_LANG, LOBE_LOCALE_COOKIE } from '@/const/locale'; import { COOKIE_CACHE_DAYS } from '@/const/settings'; -import type { Namespaces } from '@/types/locale'; -import { isOnServerSide } from '@/utils/env'; - -import { resources } from './options'; +import { isDev, isOnServerSide } from '@/utils/env'; const { I18N_DEBUG, I18N_DEBUG_BROWSER, I18N_DEBUG_SERVER } = getClientConfig(); const debugMode = I18N_DEBUG ?? isOnServerSide ? I18N_DEBUG_SERVER : I18N_DEBUG_BROWSER; export const createI18nNext = (lang?: string) => { - const ns: Namespaces[] = [ - 'error', - 'common', - 'empty', - 'error', - 'market', - 'plugin', - 'setting', - 'welcome', - ]; + const instance = i18n + .use(initReactI18next) + .use(LanguageDetector) + .use( + resourcesToBackend(async (lng: string, ns: string) => { + switch (lng) { + case 'zh-CN': + case 'zh': { + if (isDev) return import(`./default/${ns}`); + + return import(`@/../locales/zh-CN/${ns}.json`); + } - const instance = i18n.use(initReactI18next).use(LanguageDetector); - // not find a good way to handle suspense loading - // .use( - // resourcesToBackend( - // isDev ? resources : (lng: string, ns: string) => import(`../../locales/${lng}/${ns}.json`), - // ), - // ) + case 'en': { + return import(`@/../locales/en-US/${ns}.json`); + } + + default: { + return import(`@/../locales/${lng}/${ns}.json`); + } + } + }), + ); return { init: () => instance.init({ debug: debugMode, - defaultNS: ['error', 'common'], + defaultNS: ['error', 'common', 'empty'], detection: { caches: ['cookie'], cookieMinutes: 60 * 24 * COOKIE_CACHE_DAYS, @@ -48,8 +51,6 @@ export const createI18nNext = (lang?: string) => { escapeValue: false, }, lng: lang, - ns, - resources, }), instance, }; diff --git a/src/locales/default/index.ts b/src/locales/default/index.ts new file mode 100644 index 000000000000..50a398a0cf59 --- /dev/null +++ b/src/locales/default/index.ts @@ -0,0 +1,23 @@ +import chat from './chat'; +import common from './common'; +import empty from './empty'; +import error from './error'; +import market from './market'; +import migration from './migration'; +import plugin from './plugin'; +import setting from './setting'; +import welcome from './welcome'; + +const resources = { + chat, + common, + empty, + error, + market, + migration, + plugin, + setting, + welcome, +} as const; + +export default resources; diff --git a/src/locales/options.ts b/src/locales/options.ts index 636c8daf0ac5..2e5bb3c7e68f 100644 --- a/src/locales/options.ts +++ b/src/locales/options.ts @@ -1,6 +1,6 @@ import type { Locales } from '@/types/locale'; -import _resources from './resources'; +import { locales } from './resources'; type LocaleOptions = { label: string; @@ -54,12 +54,4 @@ export const localeOptions: LocaleOptions = [ }, ] as LocaleOptions; -export const supportLangs: string[] = localeOptions.map((i) => i.value); - -export const resources = { - ..._resources, - en: _resources['en-US'], - zh: _resources['zh-CN'], -}; - -export const supportLocales = Object.keys(resources); +export const supportLocales: string[] = [...locales, 'en', 'zh']; diff --git a/src/locales/resources.ts b/src/locales/resources.ts new file mode 100644 index 000000000000..b62ef948174c --- /dev/null +++ b/src/locales/resources.ts @@ -0,0 +1,18 @@ +import resources from './default'; + +export const locales = [ + 'de-DE', + 'en-US', + 'es-ES', + 'fr-FR', + 'ja-JP', + 'ko-KR', + 'pt-BR', + 'ru-RU', + 'tr-TR', + 'zh-CN', + 'zh-TW', +] as const; + +export type DefaultResources = typeof resources; +export type Locales = (typeof locales)[number]; diff --git a/src/locales/resources/de_DE.ts b/src/locales/resources/de_DE.ts deleted file mode 100644 index d750e846b70f..000000000000 --- a/src/locales/resources/de_DE.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/de_DE/chat.json'; -import common from '../../../locales/de_DE/common.json'; -import empty from '../../../locales/de_DE/empty.json'; -import error from '../../../locales/de_DE/error.json'; -import market from '../../../locales/de_DE/market.json'; -import migration from '../../../locales/de_DE/migration.json'; -import plugin from '../../../locales/de_DE/plugin.json'; -import setting from '../../../locales/de_DE/setting.json'; -import welcome from '../../../locales/de_DE/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/en_US.ts b/src/locales/resources/en_US.ts deleted file mode 100644 index 7b0fd7123223..000000000000 --- a/src/locales/resources/en_US.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/en_US/chat.json'; -import common from '../../../locales/en_US/common.json'; -import empty from '../../../locales/en_US/empty.json'; -import error from '../../../locales/en_US/error.json'; -import market from '../../../locales/en_US/market.json'; -import migration from '../../../locales/en_US/migration.json'; -import plugin from '../../../locales/en_US/plugin.json'; -import setting from '../../../locales/en_US/setting.json'; -import welcome from '../../../locales/en_US/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/es_ES.ts b/src/locales/resources/es_ES.ts deleted file mode 100644 index ccd2bc83b288..000000000000 --- a/src/locales/resources/es_ES.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/es_ES/chat.json'; -import common from '../../../locales/es_ES/common.json'; -import empty from '../../../locales/es_ES/empty.json'; -import error from '../../../locales/es_ES/error.json'; -import market from '../../../locales/es_ES/market.json'; -import migration from '../../../locales/es_ES/migration.json'; -import plugin from '../../../locales/es_ES/plugin.json'; -import setting from '../../../locales/es_ES/setting.json'; -import welcome from '../../../locales/es_ES/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/fr_FR.ts b/src/locales/resources/fr_FR.ts deleted file mode 100644 index c9f86286b0dc..000000000000 --- a/src/locales/resources/fr_FR.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/fr_FR/chat.json'; -import common from '../../../locales/fr_FR/common.json'; -import empty from '../../../locales/fr_FR/empty.json'; -import error from '../../../locales/fr_FR/error.json'; -import market from '../../../locales/fr_FR/market.json'; -import migration from '../../../locales/fr_FR/migration.json'; -import plugin from '../../../locales/fr_FR/plugin.json'; -import setting from '../../../locales/fr_FR/setting.json'; -import welcome from '../../../locales/fr_FR/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/index.ts b/src/locales/resources/index.ts deleted file mode 100644 index 9d9dc05f9eb9..000000000000 --- a/src/locales/resources/index.ts +++ /dev/null @@ -1,31 +0,0 @@ -import de_DE from './de_DE'; -import en_US from './en_US'; -import es_ES from './es_ES'; -import fr_FR from './fr_FR'; -import ja_JP from './ja_JP'; -import ko_KR from './ko_KR'; -import pt_BR from './pt_BR'; -import ru_RU from './ru_RU'; -import tr_TR from './tr_TR'; -import zh_CN from './zh_CN'; -import zh_TW from './zh_TW'; - -const resources = { - 'de-DE': de_DE, - 'en-US': en_US, - 'es-ES': es_ES, - 'fr-FR': fr_FR, - 'ja-JP': ja_JP, - 'ko-KR': ko_KR, - 'pt-BR': pt_BR, - 'ru-RU': ru_RU, - 'tr-TR': tr_TR, - 'zh-CN': zh_CN, - 'zh-TW': zh_TW, -} as const; -export default resources; -export const defaultResources = zh_CN; -export type Resources = typeof resources; -export type DefaultResources = typeof defaultResources; -export type Namespaces = keyof DefaultResources; -export type Locales = keyof Resources; diff --git a/src/locales/resources/ja_JP.ts b/src/locales/resources/ja_JP.ts deleted file mode 100644 index bc7ee846f3b8..000000000000 --- a/src/locales/resources/ja_JP.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/ja_JP/chat.json'; -import common from '../../../locales/ja_JP/common.json'; -import empty from '../../../locales/ja_JP/empty.json'; -import error from '../../../locales/ja_JP/error.json'; -import market from '../../../locales/ja_JP/market.json'; -import migration from '../../../locales/ja_JP/migration.json'; -import plugin from '../../../locales/ja_JP/plugin.json'; -import setting from '../../../locales/ja_JP/setting.json'; -import welcome from '../../../locales/ja_JP/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/ko_KR.ts b/src/locales/resources/ko_KR.ts deleted file mode 100644 index 5326b6ff9d17..000000000000 --- a/src/locales/resources/ko_KR.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/ko_KR/chat.json'; -import common from '../../../locales/ko_KR/common.json'; -import empty from '../../../locales/ko_KR/empty.json'; -import error from '../../../locales/ko_KR/error.json'; -import market from '../../../locales/ko_KR/market.json'; -import migration from '../../../locales/ko_KR/migration.json'; -import plugin from '../../../locales/ko_KR/plugin.json'; -import setting from '../../../locales/ko_KR/setting.json'; -import welcome from '../../../locales/ko_KR/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/pt_BR.ts b/src/locales/resources/pt_BR.ts deleted file mode 100644 index fc3a074a4d89..000000000000 --- a/src/locales/resources/pt_BR.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/pt_BR/chat.json'; -import common from '../../../locales/pt_BR/common.json'; -import empty from '../../../locales/pt_BR/empty.json'; -import error from '../../../locales/pt_BR/error.json'; -import market from '../../../locales/pt_BR/market.json'; -import migration from '../../../locales/pt_BR/migration.json'; -import plugin from '../../../locales/pt_BR/plugin.json'; -import setting from '../../../locales/pt_BR/setting.json'; -import welcome from '../../../locales/pt_BR/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/ru_RU.ts b/src/locales/resources/ru_RU.ts deleted file mode 100644 index 1b67397a868d..000000000000 --- a/src/locales/resources/ru_RU.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/ru_RU/chat.json'; -import common from '../../../locales/ru_RU/common.json'; -import empty from '../../../locales/ru_RU/empty.json'; -import error from '../../../locales/ru_RU/error.json'; -import market from '../../../locales/ru_RU/market.json'; -import migration from '../../../locales/ru_RU/migration.json'; -import plugin from '../../../locales/ru_RU/plugin.json'; -import setting from '../../../locales/ru_RU/setting.json'; -import welcome from '../../../locales/ru_RU/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/tr_TR.ts b/src/locales/resources/tr_TR.ts deleted file mode 100644 index 0a3782cc3739..000000000000 --- a/src/locales/resources/tr_TR.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/tr_TR/chat.json'; -import common from '../../../locales/tr_TR/common.json'; -import empty from '../../../locales/tr_TR/empty.json'; -import error from '../../../locales/tr_TR/error.json'; -import market from '../../../locales/tr_TR/market.json'; -import migration from '../../../locales/tr_TR/migration.json'; -import plugin from '../../../locales/tr_TR/plugin.json'; -import setting from '../../../locales/tr_TR/setting.json'; -import welcome from '../../../locales/tr_TR/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/zh_CN.ts b/src/locales/resources/zh_CN.ts deleted file mode 100644 index 6c7f946642b4..000000000000 --- a/src/locales/resources/zh_CN.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../default/chat'; -import common from '../default/common'; -import empty from '../default/empty'; -import error from '../default/error'; -import market from '../default/market'; -import migration from '../default/migration'; -import plugin from '../default/plugin'; -import setting from '../default/setting'; -import welcome from '../default/welcome'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources; diff --git a/src/locales/resources/zh_TW.ts b/src/locales/resources/zh_TW.ts deleted file mode 100644 index 5238405b98cd..000000000000 --- a/src/locales/resources/zh_TW.ts +++ /dev/null @@ -1,23 +0,0 @@ -import chat from '../../../locales/zh_TW/chat.json'; -import common from '../../../locales/zh_TW/common.json'; -import empty from '../../../locales/zh_TW/empty.json'; -import error from '../../../locales/zh_TW/error.json'; -import market from '../../../locales/zh_TW/market.json'; -import migration from '../../../locales/zh_TW/migration.json'; -import plugin from '../../../locales/zh_TW/plugin.json'; -import setting from '../../../locales/zh_TW/setting.json'; -import welcome from '../../../locales/zh_TW/welcome.json'; - -const resources = { - chat, - common, - empty, - error, - market, - migration, - plugin, - setting, - welcome, -} as const; - -export default resources;