diff --git a/web/i18n-config/i18next-config.ts b/web/i18n-config/i18next-config.ts index bba10b6a34..d3a138570b 100644 --- a/web/i18n-config/i18next-config.ts +++ b/web/i18n-config/i18next-config.ts @@ -69,10 +69,6 @@ export const namespaces = { workflow, } -export type Namespaces = typeof namespaces - -// pluginTrigger -> plugin-trigger - export type KebabCase = S extends `${infer T}${infer U}` ? T extends Lowercase ? `${T}${KebabCase}` @@ -83,10 +79,10 @@ export type CamelCase = S extends `${infer T}-${infer U}` ? `${T}${Capitalize>}` : S -export type KeyPrefix = keyof typeof namespaces -export type Namespace = KebabCase +export type NamespaceCamelCase = keyof typeof namespaces +export type NamespaceKebabCase = KebabCase -const requireSilent = async (lang: Locale, namespace: Namespace) => { +const requireSilent = async (lang: Locale, namespace: NamespaceKebabCase) => { let res try { res = (await import(`../i18n/${lang}/${namespace}.json`)).default @@ -98,11 +94,11 @@ const requireSilent = async (lang: Locale, namespace: Namespace) => { return res } -const NAMESPACES = Object.keys(namespaces).map(kebabCase) as Namespace[] +const NAMESPACES = Object.keys(namespaces).map(kebabCase) as NamespaceKebabCase[] // Load a single namespace for a language -export const loadNamespace = async (lang: Locale, ns: Namespace) => { - const camelNs = camelCase(ns) as KeyPrefix +export const loadNamespace = async (lang: Locale, ns: NamespaceKebabCase) => { + const camelNs = camelCase(ns) as NamespaceCamelCase if (i18n.hasResourceBundle(lang, camelNs)) return diff --git a/web/i18n-config/server.ts b/web/i18n-config/server.ts index 1586ea1a8e..61d20e2ee9 100644 --- a/web/i18n-config/server.ts +++ b/web/i18n-config/server.ts @@ -1,5 +1,5 @@ import type { Locale } from '.' -import type { KeyPrefix, Namespace } from './i18next-config' +import type { NamespaceCamelCase, NamespaceKebabCase } from './i18next-config' import { match } from '@formatjs/intl-localematcher' import { camelCase } from 'es-toolkit/compat' import { createInstance } from 'i18next' @@ -10,11 +10,11 @@ import { initReactI18next } from 'react-i18next/initReactI18next' import { i18n } from '.' // https://locize.com/blog/next-13-app-dir-i18n/ -const initI18next = async (lng: Locale, ns: Namespace) => { +const initI18next = async (lng: Locale, ns: NamespaceKebabCase) => { const i18nInstance = createInstance() await i18nInstance .use(initReactI18next) - .use(resourcesToBackend((language: Locale, namespace: string) => { + .use(resourcesToBackend((language: Locale, namespace: NamespaceKebabCase) => { return import(`../i18n/${language}/${namespace}.json`) })) .init({ @@ -26,8 +26,8 @@ const initI18next = async (lng: Locale, ns: Namespace) => { return i18nInstance } -export async function getTranslation(lng: Locale, ns: Namespace) { - const camelNs = camelCase(ns) as KeyPrefix +export async function getTranslation(lng: Locale, ns: NamespaceKebabCase) { + const camelNs = camelCase(ns) as NamespaceCamelCase const i18nextInstance = await initI18next(lng, ns) return { t: i18nextInstance.getFixedT(lng, camelNs),