简体   繁体   English

使用i18next构建菜单

[英]Using i18next to build a menu

I have a page using i18next on node. 我在节点上使用i18next的页面。

In the translation files, there are translations (duh) for various things (it's like a database for little helper snippets) 在翻译文件中,有针对各种内容的翻译(duh)(就像一个小助手片段的数据库)

now i want to build a page, where the user can view them individually. 现在我想建立一个页面,用户可以在其中单独查看它们。

idea 理念

i want to access the loaded xx-translation.json and make a (dropdown?) menu containing all the entries in one namespace 我想访问加载的xx-translation.json并制作一个(下拉菜单)菜单,其中包含一个名称空间中的所有条目

{
    "category": {
        "subcategory": {    // i want to get ["bla", "blu", "bli"] as result
            "bla"    : "ble",
            "blu"    : "blo",
            "bli"    : "bly"
        }
    }
}

i'm thinking about a simple loop like 我正在考虑一个简单的循环

var amountOfEntries = translationJSON.category.subcategory.length;
for (var i = 0; i < amountOfEntries; i++) {
    $('#menu').   //append that entry somehow
}

tldr questions tldr问题

is there a way to access the translation.json inside my "normal" js and count the number of entries? 有没有办法访问我的“普通” js中的translation.json并计算条目数? and how can i build a menu piece by piece with the entries? 以及如何按条目逐个构建菜单? (this is bonus, i think i'd manage to do that. the main question is the first one) (这是奖金,我想我会做到的。主要问题是第一个)

thanks 谢谢

Check out lodash.js for some good collection utility functions. 请查看lodash.js,以获取一些有用的收集实用程序功能。 You can iterate over the object with something like: 您可以使用以下方法迭代对象:

_.each(subcategory, function(val, key){
    $(".menu").append("<li>" + key + ": " + val + "</li>");
});

is that helpful? 这样有帮助吗?

UPDATE UPDATE

After clarifying your question in the comments above, I think what you're looking for is either getResource() or getResourceBundle() . 在上面的评论中澄清了您的问题之后,我认为您正在寻找的是getResource()getResourceBundle()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM