This package provides an easy way for internationalization support in React.js apps.
In many - but not all - cases language variables are tied to one component in React, but most i18n libraries do not fit into a component based approach.
This package solves this issue and helps you to define language messages component based by providing component based dictionaries. Also each prop of a component can be "intlized", so that it can be used with a language variable or with formatting (date time, number and time ago).
npm install intlized-components
# or
yarn add intlized-componentsThe most convient way to use intlized components is to create a local dictionary for every component, define intlized components and use the predefined <Trans> component. Basically with intlized components you can use any component as intlized component, just import the intlized function and wrap the component. But in most cases you need nothing more than the <Trans> component.
importReactfrom'react';import{createDict,intlized,Trans}from'intlized-components';// Create local dictionary: Define a scope, then define the messages with the default translationconstdict=createDict('RegistrationForm',{welcome: 'Welcome {name}',inputPlaceholder: 'Your name...',imageAlt: 'This image was created on {date}',});// wrap components, intlize propsconstIntlizedInput=intlized('input',['placeholder']);constIntlizedImage=intlized('img',['alt']);// use defined messagesexportdefaultfunction(){return(<div>{/* Basic translation with variable */}<Trans{...dict('welcome',{name: 'dude'})}/>{/* Intlized attribute placeholder */}<IntlizedInputplaceholder={dict('inputPlaceholder')}/>{/* Intlized attribute with date formatting util */}<IntlizedImagealt={({ dateTime })=>dict('imageAlt',{date: dateTime('2017-07-07')})}/></div>);}Furthermore you can format dates and numbers without intlized components:
importReactfrom'react';import{DateTime,TimeAgo,Number}from'intlized-components';exportdefaultfunction({ name }){return(<div><DateTimevalue="2017-07-07"/><TimeAgovalue="2017-07-07"/><Numbervalue={1000}/></div>);}Note that all formatting components use the Intl api which is supported by most browsers and also Node.js. Only Intl.RelativeTimeFormat is relatively new and not widely supported, so we suggest to use a polyfill like relative-time-format for it.
This package provides a useIntl hook, which might be helpful in some use cases:
importReactfrom'react';import{useIntl}from'intlized-components';exportdefaultfunction(){constintl=useIntl();if(intl.locale==='en'){return<span>This text is only visible if locale is set to English.</span>;}else{returnnull;}}typeCreateDict=(scope: string,messages: {[string]: string})=>Messages;typeMessages=(key: string,variables?: Object)=>Intlized$Message;typeuseIntl=()=>{
locale: string,changeLocale(locale: string,messages: Object): void,addMessages(messages: Object): void,trans(...Intlized$Message): string,dateTime(value: string|Date): string,number(value: number): string,timeAgo(value: string|Date): string,};typeIntlized=(Component: React$ElementType,intlizedProps: Array<string>,)=>Intlized$Component;typeIntlized$Trans=React$ComponentType<{ ...Intlized$Message}>;typeIntlized$DateTime=React$ComponentType<{value: string}>;typeIntlized$TimeAgo=React$ComponentType<{value: string}>;typeIntlized$Number=React$ComponentType<{value: string}>;There is a babel plugin called babel-plugin-intlized-components that helps you to extract all defined messages from the component files and to easily create translation definition files.
This package is released under the MIT License.