Quickly tweak react-admin and replace internationalization module polyglot with react-intl. It can work thanks to your resolve aliases babel or webpack configuration.
$ npm install @yeutech-lab/react-admin-intl --save-dev
Swap polyglot with our react-intl adapter
This will replace the internationalization of react-admin (It use polyglot). Instead, it will use react-intl Because of that, default messages provided by react-admin won't be compatible anymore. This is why you must use this package to import the react-intl version of messages:
$ npm install @yeutech-lab/ra-language-intl --save
Visit ra-language-intl documentation to view available or add locale(s) translation(s).
webpackConfig
object Your application webpack configuration
const swapPolyglotWithReactIntl = require('@yeutech-lab/react-admin-intl/lib/swapPolyglotWithReactIntl');
// later in your application when you create your webpack configuration
webpackConfig = swapPolyglotWithReactIntl(webpackConfig);
Returns object webpackConfig - The edited webpack config
Create a i18nProvider used for retrieving translation messages.
The i18nProvider is a function that accept a locale
as a parameter, it return:
- a messages
object
directly for the defaultlocale
- an unresolved
Promise
that is fetching the message object for thelocale
This permit to do application code splitting on language, and load new language only if the user change locale.
Ideally, you create a i18n folder in the src
folder of your project and had all the languages here.
src/i18n
├── de.js
├── en.js
├── fr.js
├── i18nProvider.js
├── index.js
├── translation
│ ├── de.json
│ ├── en.json
│ ├── fr.json
│ └── vi.json
└── vi.js
The i18n/index.js
file export the default application locale and the list of applications locales
options
Object createI18nProvider optionsdefault
string options.locale - Default application locale ISO3166 alpha 2 (See: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) (optional, defaulten
)default
string options.locales - List of application locales available ISO3166 (optional, default['en']
)default
(function | object) options.defaultMessages - A function or an object which return messages fromrequire(locale)
(optional, default{}
)default
Array<Promise> options.deferredMessagesList - A map fromlocales
, return a function that return messages fromimport(locale)
(optional, default[]
)
import createI18nProvider from '@yeutech-lab/react-admin-intl';
import { appLocales as locales, DEFAULT_LOCALE as locale } from './index';
// this will create the i18nProvider
const { i18nProvider, messages } = createI18nProvider({
locale,
locales,
// this must be a function, otherwise webpack does not support dynamic import
defaultMessages: () => require(`./${locale}`),
deferredMessagesList: locales.map((l) => () => import(`./${l}`))
});
Returns i18nProvider
Read react-admin-intl documentation.
If you want to contribute to react-admin-intl please see our contributing and community guidelines, they'll help you get set up locally and explain the whole process.
Please also note that all repositories under the yeutech-lab organization follow our Code of Conduct, make sure to review and follow it.
Copyright 2019 Yeutech Company Limited
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.