简体   繁体   English

如何在i18next中将语言更改为多个文件而不是一个?

[英]How do I change the language to multiple files instead of one in i18next?

I am using i18next with localstorage to change the language of my website (leaflet map with markers). 我正在使用i18next和localstorage来更改我的网站的语言(带标记的传单地图)。 I have all the languages in one file called lang.js. 我将所有语言都放在一个名为lang.js的文件中。 I want to split it into multiple files, each one for a language. 我想将它分成多个文件,每个文件都用于一种语言。 How I can do that? 我怎么能这样做?

link to the language file on github: https://github.com/hurtworldmap/hurtworldmap.github.io/blob/master/raw/lang.js 链接到github上的语言文件: https//github.com/hurtworldmap/hurtworldmap.github.io/blob/master/raw/lang.js

My function: 我的功能:

$(document).ready(function() {
    var language = "en";
    if (localStorage.getItem("language") != null)
        language = localStorage.getItem("language");

    i18n.init({
        lng: language,
        resStore: resources,
        fallbackLng: "en"
    }, function(o) {
        $(document).i18n()
    }), $(".lang").click(function() {
        var o = $(this).attr("data-lang");

        localStorage.setItem("language", o);

        i18n.init({
            lng: o
        }, function(o) {
            $(document).i18n()
        })
    })
});

Example of language (in the same file): 语言示例(在同一文件中):

"ru": {
        "translation": {
            "locations": "Локации",
            "showlocations": "Показать локации",
            "animals": "Животные",
            "shigi": "Шиги",
            "shigiwolf": "Дикий Шиги",
            "shigiforest": "Лесной Шиги",
            "shigiarctic": "Арктический Шиги",
            "bor": "Кабан",
            "radbor": "Радиоактивный Кабан",
            "tokar": "Токар",
            "tokarblue": "Синий Токар",
            "tokarparrot": "Токар-попугай",
            "sasquatch": "Снежный",
            "yeti": "Йети",
            "plants": "Растения",
            "owrong": "Апельсин",
            "pitcherplant": "Кувшинка",
            "succulentseeds": "семена Succulent",
            "items": "Вещи",
            "itemflint": "Кремень",
            "itemwood": "Дерево",
            "lootcrate": "Ящик",
            "resources": "Ресурсы",
            "woodlog": "Бревно",
            "coal": "Уголь",
            "limestone": "Известняк/Глина",
            "flint": "Кремень/Камень",
            "deadtree": "Мертвое дерево",
            "ores": "Руды",
            "ironore": "Железная руда",
            "titranium": "Титраниумная руда",
            "mondinium": "Мондиниумная руда",
            "ultranium": "Ультраниумная руда",
            "largerock": "Большой камень",
            "valley" : "Valley",
            "fortress": "Fortress",
            "transit": "Transit",
            "boonies": "Boonies",
            "crossroad": "Crossroad",
            "airfield": "Airfield",
            "carrier": "Carrier",
            "dome": "Dome",
            "arch": "Arch",
            "created": "Создано:",
            "names": "RogerHN и LuisMika",
            "legal": "Hurtworld и логотипы Hurtworld являются тварными знаками Bankroll Studios",
            "website": "Официальный сайт игры",
            "welcome": "Это первая версия карты.",
            "welcome2": "Если вы нашли баг, то пожалуйста пришли его в наш пост на ",
            "welcome3": "Reddit'e или в группу ВК.",
            "settings": "настройки",
            "languagechange": "Изменить язык",
            "translationbr": "Португальский Бразильский перевод RogerHN",
            "translationru": "Перевод на русский от Roofy",
            "translationde": "Перевод на Немецкий от Valixx",
            "translationcn": "Перевод на китайский от Smoke6",
            "translationpl": "Польский перевод от Magiczna Huśtawka",
            "translationro": "Румынский перевод от Blaconix",
            "translationtr": "турецкий перевод от Vinerra",
            "translationit": "Итальянский перевод от CHC",
            "translationes": "Испанский перевод Yoje",
            "version": "версия"
        }
    }

Not sure if you still require an answer to this so I'll just put this here in case anyone else faces a similar issue. 不确定你是否仍然需要这个答案,所以我只是把它放在这里以防万一其他人面临类似的问题。

What I have done in a similar situation is: 我在类似情况下所做的是:

$(document).ready(function() {
    var language = "en";
    if (localStorage.getItem("language") != null)
        language = localStorage.getItem("language");

    i18n.init({
        lng: language,
        resGetPath: '/languages/__lng__.min.json',
        fallbackLng: "en"
    }, function(o) {
        $(document).i18n()
    }), $(".lang").click(function() {
        var o = $(this).attr("data-lang");

        localStorage.setItem("language", o);

        i18n.init({
            lng: o
        }, function(o) {
            $(document).i18n()
        })
    })
});

This gives you a file per language and you can remove the language key from the file: 这为您提供了每种语言的文件,您可以从文件中删除语言键:

{
    "translation": {
         "help": "Help Me!
    }
}

If you have multiple namespaces you can also use a single file per namespace by using: 如果您有多个名称空间,则还可以使用每个名称空间使用单个文件:

resGetPath: "/languages/__lng__/__ns__.min.json"

Your file /languages/en/translation.min.json should then contain no key for the namespace like so: 您的文件/languages/en/translation.min.json应该不包含命名空间的键,如下所示:

{
     "help": "Help Me!
}

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

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