Skip to content

Commit

Permalink
update types for i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Feb 5, 2024
1 parent 0d1b0dc commit 097429c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
33 changes: 25 additions & 8 deletions apps/core/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
import { notFound } from 'next/navigation';
import { getRequestConfig } from 'next-intl/server';
import { z } from 'zod';

enum LocalePrefixes {
ALWAYS = 'always',
NEVER = 'never',
ASNEEDED = 'as-needed', // remove prefix on default locale
ASNEEDED = 'as-needed', // removes prefix on default locale
}

const locales = ['en', 'de'] as const;

type LocalePrefixesType = `${LocalePrefixes}`;

// Temporary we use NEVER prefix to give priority to accept-language header
// Temporary we use NEVER prefix to prioritize accept-language header
// & disable internationalized routes due to multi-language in Catalyst is in progress
const localePrefix: LocalePrefixesType = LocalePrefixes.NEVER;
const defaultLocale = 'en';

type LocaleType = (typeof locales)[number];

interface AbstractDictionary {
[id: string]: AbstractDictionary | string;
}

const translationsSchema: z.ZodType<AbstractDictionary> = z.lazy(() =>
z.record(translationsSchema),
);

export default getRequestConfig(async (params) => {
const { locale } = params;

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
if (!locales.includes(locale as LocaleType)) notFound();
if (!locales.includes(locale as LocaleType)) notFound(); // eslint-disable-line

const isLocaleFound = locales.includes(locale as LocaleType); // eslint-disable-line

const dictionary: unknown = await import(
`../core/dictionaries/${isLocaleFound ? locale : 'en'}.json`
);
const result = translationsSchema.safeParse(dictionary);

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const isLocaleFound = locales.includes(locale as LocaleType);
if (result.success) {
return {
messages: result.data,
};
}

return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
messages: (await import(`../core/dictionaries/${isLocaleFound ? locale : 'en'}.json`)).default,
messages: undefined,
};
});

Expand Down
10 changes: 4 additions & 6 deletions apps/core/middlewares/with-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import { type MiddlewareFactory } from './compose-middlewares';
const isPriorityToAcceptLanguageHeaderEnabled = localePrefix === LocalePrefixes.NEVER;

// we use negotiator and intl-localematcher to get best locale
const getLocale = (request: NextRequest): LocaleType => {
const getLocale = (request: NextRequest): string => {
const negotiatorHeaders: Record<string, string> = {};

request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const supportedLocales = locales as unknown as string[];
const supportedLocales = [...locales];
const languages = new Negotiator({ headers: negotiatorHeaders }).languages(supportedLocales);
const locale = matchLocale(languages, supportedLocales, defaultLocale);

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return locale as LocaleType;
return locale;
};

const updateLocaleCookie = (currentLocale: string, request: NextRequest) => {
Expand All @@ -42,7 +40,7 @@ export const withI18n: MiddlewareFactory = () => {
const intlMiddleware = createMiddleware({
locales,
localePrefix,
defaultLocale: locale,
defaultLocale: locale as LocaleType, // eslint-disable-line
});
const response = intlMiddleware(request);

Expand Down

0 comments on commit 097429c

Please sign in to comment.