Polyglot i18n provider for react-admin
Polyglot i18n provider for react-admin, the frontend framework for building admin applications on top of REST/GraphQL services. It relies on polyglot.js, which uses JSON files for translations.
Installation
npm install --save ra-i18n-polyglot
Usage
Wrap the function exported by this package around a function returning translation messages based on a locale to produce a valid i18nProvider
.
import * as React from "react";
import { Admin, Resource } from 'react-admin';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';
import frenchMessages from 'ra-language-french';
const messages = {
fr: frenchMessages,
en: englishMessages,
};
const i18nProvider = polyglotI18nProvider(locale => messages[locale]);
const App = () => (
<Admin locale="en" i18nProvider={i18nProvider}>
...
</Admin>
);
export default App;
Translation Messages
The message
returned by the function argument should be a dictionary where the keys identify interface components, and values are the translated string. This dictionary is a simple JavaScript object looking like the following:
{
ra: {
action: {
delete: 'Delete',
show: 'Show',
list: 'List',
save: 'Save',
create: 'Create',
edit: 'Edit',
cancel: 'Cancel',
},
...
},
}
All core translations are in the ra
namespace, in order to prevent collisions with your own custom translations. The root key used at runtime is determined by the value of the locale
prop.
The default messages are available here.
Asynchronous Locale Change
The function passed as parameter of polyglotI18nProvider
can return a Promise for messages instead of a messages object. This lets you lazy load messages upon language change.
Note that the messages for the default locale (used by react-admin for the initial render) must be returned in a synchronous way.
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';
const asyncMessages = {
fr: () => import('ra-language-french').then(messages => messages.default),
it: () => import('ra-language-italian').then(messages => messages.default),
};
const messagesResolver = locale => {
if (locale === 'en') {
return englishMessages;
}
return asyncMessages[params.locale]();
}
const i18nProvider = polyglotI18nProvider(messagesResolver);
Using Specific Polyglot Features
Polyglot.js is a fantastic library: in addition to being small, fully maintained, and totally framework agnostic, it provides some nice features such as interpolation and pluralization, that you can use in react-admin.
const messages = {
'hello_name': 'Hello, %{name}',
'count_beer': 'One beer |||| %{smart_count} beers',
};
translate('hello_name', { name: 'John Doe' });
=> 'Hello, John Doe.'
translate('count_beer', { smart_count: 1 });
=> 'One beer'
translate('count_beer', { smart_count: 2 });
=> '2 beers'
translate('not_yet_translated', { _: 'Default translation' });
=> 'Default translation'
To find more detailed examples, please refer to http://airbnb.io/polyglot.js/
v3.11.0
Starting with this version, react-admin applications send an anonymous request on mount to a telemetry server operated by marmelab. You can see this request by looking at the Network tab of your browser DevTools:
https://react-admin-telemetry.marmelab.com/react-admin-telemetry
The only data sent to the telemetry server is the admin domain (e.g. "example.com") - no personal data is ever sent, and no cookie is included in the response. The react-admin team uses these domains to track the usage of the framework.
You can opt out of telemetry by simply adding disableTelemetry
to the <Admin>
component:
// in src/App.js
import * as React from "react";
import { Admin } from 'react-admin';
const App = () => (
<Admin disableTelemetry>
// ...
</Admin>
);
- Add domain telemetry on app mount (5631) (djhi)
- Add ability to access (and override) side effects in
SaveContext
(5604) (djhi) - Add support for
disabled
in <ArrayInput>
(5618) (fzaninotto) - Add ability to customize the notification element in the
<Login>
page (5630) (hieusmiths) - Disable ripple effect on Buttons for improved performance (5598) (fzaninotto)
- Fix
<TestContext>
doesn't contain notifications
node (5659) (fzaninotto) - Fix
<Filter>
fails to show compound filters with no default value (5657) (fzaninotto) - Fix "Missing translation" console error when the
dataProvider
fails (5655) (fzaninotto) - Fix
<FilterListItem>
doesn't appear selected when more than one filter is applied (5644) (fzaninotto) - Fix
usePermissions
always triggers a re-render even though the permissions are unchanged (5607) (fzaninotto) - [Doc] Add
rowStyle
example usage to <SimpleList>
jsDoc (5661) (vdimitroff) - [Doc] Fix
<ReferenceField link>
prop type to show that it accepts a function (5660) (vdimitroff) - [Doc] Fix missing import in
List
example (5658) (WiXSL) - [Doc] Fix syntax error in
<List exporter>
prop usage (5649) (WiXSL) - [Doc] Fix Sidebar size change resets the theme color (5646) (zheya08)
- [Doc] Fix
<ReferenceInput>
and <ReferenceArrayInput>
JSDocs point to the wrong dataProvider
method (5645) (WiXSL) - [Doc] Add mention of saved queries in List chapter (5638) (fzaninotto)
- [Doc] Fix
<Admin history>
prop injection documentation misses package version constraint (5538) (fzaninotto)