-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Translation outside React Component #909
Comments
Outside of react components just use the i18next instance: import i18n from `./i18n`
i18n.t(...); Just be aware of the normal catch you's:
|
Yep, I did and I described an error above. This is my istance:
I don't know whats going on. I use a provider in index to encapsulate i18n in DOM tree. |
Somewhere you're using a hook inside a class component...that's what the warning says... |
Correct, I missed single useTranslation in another pure js file. When I removed that now everything is working as well, including translations. Thank you so much. |
I've got the issue, what you mentioned. The language doesn't want to change. Of course I checked docs and link sent by you. But, nothing works, I try to fire this on a change language event, a then add additional namespace. I noticed that, when I set defaultNs to correct value, everything is change (class and functional React components) to a proper state, but only the outside React files don't respond. |
outside react files...what should trigger there a retranslation? once translated static content it's done...there is nothing like the render tree which gets rerendered and doing a fresh translation ---> use the events for that...https://www.i18next.com/overview/api#onlanguagechanged |
I use it, but should I wrap this translations in a function? Code below:
Still nothing. |
sorry guess i don't get what you like to do...why you need to change the namespace? makes no sense to me...what is the use case? do you have same keys on different namespaces and different values? |
and why set namespace to de i think you do something very wrong...above in code you have in resources the namespace translation now you like to access "de" namespace...!??!?? |
for change language just call i18n.changeLanguage(lng) |
I try to force this t elements, imported from an instance to change itself. |
make a codesandbox sample of what you like to do / have error with...i can't understand what you like to do, what is wrong by your text or at least paste relevant code here |
The structure is too complicated to put it on code sandbox, so I try to reproduce that issue. fileOutsideReact.js :
This example is simplified. In this case I can overrride this values in the function. But in real project this file exports a tons of values, and redundant would be painfully.
Is inside React component When I change the language strings 1 and 2, still are in prev language. PS: Dont care about namespaces, I just tried anything to force translations to update. |
you have to call t function again... exported values will not change magically... i would suggest to redesign it... simply use i18next.t() where you need it... |
how does a string magically changes? - it does not --- right? So this might work? import { i18n } from './i18n';
const myExport = {};
function fill() {
myExport.foo = i18n.t('foo');
}
// run it initial
fill();
// bind some events and fill values again (doing the magic you expect to happen magically)
i18n.on('languageChanged init',() => {
fill(); // fills myExport with new value for foo based on current i18n lng
});
// export the const
export default myExport; |
Problem is solved. For others if they are looking for solution in similar case. I loop through object, and override a value, where is i18n translation function. Thanks for advice. |
@jamuhl Your implementation in not working on my end, but I don't have any errors. Maybe it's because of loading times. Could you explain the bit about namespaces and how would that help me? |
Can you create a sample and/or enable debug: true ? |
Oh my god. I'm an idiot. It works perfectly. Thanks! |
@jamuhl Thanks a lot for the solution. |
import i18n from `./i18n`
i18n.t(...); This is working, but I have a problem with Thanks! |
If you're calling the t function from outside a react component, you should call the t function from i18next instead. Also if the default namespace is different than translation, you can either change the default namespace, or pass the namespace as a prefix to the t function, like this:
|
I18n.t() function is working only inside in functions. There is a way to use them in variables. |
People are talking about instances but are just importing i18n via |
@dominictobias the instance is the same...important is that your i18n.js file (or where ever you init that instance) is imported and included into your build. re-exporting helps most to not forget it ;) different situation if you create multiple instances - but that's another topic |
@jamuhl Could you please expand on this? I'm in a similar situation, in which I have a React app where most translations are done inside components, and |
@bildungsroman I guess inside your The reason for this (I guess) is that those files get imported earlier or too early. #1236 (comment) might help for using i18n.t outside of react functions (and react to language changes, ....) |
do you get this instance from the |
i18next.use(initReactI18next).init()
export default i18next |
ah, okay. I thought I need to get the instance from the |
Hey i have issue calling
this one has not been translated successfully on the page, however if i convert it into a function like this: import i18n from '../i18n'; export const sec1 = () => ({ is working, any idea how i can make it work with plain object? PS: I have added:
and i can see Yes, can confirm contants.ts file is executed before i18n to be initialized, wrapped app in Suspense but again not working, any ideas? |
@AndonMitev why not search in issues: #1698 (comment) |
I created an intermediary function like this:
|
@dev-itgrapes just keep in mind...on language change your config.title gets not updated and will provide the value it had on time calling that t function...#1236 (comment) for the case you need that |
import React, { useState, ReactElement } from "react"; interface RowData { const HeaderActionsContainer = styled(Box) const initialRows: GridRowsProp = [ export const CollectionSiteTable: React.FC = ({ const { t } = useTranslation(); const handleRowEditStop: GridEventListener<"rowEditStop"> = (params, event) => { return ( export default CollectionSiteTable; const COLUMNS = [ i18next.init({ the translations outside a component inside the columns object ns file name isnt accessible for this any one has solution for this? |
Export the key as it is from the static file from the ts file and useTranslation to translate the key in the tsx file. Example en.json plans.ts export const plans = [ subscriptions.tsx const { t } = useTranslation(); return
{plans.map((plan, index) => (
{t(plan.short_description)}
))}
|
I tried to translate strings exported outside React Components.
But any methods in docs doesn't work, and I guess its normal, because its typically React methods.
I've found similar topic, and there it is solution about importing instance.
But, when I do it I've had an error:
Probably it because of this line in the instance:
When I delete this line, the error is gone, but the translations are not included.
How can I plug this simple .js file to React translations?
The text was updated successfully, but these errors were encountered: