由于这是关于数据的一切,您不需要特殊的React库。您只能使用有关像here这样的国家/地区的数据。这是在下面的模式JSON(我削减了一些领域,完整的例子可以发现here):
{
"name": {
"common": "Austria",
"official": "Republic of Austria",
"native": {
"bar": {
"official": "Republik Österreich",
"common": "Österreich"
}
}
},
"cca2": "AT",
"cca3": "AUT",
"translations": {
"deu": {"official": "Republik Österreich", "common": "Österreich"},
"fra": {"official": "République d'Autriche", "common": "Autriche"},
"ita": {"official": "Repubblica d'Austria", "common": "Austria"},
"jpn": {"official": "オーストリア共和国", "common": "オーストリア"},
"spa": {"official": "República de Austria", "common": "Austria"}
},
}
注意,虽然翻译部分采用ISO 639-3
码和识别部分使用ISO 3166-1
代码,你可以很容易地将它们映射因为土生土长的语言部分使用ISO 639-3
代码。
假设您必须显示预定义的国家列表。你所要做的就是获得他们的cca3
代码并将它们放入一个数组中。然后,当你在ISO 639-3
格式(userLoc在下面的例子中)发现你的用户区域,可以使用lodash或类似的库来获得相应的翻译:
// userLoc - current user locale in ISO 639-3
// codesToSelect - array of country codes in ISO 3166-1 you want to show
_.map(codesToSelect, (cca3) => {
let countryObject = _.find(countriesData, { 'cca3': cca3 });
let translatedName = (countryObject &&
countryObject.translations &&
countryObject.translations[ userLoc ])
? countryObject.translations[ userLoc ]
: cca3
return translatedName;
})
注意,有可能不是一个指定的翻译国家代码,所以cca3将被用作后备。