Introduction
With the customized block in the store, we need to learn to internationalize the content presented.
It is important to remember that blocks must always follow good localization practices, and must not show hardcoded strings, but ones that are sensitive to the language that the store operates.
Don't worry, you won't need to add translations of all texts for the various languages in which the Store Framework is used. Therefore, in this stage, will be presented concepts about the internationalization of apps and how to do it.
The Messages
The concept of messages makes it easy to add new languages to the theme. The Messages centralize all translation services on the platform. Given a text to be translated, Messages will first check the user-defined context, then check the translations of the apps and, finally, go through the automatic translation system.
In the directory structure, you can see that there is a folder called messages, which has three main files: pt.json, en.json, andes.json, each responsible for the translations: Portuguese, English, and Spanish, respectively. In addition, in order to provide better automatic translations, the context.json file is used, which is responsible for avoiding ambiguities.
To use such definitions, the translation files mentioned above are JSON, whose keys are messages and values are translations.
The
context.jsonfile is necessary and must contain all messages, besides offering automatic translations in exceptional cases.
Internationalizing your block
You must have learned how to use our builder messages, and it will be through it that internationalized strings will be added to the components.
-
To do so, in the directory
/messages, add now a message for the title of the component:messages/pt.json_10{_10...,_10+ "countdown.title": "Contagem Regressiva"_10}messages/en.json_10{_10...,_10+ "countdown.title": "Countdown"_10}messages/es.json_10{_10...,_10+ "countdown.title": "Cuenta Regresiva"_10}messages/context.json_10{_10...,_10+ "countdown.title": "Countdown"_10} -
After that, to render the title the component
FormattedMessageof the lib react-intl must be used.The lib react-intl supports many ways of configuration and internationalization, it is worth checking it out.
-
Now, add the lib using
yarn add react-intlin the react directory. After doing that, in your component's code,Countdown.tsx, import the FormattedMessage:_10//react/Countdown.tsx_10import { FormattedMessage } from 'react-intl' -
And add a new prop to the interface
CountdownProps:_10interface CountdownProps {_10+ title: string_10targetDate: string_10} -
You'll also need to add a const that will be your title:
_10//react/Countdown.tsx_10const titleText = title || <FormattedMessage id="countdown.title" /> -
Now, join the title to the countdown to render. To do so, define a container outside. Besides, the text for the title will be passes using the prop
title:_25//react/Countdown.tsx_25const Countdown: StorefrontFunctionComponent<CountdownProps> = ({_25title,_25targetDate,_25}) => {_25const [timeRemaining, setTime] = useState<TimeSplit>({_25hours: '00',_25minutes: '00',_25seconds: '00',_25})_25_25const titleText = title || <FormattedMessage id="countdown.title" />_25const handles = useCssHandles(CSS_HANDLES)_25_25tick(targetDate, setTime)_25_25return (_25<div className={`${handles.container} t-heading-2 fw3 w-100 c-muted-1`}>_25<div className={`${handles.title} db tc`}>{titleText}</div>_25<div className={`${handles.countdown} db tc`}>_25{`${timeRemaining.hours}:${timeRemaining.minutes}:${timeRemaining.seconds}`}_25</div>_25</div>_25)_25} -
Note that three new handles are used: container, countdown and title. Therefore, remember to declare them in the const
CSS_HANDLES, seen in the previous step:_10const CSS_HANDLES = ['container', 'countdown', 'title'] -
At last but not least, it is needed to add the
titleprop in the schema:_19//react/Countdown.tsx_19Countdown.schema = {_19title: 'editor.countdown.title',_19description: 'editor.countdown.description',_19type: 'object',_19properties: {_19+ title: {_19+ title: 'I am a title',_19+ type: 'string',_19+ default: null,_19+ },_19targetDate: {_19title: 'Final date',_19description: 'Final date used in the countdown',_19type: 'string',_19default: null,_19},_19},_19}
Done! Now, to try out your store in other languages, you just need to add the query string /?cultureInfo=pt-br or /?cultureInfo=es-ar on the URL, for example. By using the first URL, the expected result is this one:
