Cleanup
This commit is contained in:
17
apps/web/lib/i18n/i18n.resolver.ts
Normal file
17
apps/web/lib/i18n/i18n.resolver.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Resolves the translation file for a given language and namespace.
|
||||
*
|
||||
*/
|
||||
export async function i18nResolver(language: string, namespace: string) {
|
||||
try {
|
||||
const { default: data } = await import(
|
||||
`../../public/locales/${language}/${namespace}.json`
|
||||
);
|
||||
|
||||
return data as Record<string, string>;
|
||||
} catch (e) {
|
||||
console.error('Could not load translation file', e);
|
||||
|
||||
return {} as Record<string, string>;
|
||||
}
|
||||
}
|
||||
10
apps/web/lib/i18n/i18n.server.ts
Normal file
10
apps/web/lib/i18n/i18n.server.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import getLanguageCookie from '@kit/i18n/cookie';
|
||||
import { initializeServerI18n } from '@kit/i18n/server';
|
||||
|
||||
import { i18nResolver } from './i18n.resolver';
|
||||
|
||||
export function createI18nServerInstance() {
|
||||
const cookie = getLanguageCookie();
|
||||
|
||||
return initializeServerI18n(cookie, i18nResolver);
|
||||
}
|
||||
13
apps/web/lib/i18n/with-i18n.tsx
Normal file
13
apps/web/lib/i18n/with-i18n.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
import { createI18nServerInstance } from './i18n.server';
|
||||
|
||||
type LayoutOrPageComponent<Params> = React.ComponentType<Params>;
|
||||
|
||||
export function withI18n<Params extends object>(
|
||||
Component: LayoutOrPageComponent<Params>,
|
||||
) {
|
||||
return async function I18nServerComponentWrapper(params: Params) {
|
||||
await createI18nServerInstance();
|
||||
|
||||
return <Component {...params} />;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user