How to use ra-i18n-polyglot - 3 common examples

To help you get started, weā€™ve selected a few ra-i18n-polyglot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github marmelab / react-admin / examples / demo / src / App.tsx View on Github external
import { Login, Layout } from './layout';
import { Dashboard } from './dashboard';
import customRoutes from './routes';
import englishMessages from './i18n/en';

import visitors from './visitors';
import orders from './orders';
import products from './products';
import invoices from './invoices';
import categories from './categories';
import reviews from './reviews';

import dataProviderFactory from './dataProvider';
import fakeServerFactory from './fakeServer';

const i18nProvider = polyglotI18nProvider(locale => {
    if (locale === 'fr') {
        return import('./i18n/fr').then(messages => messages.default);
    }

    // Always fallback on english
    return englishMessages;
}, 'en');

const App = () => {
    const [dataProvider, setDataProvider] = useState(null);

    useEffect(() => {
        let restoreFetch;

        const fetchDataProvider = async () => {
            restoreFetch = await fakeServerFactory(
github navidrome / navidrome / ui / src / i18n / provider.js View on Github external
}
  }
}

const prepareLanguage = (lang) => {
  removeEmpty(lang)
  // Make "albumSong" and "playlistTrack" resource use the same translations as "song"
  lang.resources.albumSong = lang.resources.song
  lang.resources.playlistTrack = lang.resources.song
  // ra.boolean.null should always be empty
  lang.ra.boolean.null = ''
  // Fallback to english translations
  return deepmerge(en, lang)
}

export default polyglotI18nProvider((locale) => {
  // English is bundled
  if (locale === 'en') {
    return prepareLanguage(en)
  }
  // If the requested locale is in already loaded, return it
  const current = JSON.parse(localStorage.getItem('translation'))
  if (current && current.id === locale) {
    return prepareLanguage(JSON.parse(current.data))
  }
  // If not, get it from the server, and store it in localStorage
  return retrieveTranslation(locale)
}, defaultLocale())
github marmelab / react-admin / packages / ra-core / src / i18n / useSetLocale.spec.js View on Github external
it('should use the i18n provider when using TranslationProvider', async () => {
        const i18nProvider = polyglotI18nProvider(locale => {
            if (locale === 'en') return { hello: 'hello' };
            if (locale === 'fr') return { hello: 'bonjour' };
        });
        const { getByText, queryAllByText } = renderWithRedux(
            
                
            
        );
        expect(queryAllByText('hello')).toHaveLength(1);
        expect(queryAllByText('bonjour')).toHaveLength(0);
        act(() => {
            fireEvent.click(getByText('FranƧais'));
        });
        await wait();
        expect(queryAllByText('hello')).toHaveLength(0);
        expect(queryAllByText('bonjour')).toHaveLength(1);

ra-i18n-polyglot

Polyglot i18n provider for react-admin

MIT
Latest version published 13 days ago

Package Health Score

89 / 100
Full package analysis

Popular ra-i18n-polyglot functions

Similar packages