react-translate-maker
React translation module. Internationalize your great project. This library is part of translate-maker.
Installation
Install via npm.
Versions
If you are using react >= 15.4.0 use version >= 0.3.0 otherwise use version < 0.3.0
Features
- Build on standards (ICU Message syntax, Unicode CLDR), ES6 and Promises
- Support for 190+ languages
- Runs in the browser and Node.js
- JSON Structure
- Nested and reference translations
- Variables
- Conditioned translations (Plural, Gender etc...)
- Filters capitalize, upperCase, lowerCase etc... and custom filters
- Default translations
- Integrates with React and Angular
- Automatic extraction of translations from your project
- For more details and examples click on translate-maker
Usage
Basic example
The result will be
File adapter
If you want to load localisation files automatically with the require function you can use File adapter.
File structure
project
│ component.jsx
└──locales
│ en_US.js
en_US.js
component.jsx
The result will be
File adapter and webpack
If you want to use webpack with file adapter you need to use own function getFile. You need to use getFile instead of path because you need to change the webpack context.
component.jsx
Placeholder and direct translations
The result will be
Direct translations
The result will be
Namespaces
Sometimes when you are using dot notation you can stack with long paths. For example: header.navigation.button.login. You can use component named Namespace which will help you to simplify your jsx file.
Namespace with compose component
HTML content
Sometimes you need to provide HTML content.
The result will be
Locale switch
We are providing a component for the locale switch. It is a select with everything what do you need.
You can use it in two ways. Here is first example.
As you can see in previous example. We are using onChange event. The main reason for that is that LocaleProvider is a controlled component. That means if you wanted to change property named locale of the LocaleProvider you need to change it directly in the render function.
If you want to use LocaleProvider as uncontrolled component you can do that. But all properties of the LocaleProvider will be used only as initialisation properties. Here is a second example (please take a loon on the property named controlled of the LocaleProvider).
The main difference is that you are not able to change locale of the LocaleProvider with property named locale after first render.
Properties of the LocaleSwitch
- onChange (function): Callback witch a new locale (immediate change)
- onLocaleChange (function): Callback witch a new locale (after success change)
- setLocale (Boolean): It will set locale automatically in the translate-maker after change (default: true)
- locales (array): Array of the available locales. [{ label: 'English', value: 'en_US' }]
- all properties of the standard select component
Filters
Sometimes you need to provide HTML content.
The result will be
Options of Locale Provider
- locale (String): Current locale ID (default null)
- locales (Array): List of available locales IDs (default null)
- cache (Instance of Cache): Cache of the translations (default MemoryCache)
- adapter (Instance of Adapter | data): Adapter provides translations (default see option defaultAdapter)
- defaultAdapter (Class of Adapter): Default adapter (default MemoryAdapter)
- dotNotation (Boolean): You can turn of dot notation. This is useful if you are using PO translation files (default true)
- mode (Enum): You can use full compatible ICU version with Mode.ICU. After that you can use external variables witout dolar character (Default Mode.MAIN)
- references (Boolean): You can turn on/off references. (Default true)
- variables (Boolean): You can turn on/off variables. (Default true)
- combinations (Boolean): You can turn on/off combinations. (Default true)
- defaultValue (Function): What you will see for missing translations (Default (path, attrs) =>
Missing default translation for: ${path}
) - filters (Object): Object with custom filters
- controlled (Boolean): You can set component as uncontrolled (default true). More information.
More examples
Please take a look on translate-maker
Roadmap
- [ ] Locales property of the LocaleSwitch can be an object
- [ ] Locales property of the LocaleSwitch can be autopopulated from the adapter
- [ ] Locales property of the LocaleSwitch can be translated automatically by translate-maker
Running Tests
To run the test suite, first invoke the following command within the repo, installing the development dependencies:
Then run the tests: