Comments (12)
I need to run it inside useEffect as I dont know what language to set it to in the initial load ... I get the fixed language from the server and then set it.
from react-i18next.
your fr
language is not loaded therefore it shows fallback...(you have to assert it's loaded...)
add preload: ["fr"],
to your init to preload it...https://www.i18next.com/overview/configuration-options#languages-namespaces-resources
from react-i18next.
Thanks @jamuhl Preloading in the init is not really convenient as I would have to preload all possible languages. My app is a client side app and will use two possible languages... one that comes from outside the users browser and the browser language. I cannot know before the app loads either of the two languages.
Is there a way to manually load a language? Or is my current work around (changing the language and back) the best way to getFixedT of an un-preloaded language. Cheers
from react-i18next.
https://www.i18next.com/overview/api#loadlanguages
and I guess you can use https://www.i18next.com/overview/api#hasresourcebundle to check for loaded (eg. building your own useTranslation for the different language)
or better use: #1637 which (@adrai) is currently not documented
from react-i18next.
fyi: the lng prop via useTranslation hook will not load the additional language, it will only return the "appropriate" t function bound to that language
from react-i18next.
@adrai @jamuhl
This seems to work. Is this what you meant?
if (!i18n.hasResourceBundle(lng, "translation")){
i18n.loadLanguages(lng, (err: unknown) => {
if (err instanceof Error) {
console.error("Error loading language: " + err.message)
} else {
const newDocT = i18n.getFixedT(lng)
//.... use newDocT
}
})
} else {
const newDocT = i18n.getFixedT(lng)
//.... use newDocT
}
The way I use it is by setting it to a state variable:
const [ myFixtT, setMyFixedT] = React.useState<TFunction | undefined>()
setMyFixedT(() => newDocT)
from react-i18next.
Did the useTranslation hook not work?
const { t } = useTranslation('trandlation', { lng })
from react-i18next.
Not sure I understand
fyi: the lng prop via useTranslation hook will not load the additional language, it will only return the "appropriate" t function >bound to that language
You said it would not load the language. I dont want to preload in the init. I just want a TFunction I can cache. This language is fixed and specified by external data, not by the user or the browser.
from react-i18next.
Seems I was wrong... it loads the language... can you try that?
from react-i18next.
I need to do this inside a React.useEffect... useTranslate is throwing an error because it is a Hook inside a Hook... Well that is what I think is happening.
from react-i18next.
useTranslation is a hook...no need to run it inside useEffect
from react-i18next.
I moved it into its own empty div rendering component so it could use the language as a property. useTranslation works there.
Thanks.
from react-i18next.
Related Issues (20)
- `Trans` component should NOT modify the input props value. HOT 1
- Less than sign `<` breaks tag parsing with Trans component HOT 1
- 18n.exists() not working properly? HOT 6
- google play console rejected due to java error HOT 8
- i18n.changeLanguage dont work if we setup our app using SSR razzle example in react-i18next HOT 5
- Client translation in NextJS 14 breaks React hooks rules HOT 3
- Generated types not working with unescape "- " syntax HOT 1
- Typescript error: Context is not assignable to type 'string' HOT 5
- Not existing context not detected as type error if covered by string union HOT 2
- TS Context error when using json for storing translations HOT 1
- Trans component modifies passed tOptions and overrides "context" property from the object HOT 1
- Proposal: useLanguage hook HOT 2
- Anchor tag not working on Trans component HOT 3
- [Bug][IOS]: Error - Multiple commands produce React-Core-3e4f9a72-RCTI18nStrings | React-Core.common-RCTI18nStrings HOT 2
- Trans component using context has unexpected type errors HOT 2
- No React update when updating resource bundle in i18next HOT 1
- Text content did not match & Error while hydrating HOT 3
- I18 doesnt work inside object HOT 3
- Number vars passed as Trans children are not processed correctly 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 react-i18next.