I18N
@refinitiv-ui/i18n
provides wrappers and APIs around formatjs IntlMessageFormat and @refinitiv-ui/phrasebook
. In addition, it provides tools to observe lang
attribute changes.
Usage
Use with @refinitiv-ui/translate
to translate Element Framework components.
API Overview
@refinitiv-ui/i18n
provides a set of APIs to facilities translations.
t()
t
function is used to get the translation message from Phrasebook. Please refer to @refinitiv-ui/phrasebook
documentation on how to populate phrasebooks.
const message = await t('element-scope', 'en', 'TRANSLATE_KEY', {
option: 'value',
});
If the requested locale is not available, t
always tries to resolve the translation from the default locale (DEFAULT_LOCALE
). If translation cannot be found the requested translation key is returned:
const message = await t('element-scope', 'unknown-LOCALE', 'TRANSLATION_KEY');
LangAttributeObserver
It is common that translations are applied based on lang
HTML attribute. Please see Language tag syntax to get additional information.
LangAttributeObserver
utilizes MutationObserver to run a callback when lang
attribute changes either on document level or on element level.
<html lang="en-GB">
<body>
<p id="first">This paragraph uses document locale</p>
<p id="second" lang="ru">Этот параграф написан на русском.</p>
</body>
</html>
const paragraph1 = document.getElementById('first');
const paragraph2 = document.getElementById('second');
LangAttributeObserver.observe(paragraph1, () => {
console.log('First paragraph locale changes');
});
LangAttributeObserver.observe(paragraph2, () => {
console.log('Second paragraph locale changes');
});
document.documentElement.lang = 'it';
paragraph2.lang = 'es';
To stop observing lang
changes and avoid memory leaks you must disconnect an element.
LangAttributeObserver.disconnect(paragraph1);
LangAttributeObserver.disconnect(paragraph2);
clearCache() and clearCachedRecord()
@refinitiv-ui/i18n
caches translations for efficient re-use. In rare scenarios, you might need to clear the cache manually.
clearCache();
clearCachedRecord('element-scope', 'en');
resolveLocale()
The function is used to get the most suitable locale for the scope by checking supported locales from Phrasebook.
resolveLocale('element-scope', 'en');
resolveLocale('element-scope', 'en-GB');
resolveLocale('element-scope', 'en-US');
resolveLocale('element-scope', 'ru');
resolveLocale('element-scope', 'zh');
resolveLocale('element-scope', 'zh-Hant');
resolveLocale('element-scope', 'zh-Hant-HK');
Technical Details
Locale resolution
@refinitiv-ui/i18n
tries to match the best available locale or fallback to default if none is available.
For example, if the Phrasebooks defines the following locales: ['en', 'zh-Hant', 'zh-Hans']
. The list below shows how the locales will be resolved:
- en -> en
- en-GB -> en
- en-US -> en
- ru -> DEFAULT_LOCALE (en-GB) -> en
- zh -> DEFAULT_LOCALE (en-GB) -> en
- zh-Hant -> zh-Hant
- zh-Hant-HK -> zh-Hant
Caching
Creating instances of Intl
formats is an expensive operation. @refinitiv-ui/i18n
re-uses Intl Format Cache to memoise translations.
Unicode Extension
Translate object supports BCP47 unicode extensions. You can provide unicode as part of locale
or by passing unicodeExtensions
to t
function.
<html lang="en-u-hc-h24-ca-islamic"></html>
const message = await t(
'element-scope',
'en',
'TRANSLATE_KEY',
{
option: 'value',
},
{
unicodeExtensions: {
hc: 'h24',
ca: 'islamic',
},
}
);
Note that you cannot provide more than one list of unicode extensions. Therefore, if extensions are provided via html and JavaScript, the list is merged.
References
You can get additional information about internationalization: