Skip to content

Instantly share code, notes, and snippets.

@GarryOne
Created March 24, 2022 12:00
Show Gist options
  • Select an option

  • Save GarryOne/79b9d8c728c70e3ac4836cbf4ff69841 to your computer and use it in GitHub Desktop.

Select an option

Save GarryOne/79b9d8c728c70e3ac4836cbf4ff69841 to your computer and use it in GitHub Desktop.

Revisions

  1. GarryOne created this gist Mar 24, 2022.
    24 changes: 24 additions & 0 deletions useTranslation-custom-implementation.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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;