diff options
| author | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
| commit | 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 (patch) | |
| tree | 8a5587f10ca55b162d7e3254cb088b323a34c41b /i18n/index.ts | |
initial commit
Diffstat (limited to 'i18n/index.ts')
| -rw-r--r-- | i18n/index.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/i18n/index.ts b/i18n/index.ts new file mode 100644 index 00000000..c78ecd1e --- /dev/null +++ b/i18n/index.ts @@ -0,0 +1,34 @@ +import { createInstance, Namespace, FlatNamespace, KeyPrefix } from 'i18next'; +import resourcesToBackend from 'i18next-resources-to-backend'; +import { initReactI18next } from 'react-i18next/initReactI18next'; +import { FallbackNs } from 'react-i18next'; + +import { getOptions } from '@/i18n/settings'; + +const initI18next = async (lng: string, ns: string | string[]) => { + const i18nInstance = createInstance(); + await i18nInstance + .use(initReactI18next) + .use( + resourcesToBackend( + (language: string, namespace: string) => + import(`./locales/${language}/${namespace}.json`) + ) + ) + .init(getOptions(lng, ns)); + return i18nInstance; +}; + +export async function useTranslation< + Ns extends FlatNamespace, + KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined, +>(lng: string, ns?: Ns, options: { keyPrefix?: KPrefix } = {}) { + const i18nextInstance = await initI18next( + lng, + Array.isArray(ns) ? (ns as string[]) : (ns as string) + ); + return { + t: i18nextInstance.getFixedT(lng, ns, options.keyPrefix), + i18n: i18nextInstance, + }; +}
\ No newline at end of file |
