|
| 1 | +// list of available languages |
| 2 | +export const enabledLanguages = [ |
| 3 | + 'en', |
| 4 | + 'fr', |
| 5 | +]; |
| 6 | + |
| 7 | +// this object will have language-specific data added to it which will be placed in the state when that language is active |
| 8 | +// if localization data get to big, stop importing in all languages and switch to using API requests to load upon switching languages |
| 9 | +export const localizationData = {}; |
| 10 | + |
| 11 | +// here you bring in 'intl' browser polyfill and language-specific polyfills |
| 12 | +// (needed as safari doesn't have native intl: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) |
| 13 | +// as well as react-intl's language-specific data |
| 14 | +// be sure to use static imports for language or else every language will be included in your build (adds ~800 kb) |
| 15 | +import { addLocaleData } from 'react-intl'; |
| 16 | + |
| 17 | +// need Intl polyfill, Intl not supported in Safari |
| 18 | +import Intl from 'intl'; |
| 19 | +global.Intl = Intl; |
| 20 | + |
| 21 | +// use this to allow nested messages, taken from docs: |
| 22 | +// https://github.com/yahoo/react-intl/wiki/Upgrade-Guide#flatten-messages-object |
| 23 | +function flattenMessages(nestedMessages = {}, prefix = '') { |
| 24 | + return Object.keys(nestedMessages).reduce((messages, key) => { |
| 25 | + const value = nestedMessages[key]; |
| 26 | + const prefixedKey = prefix ? `${prefix}.${key}` : key; |
| 27 | + |
| 28 | + if (typeof value === 'string') { |
| 29 | + messages[prefixedKey] = value; // eslint-disable-line no-param-reassign |
| 30 | + } else { |
| 31 | + Object.assign(messages, flattenMessages(value, prefixedKey)); |
| 32 | + } |
| 33 | + |
| 34 | + return messages; |
| 35 | + }, {}); |
| 36 | +} |
| 37 | + |
| 38 | +// bring in intl polyfill, react-intl, and app-specific language data |
| 39 | +import 'intl/locale-data/jsonp/en'; |
| 40 | +import en from 'react-intl/locale-data/en'; |
| 41 | +import enData from './localizationData/en'; |
| 42 | +addLocaleData(en); |
| 43 | +localizationData.en = enData; |
| 44 | +localizationData.en.messages = flattenMessages(localizationData.en.messages); |
| 45 | + |
| 46 | +import 'intl/locale-data/jsonp/fr'; |
| 47 | +import fr from 'react-intl/locale-data/fr'; |
| 48 | +import frData from './localizationData/fr'; |
| 49 | +addLocaleData(fr); |
| 50 | +localizationData.fr = frData; |
| 51 | +localizationData.fr.messages = flattenMessages(localizationData.fr.messages); |
0 commit comments