最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you use multiple namespaces in i18next? - Stack Overflow

programmeradmin7浏览0评论

I'm just starting i18next and I'd like to create translation file for each module in my project. It seems like using namespaces would be the right way to do this. The project can create a page layout using multiple views, so I need to be able to translate strings from multiple namespaces simultaneously.

I've create a simple example that has two namespaces, but I can only get i18next to translate strings for one namespace. If I use defaultNs: namespaces[0] then the numbers get translated, with defaultNs: namespaces[1] the colors are translated, and with defaultNs: namespaces nothing is translated. But I cannot figure out how to get both namespaces to translate.

<!doctype html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <script src=".11.0.min.js"></script>
        <script src="i18next.js"></script>
        <script>
        $(document).ready(function(){
            var language = "en";
            var namespaces = [ "numbers", "colors" ];
            var config = {
                lng: language,
                fallbackLng: "en",
                resGetPath: "namespaces/__ns__/__ns__-__lng__.json",
                ns: {
                    namespaces: namespaces,
                    defaultNs: namespaces[0]
                },
                debug: true
            };
            i18n.init( config, function onInitComplete() {
                $(".xl8").i18n();
            });

        });
        </script>
    </head>
    <body>
        <h1>hello, i18n!</h1>
        <ol>
            <li class="xl8" data-i18n="numbers.one">1</li>
            <li class="xl8" data-i18n="numbers.two">2</li>
            <li class="xl8" data-i18n="numbers.three">3</li>
        </ol>
        <ul>
            <li class="xl8" data-i18n="colors.red">r</li>
            <li class="xl8" data-i18n="colors.green">g</li>
            <li class="xl8" data-i18n="colors.blue">b</li>
        </ul>
    </body>
</html>

I'm just starting i18next and I'd like to create translation file for each module in my project. It seems like using namespaces would be the right way to do this. The project can create a page layout using multiple views, so I need to be able to translate strings from multiple namespaces simultaneously.

I've create a simple example that has two namespaces, but I can only get i18next to translate strings for one namespace. If I use defaultNs: namespaces[0] then the numbers get translated, with defaultNs: namespaces[1] the colors are translated, and with defaultNs: namespaces nothing is translated. But I cannot figure out how to get both namespaces to translate.

<!doctype html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <script src="http://code.jquery./jquery-1.11.0.min.js"></script>
        <script src="i18next.js"></script>
        <script>
        $(document).ready(function(){
            var language = "en";
            var namespaces = [ "numbers", "colors" ];
            var config = {
                lng: language,
                fallbackLng: "en",
                resGetPath: "namespaces/__ns__/__ns__-__lng__.json",
                ns: {
                    namespaces: namespaces,
                    defaultNs: namespaces[0]
                },
                debug: true
            };
            i18n.init( config, function onInitComplete() {
                $(".xl8").i18n();
            });

        });
        </script>
    </head>
    <body>
        <h1>hello, i18n!</h1>
        <ol>
            <li class="xl8" data-i18n="numbers.one">1</li>
            <li class="xl8" data-i18n="numbers.two">2</li>
            <li class="xl8" data-i18n="numbers.three">3</li>
        </ol>
        <ul>
            <li class="xl8" data-i18n="colors.red">r</li>
            <li class="xl8" data-i18n="colors.green">g</li>
            <li class="xl8" data-i18n="colors.blue">b</li>
        </ul>
    </body>
</html>
Share Improve this question asked Nov 18, 2014 at 20:49 Lee JenkinsLee Jenkins 2,4803 gold badges27 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I posted this same question on github and received the following working answer from jamuhl:

if a resource is not on the default namespace use:

    <li class="xl8" data-i18n="colors:colors.blue">b</li> --> namespace:key

Not sure if this is slightly hack-ish for the use case you describe but (assuming you only want to have two namespaces active at any one time & the keys do not clash) you can do this:

fallbackNS: 'colors',
ns: {
    namespaces: ['colors', 'numbers'],
    defaultNs: 'numbers'
}

This is not really designed for your use case though - it's more designed for a situation where you want to display some translations differently based on another factor - e.g an app which displays 'Employee' for business users or 'Student' for school users - but mainly uses the same set of defaults.

发布评论

评论列表(0)

  1. 暂无评论