Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
feat: add unsafe helpers for server
Browse files Browse the repository at this point in the history
  • Loading branch information
matejm committed Oct 30, 2023
1 parent 2048da8 commit ef52747
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/i18n/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@

import { ReactNode } from 'react';
import { createI18nClient } from 'next-international/client';
import type { Locale, UnsafeT } from './i18n';

import { defaultLocale, Locale } from './i18n';
import { defaultLocale } from './i18n';

export const { useI18n, useScopedI18n, I18nProviderClient } = createI18nClient({
en: () => import('./en'),
sl: () => import('./sl'),
});

type UnsafeT = (
localeKey: string,
params?: Record<string, string | ReactNode | number>,
) => string | ReactNode;

export const useUnsafeI18n = useI18n as unknown as () => UnsafeT;
export const useUnsafeScopedI18n = useScopedI18n as unknown as () => UnsafeT;

Expand Down
7 changes: 7 additions & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ReactNode } from 'react';

export const LOCALES = ['en', 'sl'] as const;

export type Locale = (typeof LOCALES)[number];
Expand All @@ -13,6 +15,11 @@ export const isLocale = (
return LOCALES.includes(locale as Locale);
};

export type UnsafeT = (
localeKey: string,
params?: Record<string, string | ReactNode | number>,
) => string | ReactNode;

// Assert that defaultLocale is in LOCALES
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _checkDefaultLocale: Locale = defaultLocale;
4 changes: 4 additions & 0 deletions src/i18n/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createI18nServer } from 'next-international/server';
import type { UnsafeT } from './i18n';

export const { getI18n, getScopedI18n, getStaticParams } = createI18nServer({
en: () => import('./en'),
sl: () => import('./sl'),
});

export const getUnsafeI18n = getI18n as unknown as () => UnsafeT;
export const getUnsafeScopedI18n = getScopedI18n as unknown as () => UnsafeT;

0 comments on commit ef52747

Please sign in to comment.