如何获取给定语言的整个命名空间,或者如果它不存在则返回后备命名空间?

How do I get the whole namespace for a given language, or the fallback namespace if it does not exist?

成功加载命名空间后,我想将其作为给定语言的对象进行访问。

如果给定语言的命名空间不存在,我想获取后备语言的命名空间。

如果后备语言连命名空间都不存在,我想得到一个空对象。

我已经试过了:

var ns = "abc";
var ns_obj = i18next.translator.resourceStore.data[i18next.language][ns];

但一定有更优雅的解决方案。

您可以使用 getResourceBundle() 直接从 i18next 访问翻译对象资源存储的内容。

来自API

getResourceBundle

i18next.getResourceBundle(lng, ns)

Returns a resource bundle.

要获取给定语言或后备语言的命名空间,您可以使用:

var ns = "abc";
var ns_obj = i18next.getResourceBundle(i18next.language, ns)
          || i18next.getResourceBundle(i18next.options.fallbackLng[0], ns) 
          || {};

如果您有多种后备语言,则必须迭代 i18next.options.fallbackLng