本文介绍了如何使用React Intl动态添加语言环境数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am使用React-intl来实现UI Util库的国际化.该库有一个名为i18n的文件夹,其中我放置了用于不同语言环境的json文件.如果该库的用户想要添加对其他语言环境的支持,则他/她可以将具有各自的语言环境的键/值对的其他json文件放置在该文件中. >

但是React-intl需要先为各自的语言环境导入并添加LocaleData.例如,

import fr from 'react-intl/locale-data/fr';
addLocaleData([...fr]);

是否可以在React-intl中动态添加addLocaleData并为相应的语言环境导入语言环境库?

解决方案

嘿,我现在已经这样做了,如下所述及其工作方法:-)

const possibleLocale = navigator.language.split('-')[0] || 'en';
addLocaleData(require(`react-intl/locale-data/${possibleLocale}`));

在这里,通过navigator.language从浏览器中获取语言环境.希望这会有所帮助:-)

Am using React-intl for internationalization of an UI Util Library. The library has a folder say i18n wherein I place json files for different locales.If the user of this library want to add support for additional locales, he/she can place additional json file with key/value pairs for the respective locale.

But React-intl requires to import and addLocaleData for the respective locale in prior.For example,

import fr from 'react-intl/locale-data/fr';
addLocaleData([...fr]);

Is there a way to addLocaleData and import the locale library for the respective locale dynamically in React-intl?

解决方案

Hey I have done this now, as described below and its working :-)

const possibleLocale = navigator.language.split('-')[0] || 'en';
addLocaleData(require(`react-intl/locale-data/${possibleLocale}`));

Here, the locale is fetched from the browser through navigator.language.Hope this helps :-)

这篇关于如何使用React Intl动态添加语言环境数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 05:17