1.0.2 • Published 6 years ago

internationalization-js v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Internationalization.js

Add to your project

NPM

npm install internationalization-js

Yarn

yarn add internationalization-js

Add translation files to your project

I prefer to make one file per language and then combine them in one js file, but you could also put all translations in one big file or use seperate files and import them seperatly.

export default {
  en: {
    welcome: 'Welcome to my cool React app',
    log_in: 'Log in',
    log_out: 'Log out',
    account: 'Account',
  }
};
export default {
  nl: {
    welcome: 'Welkom bij mijn coole React app',
    log_in: 'Log in',
    log_out: 'Log out',
    account: 'Account',
  }
};
import nl from './nl';
import en from './en';

export default {
  nl,
  en,
};

Setup in your project

Now it's time to import the translations and import them into the plugin.

import I from 'internationalization-js';
import Translations from './localisation';

...
L.setStringsForLanguages(Translations); // This function adds the translation strings into our plugin
L.setDefaultLanguage('nl', true); // You can use whatever language code styling you prefer, as long as you keep the key in the language file the same

Let's translate!

import I from 'internationalization-js';
const localizedWelcome = I.translate('welcome'); // returns => 'Welkom bij mijn coole React app'

Or for example in your react app

import I from 'internationalization-js';

...
render () {
    return (
        <View>
            <Text>{I.translate('welcome')}</Text>
        </View>
    );
}

Available methods

MethodDescriptionParametersReturns
setStringsForLanguageSet the strings from the strings object for a given languagelanguage: string strings: object
setStringsForLanguagesLoops over object of language strings objects and set them as string for the given languagelanguages: Object
setDefaultLanguageSet the default language to be used when no language has been set yetlanguage: string isCurrentLanguage: boolean
getLanguageGet the currently selected language Or the default language when no language has been set yetstring: current or default language
getAvailableLanguagesGet all available languagesarray: array of language codes (string)
getAllStringsForCurrentLanguageGet all available strings (with translation) for current languageobject: all available strings
translateGet a translation for given key Returns key when no translation could be foundkey: stringstring: translation or key

License

MIT