Skip to content
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

Not able to call the t function from outside utility. #95

Open
pathfinder2104 opened this issue Dec 4, 2018 · 0 comments
Open

Not able to call the t function from outside utility. #95

pathfinder2104 opened this issue Dec 4, 2018 · 0 comments

Comments

@pathfinder2104
Copy link

pathfinder2104 commented Dec 4, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant