Skip to content

Instantly share code, notes, and snippets.

@GarryOne
Created March 24, 2022 12:00
Show Gist options
  • Save GarryOne/79b9d8c728c70e3ac4836cbf4ff69841 to your computer and use it in GitHub Desktop.
Save GarryOne/79b9d8c728c70e3ac4836cbf4ff69841 to your computer and use it in GitHub Desktop.
Custom implementation of useTranslation() from i18next
import Cookies from 'js-cookie';
import enTrans from '@public/locales/en';
import deTrans from '@public/locales/de';
import esTrans from '@public/locales/es';
import { DEFAULT_LOCALE } from 'src/global/constants';
const useCustomTranslation = () => {
const translationsByLocale = {
en: enTrans,
de: deTrans,
es: esTrans
}
const locale = Cookies.get('NEXT_LOCALE') || DEFAULT_LOCALE;
const t = (key: string) => {
const [namespace, index] = key.split(':');
const trans = translationsByLocale[locale];
return trans[namespace] && trans[namespace][index];
}
return { t };
}
export default useCustomTranslation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment