i18n.ts 1.3 KB
Newer Older
1
import {NextComponentType, NextPageContext} from 'next';
2

3
import NextI18Next from '@visualdl/i18n';
P
Peter Pan 已提交
4
import {env} from '../next.config';
5
import moment from 'moment';
P
Peter Pan 已提交
6 7 8 9

const defaultLanguage = env.DEFAULT_LANGUAGE;
const allLanguages = env.LANGUAGES;
const otherLanguages = allLanguages.filter(lang => lang !== defaultLanguage);
10

11
const isDev = process.env.NODE_ENV === 'development';
12

P
Peter Pan 已提交
13 14 15 16
allLanguages.forEach(async (lang: string) => {
    moment.updateLocale(lang, await import(`../public/locales/${lang}/moment.json`));
});

17
const nextI18Next = new NextI18Next({
P
Peter Pan 已提交
18
    localePath: env.LOCALE_PATH,
19 20
    browserLanguageDetection: !isDev,
    serverLanguageDetection: !isDev,
P
Peter Pan 已提交
21 22 23 24 25 26 27
    cleanCode: true,
    defaultLanguage,
    otherLanguages,
    localeSubpaths: otherLanguages.reduce((prev, curr) => {
        prev[curr] = curr;
        return prev;
    }, {} as Record<string, string>)
28
});
29

30 31
export default nextI18Next;

32
export const {i18n, config, appWithTranslation, withTranslation, useTranslation, Router, Link, Trans} = nextI18Next;
33

34 35
// from ~/node_modules/next/types/index.d.ts
// https://gitlab.com/kachkaev/website-frontend/-/blob/master/src/i18n.ts#L64-68
36
// eslint-disable-next-line @typescript-eslint/ban-types
37 38 39 40 41
export type NextI18NextPage<P = {}, IP = P> = NextComponentType<
    NextPageContext,
    IP & {namespacesRequired: string[]},
    P & {namespacesRequired: string[]}
>;