假设我有两个翻译资源(即语言)。一个资源包含软件中使用的所有密钥的值。此资源(此处称为“ en”)被配置为后备。另一个资源(此处称为“ CN”)没有所有密钥的值。假设默认语言为“ CN”。
现在,我们可以使用 const translation = t(“ some_key”)
查找密钥。 编辑:因此,可能是翻译
现在取自“ CN”或“ en”,具体取决于它是否在“ CN”中定义。
如何确定翻译的语言
;或更一般而言,如何让i18next查找一些密钥并返回有关使用资源的信息?
我对扫描所有资源的方法不感兴趣 translation
,也不对基于 Translation
的内容的启发式方法进行访问。
问题的原因:我想应用正确的 在特定DOM元素上的浏览器中属性。本地化在服务器端运行并通过JSON传输 - 因此,这与 i18next-react
无关。
Let's say I have two translation resources (i.e. languages). One resource contains values for all the keys used in the software. This resource (here referred to as "en") is configured as fallback. The other resource (here referred to as "cn") does not have values for all keys. Let's assume the default language is "cn".
Now we can look up a key using const translation = t("some_key")
. Edit: Thus, it could be that translation
is now taken from "cn" or "en", depending on whether it is defined in "cn".
How to determine the language of translation
; or more generally, how to have i18next look up some key and also return information about the used resource?
I'm not interested in approaches scanning all the resources for translation
, nor in heuristic approaches based on the contents of translation
.
Reason for the question: I want to apply the correct lang
attribute in the browser on a certain DOM element. The localization is run on the server-side and transmitted via JSON – so this is not about i18next-react
.
发布评论
评论(3)
内部i18next具有返回二手语言的Resolve函数……
您可以尝试访问i18next.translator.resolve(key,options)
但是就像说这是没有证件的,而不是正式的公共界面。
编辑:由于V21.7.0有一个新的选项
returnDetails
可以将其设置为true以获取所有相关信息。internally i18next has a resolve function that returns the used language…
You may try to access i18next.translator.resolve(key, options)
but like said this is undocumented and not an official public interface.
Edit: Since v21.7.0 there is a new option
returnDetails
that can be set to true to get all relevant information.如果是版本0.70
If is version 0.70 Change returnDetails from
true
tofalse
也许您可以尝试获得I18N的语言环境。例如,在VUE 2中,您可以使用:
Maybe you can try to get the locale for i18n. For example, in Vue 2, you can get it with: