简体   繁体   中英

Using i18next to build a menu

I have a page using i18next on node.

In the translation files, there are translations (duh) for various things (it's like a database for little helper snippets)

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

{
    "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

is there a way to access the translation.json inside my "normal" js and count the number of entries? 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. You can iterate over the object with something like:

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

is that helpful?

UPDATE

After clarifying your question in the comments above, I think what you're looking for is either getResource() or getResourceBundle() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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