Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this required reload page to load new language state? #245

Open
ghost opened this issue Apr 22, 2022 · 5 comments
Open

Is this required reload page to load new language state? #245

ghost opened this issue Apr 22, 2022 · 5 comments
Labels

Comments

@ghost
Copy link

ghost commented Apr 22, 2022

Same the title, Is it required reload page to load new language? I tried don't reload but the language will not show new. Please check

@e-tsakhlo
Copy link

There is no need in reloading page.

Make sure you used runtime translations:
https://ttag.js.org/docs/create-react-app.html#runtime-translations

@huykon
Copy link

huykon commented Sep 6, 2022

@e-tsakhlo
Copy link

@e-tsakhlo I think you haven't check code yet. https://github.com/ttag-org/CRA-runtime-example/blob/master/app/src/App.js#L11

This example truly uses page reload, but overoll there is no need in it.

You can import all translations (use async import if you wish performance boost), add all locales (addLocale) and then run useLocale(locale) whenever you need to change one.

Here is an example from my project using contexts:

LocaleContextProvider

import { FC, ReactNode, useEffect, useMemo, useState } from 'react';
import { addLocale, useLocale } from 'ttag';

import { Locale } from 'types';
import { DEFAULT_LOCALE, LOCALE_STORAGE_KEYS } from 'utils/constants';
import { LocaleContext } from 'components/app/LocaleContext';

import noLocaleTranslationsObject from 'i18n/no.po.json';

const getCurrentLocale = (): Locale => {
  const localStorageLocale = localStorage.getItem(LOCALE_STORAGE_KEYS.locale);

  if (localStorageLocale) return localStorageLocale;
  return DEFAULT_LOCALE;
};

const setCurrentLocale = (locale: Locale): void => {
  localStorage.setItem(
    LOCALE_STORAGE_KEYS.locale,
    locale
  );
};

const loadLocales = () => {
  addLocale('no', noLocaleTranslationsObject);
};

export interface LocaleContextProviderProps {
  children?: ReactNode;
}

export const LocaleContextProvider: FC<LocaleContextProviderProps> = ({
  children,
}) => {
  const [locale, setLocale] = useState<Locale>(() => getCurrentLocale());

  useEffect(() => {
    loadLocales();
  }, []);

  useEffect(() => {
    setCurrentLocale(locale);
    useLocale(locale);
  }, [locale]);

  const value = useMemo(
    () => ({
      locale,
      setLocale,
    }),
    [locale, setLocale]
  );

  return (
    <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>
  );
};

LocaleContext

import { createContext } from 'react';

import { Locale } from 'types';

export type LocaleContextValue = {
  locale: Locale;
  setLocale: (locale: Locale) => void;
};

export const LocaleContext = createContext<LocaleContextValue>({
  locale: 'en',
  setLocale: () => {},
});

types.d.ts

export type Locale = 'en' | 'no';

@AlexMost
Copy link
Member

Hey @e-tsakhlo, thanks for a good example! We are using page reload for our projects and it works ok for us. Looks like we should add some documentation about how it should work without page reload.

@AlexMost AlexMost added the doc label Oct 28, 2023
@dasbego
Copy link

dasbego commented Mar 13, 2024

I just think the https://ttag.js.org/docs/library-api.html#uselocale section can be better explained, but it's all there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants