@bity/i18n
v0.2.0
Published
This i18n package was written because all other i18n solutions seem to be overly complicated. `@bity/i18n` is a single function, that uses `window.i18n` to store and access all translations, in a simple key-value manner.
Downloads
31
Readme
@bity/i18n - Internationalization
This i18n package was written because all other i18n solutions seem to be overly
complicated. @bity/i18n
is a single function, that uses window.i18n
to store
and access all translations, in a simple key-value manner.
This allows the JavaScript execution environment to always have access to your translations.
Usage
npm install @bity/i18n
import { t } from '@bity/i18n';
function t(stringToTranslate, alternativeTranslationMap) {
/* Returns the original string or a translated string */
}
Self-explanatory :)
If you want to use this package with nodejs, simply define a bare window
object: global.window = { i18n: { } };
Examples
Single language translations
import { t } from '@bity/i18n';
window.i18n = {
'Hello': 'Bonjour',
'Good morning': 'Bon matin',
'You\'re welcome': 'Bienvenue'
};
console.log(`${t('Hello')} Tim`);
In the case where there is no translation, it'll use the original text passed.
Multiple language translations
In the instance you need to translate to many different languages on the same page, you can do the following:
import { t } from '@bity/i18n';
window.i18n = {
fr: {
'Hello': 'Boujor'
},
de: {
'Hello': 'Guten morgen'
}
};
console.log(`${t('Hello', window.i18n.de)} Tim`);