Comments (7)
@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.
@FallenMax Simple demo:https://github.com/zitanlin/a18n-test.git
I have two problems as follow:
- switch language in useEffect
- Dynamically switch languages in the page
from a18n.
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.
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);
};
`
-
setLocale in useEffect function not working. I setLocale('zh') in useEffect, but not working. for example:
-
it does not switch to expected language at first call changeLanguage function, then it works. You can test it.
from a18n.
from a18n.
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.
@FallenMax Got it, thank you very much!
from a18n.
Related Issues (10)
- Better handling of JSX text? HOT 2
- [CLI] Add `check` command? HOT 1
- Investigate .vue support HOT 1
- Share locale resource between a18n instances via specifying the same namespace
- Don't move a18n import statement to top of file
- Support wrap/extract files from arguments and stdin
- Add extract option to NOT prune unused keys
- Create a bundle for browser
- issue with vscode HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from a18n.