If for some reason, you cannot use the components, you can use the translate and localize helpers instead:
import{translate,localize}from'react-i18nify';translate('application.title');// => Toffe app met i18n!translate('application.hello',{name: 'Aad'});// => Hallo, Aad!'translate('export',{count: 0});// => Niks te exporterentranslate('application.unknown_translation');// => unknown_translationtranslate('application',{name: 'Aad'});// => {hello: 'Hallo, Aad!', title: 'Toffe app met i18n!'}localize(1385856000000,{dateFormat: 'date.long'});// => 1 december 2013localize(Math.PI,{maximumFractionDigits: 2});// => 3,14localize('huh',{dateFormat: 'date.long'});// => null
If you want these helpers to be re-rendered automatically when the locale or translations change, you have to wrap them in a <I18n> component using its render prop:
react-i18nify uses date-fns internally to handle date localization. In order to reduce the base bundle size, date-fns locale objects needed for date localization are not included by default. If you need date localization, you can add them manually using addLocale or addLocales. For a list of available locales, refer to the date-fns list.
import{addLocale,addLocales,setLocale}from'react-i18nify';importenfrom'date-fns/locale/en-US';importnlfrom'date-fns/locale/nl';importitfrom'date-fns/locale/it';// Add a single localeaddLocale('nl',nl);setLocale('nl');// Add multiple localesaddLocales({ nl, it, en });setLocale('it');
API Reference
<Translate>
React translate component, with the following props:
value (string)
The translation key to translate.
Other props
All other provided props will be used as replacements for the translation.
<Localize>
React localize component, with the following props:
value (number|string|object)
The number or date to localize.
dateFormat (string)
The translation key for providing the format string. Only needed for localizing dates.
For the full list of formatting tokens which can be used in the format string, see the date-fns documentation.
parseFormat (string)
An optional formatting string for parsing the value when localizing dates.
For the full list of formatting tokens which can be used in the parsing string, see the date-fns documentation.
Add multiple date-fns locales to the available locales for date localization at once.
import{addLocales,setLocale}from'react-i18nify';importnlfrom'date-fns/locale/nl';importitfrom'date-fns/locale/it';addLocales({ nl, it });setLocale('it');
setLocale(locale, rerenderComponents = true)
The used locale can be set with this function. By default, changing the locale will re-render all components.
This behavior can be prevented by providing false as a second argument.
The used translations can be set with this function. By default, changing the translations will re-render all components.
This behavior can be prevented by providing false as a second argument.
getTranslations()
Get the currently used translations.
setLocaleGetter(fn)
Alternatively to using setLocale, you can provide a callback to return the locale with setLocaleGetter:
By default, when a translation is missing, the translation key will be returned in a slightly formatted way,
as can be seen in the translate('application.unknown_translation'); example above.
You can however overwrite this behavior by setting a function to handle missing translations.
By default, when a localization failed, null will be returned,
as can be seen in the localize('huh', { dateFormat: 'date.long' }); example above.
You can however overwrite this behavior by setting a function to handle failed localizations.
Helper function to translate a key, given an optional set of replacements. See the above Helpers section for examples.
localize(value, options)
Helper function to localize a value, given a set of options. See the above Helpers section for examples.
For localizing dates, the date-fns library is used.
A dateFormat option can be used for providing a translation key with the format string.
For the full list of formatting tokens which can be used in the format string, see the date-fns documentation.
Moreover, parseFormat option can be used for providing a formatting string for parsing the value.
For the full list of formatting tokens which can be used in the parsing string, see the date-fns documentation.
请发表评论