如何获得I18Next.t返回的字符串语言?

发布于 2025-01-26 10:00:48 字数 640 浏览 3 评论 0 原文

假设我有两个翻译资源(即语言)。一个资源包含软件中使用的所有密钥的值。此资源(此处称为“ 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱猫软卧 2025-02-02 10:00:48

内部i18next具有返回二手语言的Resolve函数……

您可以尝试访问i18next.translator.resolve(key,options)
但是就像说这是没有证件的,而不是正式的公共界面。

编辑:由于V21.7.0有一个新的选项 returnDetails 可以将其设置为true以获取所有相关信息。

  const resolved = t('key', { returnDetails: true });
  resolved.res;
  resolved.usedKey;
  resolved.exactUsedKey;
  resolved.usedNS;
  resolved.usedLng;

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.

  const resolved = t('key', { returnDetails: true });
  resolved.res;
  resolved.usedKey;
  resolved.exactUsedKey;
  resolved.usedNS;
  resolved.usedLng;
红颜悴 2025-02-02 10:00:48

如果是版本0.70

If is version 0.70 Change returnDetails from true to false

一指流沙 2025-02-02 10:00:48

也许您可以尝试获得I18N的语言环境。例如,在VUE 2中,您可以使用:

let lang = this.$i18n.locale //return lang set ('es', 'en', 'cn', 'fr', etc)

Maybe you can try to get the locale for i18n. For example, in Vue 2, you can get it with:

let lang = this.$i18n.locale //return lang set ('es', 'en', 'cn', 'fr', etc)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文