React translate

Subscribe to RSS

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. My question is not purely technical, but rather about the architecture, and the patterns that people are actually using in production to solve this problem. This means that each component have for example a set of en. And a helper function to help reading the values from those depending on the selected language. So they are not aware of the current language, they just take a list of strings as props which happen to match the current language. You bypass the props a bit and possibly use the context thingy to pass down the current language. After trying quite a few solutions, I think I found one that works well and should be an idiomatic solution for React 0. The only thing your component would need by conventionis a strings props. It should be an object containing the various strings your Component needs, but really the shape of it is up to you. It does contain the default translations, so you can use the component somewhere else without the need to provide any translation it would work out of the box with the default language, english in this example. On the previous snippet, you might have noticed this on the last line: translate 'MyComponent' MyComponent. The first argument is a key that will be used to lookup the translations in the translation file I used the name of the component here, but it could be anything. The second one notice that the function is curryed, to allow ES7 decorators is the Component itself to wrap. It's not magic: it will just read the current language from the context and that context doesn't bleed all over the code base, just used here in this wrapperand then get the relevant strings object from loaded files. The important piece is that it takes the current language from the context and convert that into strings, given the key provided. On the root component, you just need to set the current language from your current state. I think is solves all the problem I was trying to avoid in my question: the translation logic doesn't bleed all over the source code, it is quite isolated and allows reusing the components without it. For example, MyComponent doesn't need to be wrapped by translate and could be separate, allowing it's reuse by anyone else wishing to provide the strings by their own mean. Since quite a lot of people asked for a real-life example in the comments, here it is:. From my experience the best approach is to create an i18n redux state and use it, for many reasons:. Currently, there are many frameworks implement the same concept using react context API without reduxI personally recommended I18next. I'm one of the authors. From my research into this there appears to be two main approaches being used to i18n in JavaScript, ICU and gettext. What amazes me is how poor the support is. You get the familiarity of sprintf for formatting strings and PO files will be translated easily by thousands of different agencies. Jed doesn't. However Jed seems like a more gettext focussed implementation - that is it's expressed intention, where as i18next just has it as an option. This has more support for the edge cases around translations, e. I think you will see the benefits from this if you have more complex languages to translate into. A popular option for this is messageformat. Discussed briefly in this sentry. He makes quite stong claims for using ICU :. Jed is feature complete in my opinion. I am happy to fix bugs, but generally am not interested in adding more to the library. I also maintain messageformat.

Subscribe to RSS


GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. It allows switching locales without a page reload. Here is a quick-start tutorial to get you up and running with Translate. It's a step-by-step guide on how to build a simple app that uses the Translate component from scratch. We assume you have recent versions of Node. The reactcounterpart and react-interpolate-component packages are peer dependencies of react-translate-component and need to be installed along-side of it. We will put our application logic into client. Open the file in your favorite editor and add the following lines:. Next, we will create a LocaleSwitcher component which will be used to, well, switch locales. Here is the code to append to client. Whenever the user selects a different locale from the drop-down, we correspondingly set the new drop-down's value as locale in the Counterpart library, which in turn triggers an event that our soon to be integrated Translate component listens to. As initially active value for the select element we specify Counterpart's current locale "en" by default. Next, we create a Greeter component that is going to display a localized message which will greet you:. In the component's render function, we simply transfer all incoming props to Translate the component this repo is all about. As content property we specify the string "example. Now add the new Greeter component to the body element, provide a with prop holding the interpolations your first name in this case and a component prop which is set to "h1":. The value of the name key will be interpolated into the translation result. All that's left to do is to add the actual translations. You do so by calling the registerTranslations function of Counterpart. Add this to client. In the translations above we defined placeholders in sprintf's named arguments syntax which will be interpolated with the value of the name key we gave to the Greeter component via the with prop. That's it for the application logic.

Translation


It allows switching locales without a page reload. Here is a quick-start tutorial to get you up and running with Translate. It's a step-by-step guide on how to build a simple app that uses the Translate component from scratch. We assume you have recent versions of Node. The reactcounterpart and react-interpolate-component packages are peer dependencies of react-translate-component and need to be installed along-side of it. We will put our application logic into client. Open the file in your favorite editor and add the following lines:. Next, we will create a LocaleSwitcher component which will be used to, well, switch locales. Here is the code to append to client. Whenever the user selects a different locale from the drop-down, we correspondingly set the new drop-down's value as locale in the Counterpart library, which in turn triggers an event that our soon to be integrated Translate component listens to. As initially active value for the select element we specify Counterpart's current locale "en" by default. Next, we create a Greeter component that is going to display a localized message which will greet you:. In the component's render function, we simply transfer all incoming props to Translate the component this repo is all about. As content property we specify the string "example. Now add the new Greeter component to the body element, provide a with prop holding the interpolations your first name in this case and a component prop which is set to "h1":. The value of the name key will be interpolated into the translation result. All that's left to do is to add the actual translations. You do so by calling the registerTranslations function of Counterpart. Add this to client. In the translations above we defined placeholders in sprintf's named arguments syntax which will be interpolated with the value of the name key we gave to the Greeter component via the with prop. That's it for the application logic. To eventually see this working in a browser, we need to create the server-side code that will be executed by Node. Note that you shouldn't use this code in production as the bundle.

Transforms


By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. CSS styles need to be passes as an object to the component. However, if you are trying to use transform styles you will get an error. My solution was to first concatenate the string and then pass it to the object. Notice the use of 'px' here. How are we doing? Please help us improve Stack Overflow. Take our short survey. Learn more. Asked 4 years, 6 months ago. Active 3 years, 5 months ago. Viewed 41k times. Guy Laor Guy Laor 1 1 gold badge 2 2 silver badges 7 7 bronze badges. Could you add a code example and error message? Active Oldest Votes. Translate also wasn't working for me. I fixed it by adding 'px' behind the var. Not awesome that React does not emit an error on this, and it just fails silently. Thanks for the correct answer. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Cryptocurrency-Based Life Forms. Q2 Community Roadmap. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Triage needs to be fixed urgently, and users need to be notified upon….

I18next is an internationalization-framework written in and for JavaScript. But it's much more than that. It provides you with a complete solution to localize your product from web to mobile and desktop. But this is not where it ends. You can also use i18next with Node. Most frameworks leave it to you how translations are being loaded. You are responsible to detect the user language, to load the translations and push them into the framework. We provide you with plugins to:. Use moment. Prefer different pre- and suffixes for interpolation? Like gettext style keys better? The framework was built with scalability in mind. For smaller projects, having a single file with all the translation might work, but for larger projects this approach quickly breaks down. Through locize. Comparison to others. Getting started. Configuration Options. Supported Frameworks. Plugins and Utils. For Enterprises. Translation Function. Objects and Arrays. Translation Resolution. How to. Add or Load Translations. JSON Format. Creating own Plugins. Migration Guide. Powered by GitBook. Complete solution.

React Translations



Comments on “React translate

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>