You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I found your library quite useful, thank you for this.
I see a problem with my code that I have to write context.t("KEY_TO_TRANSLATE"); everywhere in my components wherever I require labels to be replaced based on locale.
So I thought of an approach to writing this context.t("KEY_TO_TRANSLATE"); to a separate file and using it as a function. Now I know that I have to give the context to this file separately. For this, I am using the App context's render method to populate the context in the file that I have written.
Below is my code.
`let globalContext;
export function setContext(context){
globalContext = context;
}
export function translate(key){
console.log(globalContext);
return globalContext.t(key);
}`
Hi, I found your library quite useful, thank you for this.
I see a problem with my code that I have to write context.t("KEY_TO_TRANSLATE"); everywhere in my components wherever I require labels to be replaced based on locale.
So I thought of an approach to writing this context.t("KEY_TO_TRANSLATE"); to a separate file and using it as a function. Now I know that I have to give the context to this file separately. For this, I am using the App context's render method to populate the context in the file that I have written.
Below is my code.
`let globalContext;
export function setContext(context){
globalContext = context;
}
export function translate(key){
console.log(globalContext);
return globalContext.t(key);
}`
and below is the other file:
`class App extends Component {
componentWillMount() {
//SOme code
}
constructor(props,context) {
super();
console.log("Consturctor",context);
this.state = {
appContext : context
}
}
componentDidMount() {
console.log("componentDidMount",this.state.appContext);
}
render() {
if(this.state.appContext) {
console.log("1",this.state.appContext);
setContext(this.state.appContext);
}
return (
)
}
}
App.contextTypes = {
t: PropTypes.func.isRequired
}
export default App;`
Is this possible to achieve? This way I can extract out the code for translation from all of the components and keep it at one place.
The text was updated successfully, but these errors were encountered: