react-i18next#UseTranslationOptions TypeScript Examples

The following examples show how to use react-i18next#UseTranslationOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useI18next.ts    From gatsby-plugin-react-i18next with MIT License 5 votes vote down vote up
useI18next = (ns?: Namespace, options?: UseTranslationOptions) => {
  const {i18n, t, ready} = useTranslation(ns, options);
  const context = useContext(I18nextContext);

  const {routed, defaultLanguage, generateDefaultLanguagePage} = context;

  const getLanguagePath = (language: string) => {
    return generateDefaultLanguagePage || language !== defaultLanguage ? `/${language}` : '';
  };

  const removePrefix = (pathname: string) => {
    const base = typeof __BASE_PATH__ !== `undefined` ? __BASE_PATH__ : __PATH_PREFIX__;
    if (base && pathname.indexOf(base) === 0) {
      pathname = pathname.slice(base.length);
    }
    return pathname;
  };

  const removeLocalePart = (pathname: string) => {
    if (!routed) return pathname;
    const i = pathname.indexOf(`/`, 1);
    return pathname.substring(i);
  };

  const navigate = (to: string, options?: NavigateOptions<{}>) => {
    const languagePath = getLanguagePath(context.language);
    const link = routed ? `${languagePath}${to}` : `${to}`;
    return gatsbyNavigate(link, options);
  };

  const changeLanguage = (language: string, to?: string, options?: NavigateOptions<{}>) => {
    const languagePath = getLanguagePath(language);
    const pathname = to || removeLocalePart(removePrefix(window.location.pathname));
    const link = `${languagePath}${pathname}${window.location.search}`;
    localStorage.setItem(LANGUAGE_KEY, language);
    return gatsbyNavigate(link, options);
  };

  return {
    ...context,
    i18n,
    t,
    ready,
    navigate,
    changeLanguage
  };
}