Coder Social home page Coder Social logo

Comments (7)

FallenMax avatar FallenMax commented on June 13, 2024

@zitanlin Hi, can you provide a minimal reproducible repo?

Please note that a18n.setLocale() & a18n.addLocaleResource() only change output of a18n calls like a18n("someText") and does nothing else. In order for new language/translation take effect, you need ensure your app is re-rendered using texts newly returned by a18n("someText").

For static text (i.e. not re-generated between render), they might be bound to stale translations. See https://github.com/FallenMax/a18n#1-why-is-it-important-to-load-translation-resources-and-specify-a-locale-before--all-other-code-is-run for detail. In these situations, you'll have to window.reload() and set correct language before app code execute.

from a18n.

zitanlin avatar zitanlin commented on June 13, 2024

@FallenMax Simple demo:https://github.com/zitanlin/a18n-test.git

I have two problems as follow:

  1. switch language in useEffect
  2. Dynamically switch languages ​​in the page

from a18n.

FallenMax avatar FallenMax commented on June 13, 2024

https://github.com/zitanlin/a18n-test/blob/master/src/App.js#L15-L17

  const changeLanguage = async () => {
    const temp = language === 'zh' ? 'en' : 'zh';
    setLanguage(temp);
    await a18n.setLocale(temp); // moving this line up fixes the problem
    await a18n.setLocale(temp);  // this line is unnecessary, also `await` is not required
  };

@zitanlin Hi, the problem is that setLanguage is called before a18n.setLocale(temp), this means that react could re-renders this component BEFORE a18n is set to use new language.
As mentioned in comment, moving a18n.setLocale(temp) before setLanguage fixes this problem (await is not required and should be removed).

Hope this helps!

from a18n.

zitanlin avatar zitanlin commented on June 13, 2024

https://github.com/zitanlin/a18n-test.git

`const [language, setLanguage] = useState('zh');
useEffect(async () => {
console.log('---- use effect = ', language)
a18n.setLocale('zh');
}, []);

const changeLanguage = async () => {
console.log('--- language = ', language);
const temp = language === 'zh' ? 'en' : 'zh';
a18n.setLocale(temp);
setLanguage(temp);
};
`

  1. setLocale in useEffect function not working. I setLocale('zh') in useEffect, but not working. for example:
    image

  2. it does not switch to expected language at first call changeLanguage function, then it works. You can test it.

from a18n.

zitanlin avatar zitanlin commented on June 13, 2024

@FallenMax

from a18n.

FallenMax avatar FallenMax commented on June 13, 2024

@zitanlin

a18n is a general i18n library, and it does not know about React or any other UI framework. This means a18n.setLocale() alone won't do anything about your app UI. As mentioned in previous comments, you need to trigger app re-render somehow after a18n.setLocale(), so that latest language can be applied.

In your case, you should call setLanguage after a18n.setLocale().

from a18n.

zitanlin avatar zitanlin commented on June 13, 2024

@FallenMax Got it, thank you very much!

from a18n.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.