summaryrefslogtreecommitdiff
path: root/i18n/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'i18n/index.ts')
-rw-r--r--i18n/index.ts34
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