[英]Create labels for whole website in one javascript file
I'm using react js and I'm creating a multilanguage site. 我正在使用react js,并且正在创建一个多语言站点。
Is there any way to create an javascript file where I can put all my text, labels and then use them on the site? 有什么方法可以创建一个javascript文件,在其中可以放置所有文本,标签,然后在网站上使用它们?
How could I do that? 我该怎么办?
If you want to add i18n to your app, there are several ways to do it. 如果您想将i18n添加到您的应用程序中,可以通过多种方法进行。 In SO you can find solutions like this .
在这样你就可以找到解决方案,像这样 。 Yahoo created a library (plug-in?) named React Intl to do the same job.
雅虎创建了一个名为React Intl的库(插件?)来完成相同的工作。
But if you want to create your own file you could create a json file like: 但是,如果您想创建自己的文件,则可以创建一个json文件,例如:
{
"en":{
"okButton": "OK",
...
},
"es:{
"okButton": "Aceptar",
...
}
"de": { ... }
}
And then just choose one language. 然后只选择一种语言。 Or you could just create a language file for each language, which will give you more flexibility and smaller files.
或者,您可以只为每种语言创建一个语言文件,这将为您提供更大的灵活性和更小的文件。
Assuming that you create a file for each language ( i18n_en.json, i18n_es.json
...), you can then check what is the current language to be loaded and load just the needed file using an Ajax call, generating the URL with something like: 假设您为每种语言创建了一个文件(
i18n_en.json, i18n_es.json
...),然后可以检查当前要加载的语言是什么,并使用Ajax调用仅加载所需的文件,从而生成包含以下内容的URL喜欢:
const LANG_URL='/myPath/lang/i18n_';
var langFileUrl= LANG_URL+ language+'.json';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.