i18n
Lightweight simple translation module for React apps based on gettext
Ussage
TranslateProvider
API
name | type | default |
---|---|---|
defaultLanguage | String | 'en' |
langKey | String | 'lang' |
translationsKey | String | 'translations' |
storage | Object | required |
reload | Function | |
useDefaultLanguage | Boolean | false |
monoLanguageJSON | Boolean | false |
getTranslation | Function | required |
defaultTranslations | Object | {} |
defaultLanguage
Default language code that will be used when app opened at first time. You can check possible variants here
tip
You can use navigator.language
langKey
This is key that could be internally used by storage to cache user preferred language.
For example if you are using localStorage
Than it will be keyName
in Storage.setItem
translationsKey
This is key that could be internally used by storage to cache remote translations.
For example if you are using localStorage
Than it will be keyName
in Storage.setItem
storage(required)
Object that will cache data. In general you can use localStorage or AsyncStorage.
create own storage
reload
Function that will be used when switching from rtl-ltr to reload page. Mostly it will be Location: reload() for Web apps. It is not required function and if you don't need to reload page on switching rtl-ltr, you can skip this option
useDefaultLanguage
Boolean flag to make Provider use default lang code instead of user selected. It could be use for example using Crowdin in app translations
monoLanguageJSON
If getTranslation
function return all languages at once than it should be true
. Otherwise if getTranslation
returns translation by each language separatelly, use false
defaultTranslations
Object with default translations pairs that will be used only on first load.
getTranslation
Function to load translations.
It could resolve all languages at once
Or by language
Please use i18n JSON format
gettext(message, data)
Returns the localized translation of message, based on the current language.
pgettext(message, domain, data)
Like gettext(), but looks the message up in the specified domain
ngettext(singular, plural, n)
Like gettext(), but consider plural forms. If a translation is found, apply the plural formula to n, and return the resulting message. If no translation is found, return singular if n is 1; return plural otherwise.
npgettext( singular, plural, domain, n)
Like ngettext(), but look the message up in the specified domain.
useTranslations (hook)
Hook to use transtation in React component
Translator (render props)
React component to use tarnslations as Render prop
useGetLanguage
Hook that return current language
useSetLanguage
Hook that function to change language
useGettext
Hook that returns gettetxt useGettetxt function
usePGettext
Hook that returns pgettetxt usePGettetxt function
useNGettext
Hook that returns ngettetxt useNGettetxt function
useNPGettext
Hook that returns npgettetxt useNPGettetxt function