import i18n from 'i18next' import Backend from 'i18next-xhr-backend' import LanguageDetector from 'i18next-browser-languagedetector' import { reactI18nextModule } from 'react-i18next' import * as R from 'ramda' import dateFnsFormat from 'date-fns/format' const getPattern = R.compose( R.prop(1), R.split('|') ) i18n // load translation using xhr -> see /public/locales // learn more: https://github.com/i18next/i18next-xhr-backend .use(Backend) // detect user language // learn more: https://github.com/i18next/i18next-browser-languageDetector .use(LanguageDetector) // pass the i18n instance to react-i18next. // .use(initReactI18n) .use(reactI18nextModule) // init i18next // for all options read: https://www.i18next.com/overview/configuration-options .init({ debug: false, fallbackLng: 'pt-BR', interpolation: { escapeValue: false, // not needed for react as it escapes by default format: (value, format, lang) => { if (R.or(R.isNil, R.isEmpty)(value)) { return '' } const getValue = R.cond([ [ R.contains('date'), R.compose( pattern => dateFnsFormat(value, pattern), getPattern ) ], [ R.contains('currency'), R.compose( pattern => R.compose( R.trim, R.replace(' ', ''), R.replace(' ', ''), R.replace(pattern, ''), pattern => new Intl.NumberFormat(lang, { style: 'currency', currency: pattern, currencyDisplay: 'code' }).format(value) )(pattern), getPattern ) ], [R.contains('replaceDotToComa'), R.always(R.replace('.', ',', value.toString()))], [R.T, R.always(value)] ]) return getValue(format) } } }) export default i18n