Skip to content

Instantly share code, notes, and snippets.

@michaeldever
Last active June 1, 2022 21:57
Show Gist options
  • Save michaeldever/1ecc26353155bc961ee28f250397d4d5 to your computer and use it in GitHub Desktop.
Save michaeldever/1ecc26353155bc961ee28f250397d4d5 to your computer and use it in GitHub Desktop.
const path = require( "path" );
module.exports = {
i18n: {
defaultLocale: "en",
locales: ["en"],
localePath: path.resolve('./public/locales')
},
reloadOnPrerender: process.env.NODE_ENV === "development",
};
import i18next from "i18next";
import { i18n as nexti18next } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
export class TranslationService {
static translate = async (
key: string,
ns: string,
lng: string,
args: { [s: string]: string } = {}
) => {
const i18n = !!nexti18next ? nexti18next : await this.create(lng, ns);
return i18n.t(key, {
ns,
lng,
...args,
});
};
static t = this.translate;
private static create = async (lng: string, ns: string) => {
const { _nextI18Next } = await serverSideTranslations(lng, [ns]);
const _i18n = i18next.createInstance();
_i18n.init({
lng,
resources: _nextI18Next.initialI18nStore,
fallbackLng: _nextI18Next.userConfig?.i18n.defaultLocale,
});
return _i18n;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment