i18n with redux made easy
npm i --save mi18n-redux
There are many great i18n libraries out there (links below). However, most of them use React components to show translated text (see next question) and when you have to use the function fallback you loose the auto rerender of translated components.
With this lib you can:
Components approach looks nice and consistent with the rest of your React application. However if you need to pass translated text in props you need a function fallback anyway. Function approach makes it consistent throughout the application and is understandable for everyone.
import { reducer as i18nReducer } from 'mi18n-redux';
const rootReducer = combineReducers({
// ...
i18n: i18nReducer,
});
store = createStore(rootReducer);
const enTranslations = {
main: {
title: 'example title',
},
users: {
addUserButton: 'Add new user',
removeUserButton: 'Remove user',
removeUserAlert: {
title: 'Are you sure?',
text: 'This action cannot be undone',
},
},
translationWithDiacritics: 'ąóść'
}
import { setLanguage } from 'mi18n-redux';
store.dispatch(setLanguage('en', enTranslations));
or inside the component connected with redux state
import { setLanguage } from 'mi18n-redux';
class MyComponent {
// ... your component here
}
connect(
null,
{
setLanguage,
}
)(MyComponent); // now MyComponent can just call this.props.setLanguage('en', enTranslations) to change language
import getT from 'mi18n-redux';
class MyComponent {
// ... your component here
}
connect(
state => ({
translator: getT(state.i18n),
namespacedTranslator: getT(state.i18n, 'users'),
lang: state.i18n.languageCode,
})
)(MyComponent);
this.props.translator.t('main.title'); // example title
this.props.namespacedTranslator.t('addUserButton'); // Add new user
this.props.lang; // en
this.props.translator.t('translationWithDiacritics'); // ąóść
this.props.translator.deburr().t('translationWithDiacritics'); // aosc
this.props.translator.deburr().toUpper().t('translationWithDiacritics'); // AOSC
this.props.translator.capitalize().t('main.title'); // Example title
this.props.translator.camelCase().t('main.title'); // exampleTitle
this.props.translator.lowerCase().t('main.title'); // example title
this.props.translator.snakeCase().t('main.title'); // example_title
this.props.translator.kebabCase().t('main.title'); // example-title
this.props.translator.startCase().t('main.title'); // Example Title
this.props.translator.upperCase().t('main.title'); // EXAMPLE TITLE
this.props.translator.upperFirst().t('main.title'); // Example title
reducer
import { reducer as mi18nReducer } from 'mi18n-redux'
setLanguage
import { setLanguage } from 'mi18n-redux'
SET_LANGUAGE_ACTION
import { SET_LANGUAGE_ACTION } from 'i18n-redux'
translationObject
example in english:
const enTranslations = {
main: {
title: 'My awesome translated app'
},
pluralizable: {
dog: [
'dog',
'dogs',
],
person: [
'person',
'people',
],
},
withParams: {
welcome: 'Hello {{user}}! How are you?',
},
}
getT(state, [namespace])
import getT from 'mi18n-redux'
example
import getT from 'mi18n-redux';
class MyComponent {
// ... your component here
}
connect(
state => ({
translator: getT(state.i18n),
namespacedTranslator: getT(state.i18n, 'users'),
})
)(MyComponent);
translator
translator.translate(path, [replace, number])
: function
translator.translate.capitalize()
: function returns translator object with capitalize flag addedtranslator.translate.toUpper()
: function returns translator object with uppercase flag addedtranslator.translate.toLower()
: function returns translator object with lowercase flag addedtranslator.translate.camelCase()
: function returns translator object with camelcase flag addedtranslator.translate.lowerCase()
: function returns translator object with lowercase flag addedtranslator.translate.lowerFirst()
: function returns translator object with lowerFirst flag addedtranslator.translate.titleCase()
: function returns translator object with titleCase flag addedtranslator.translate.deburr()
: function returns translator object with deburr flag addedtranslator.t(path, [replace, number])
: alias for translator.translate
translator.namespace(namespace, [absolute])
: function
translator.n(namespace, [absolute])
: function alias for translator.namespace
translator.getTranslations(path, [absolute])
: function
translator.getLanguage()
translator.pluralMap
: object object of functions (per language code) that takes number and returns index of corresponding plural form in pluralizable translation array
pluralMap = {
default: number => number === 1 ? 0 : 1,
pl: number => {
const last = _.takeRight(number.toString().split(''), 1).join('');
const lastTwo = _.takeRight(number.toString().split(''), 2).join('');
if (number === 1) {
return 0;
}
if ( (last === '2' || last === '3' || last === '4')
&& !(lastTwo === '12' || lastTwo === '13' || lastTwo === '14') ) {
return 1;
}
return 2;
},
en: number => number === 1 ? 0 : 1,
}