i18next-node问题与对象项

时间:2013-02-13 20:08:37

标签: javascript node.js internationalization

我使用i18next本地化网站,在我的/locales/dev/translation.json上我有以下内容:

  "guide": {
    "sections": {
        "the-basics": {"title": "The Basics",
                        "contents": [{"title": "Introduction",
                                        "content": {"p1": "", "p2": "", "p3": ""}}
                                    ,{"title": "A Chapter in the Zeitgeist Movement",
                                        "content": {"p1": "", "p2": "", "p3": ""}}
                                    ]
                        }
        ,"setting-up-a-national-chapter": {"title": "Setting up a National Chapter",
                        "contents": [{"title": "Gathering Volunteers & Social Media",
                                        "content": {"p1": "", "p2": "", "p3": ""}}
                                    ]
                        }
    }
  }

我的模板就像:

aside#nav-container(role="complementary")
    nav#primary-nav.overview(role="nav")
        - for(var i in items)
            h3.section
                span.menu-toggle
                span(data-content=""+i data-i18n="guide.sections."+i+".title")=i18n.t("guide.sections."+i+".title")
            ul.sub-section
                - var contents = items[i].contents
                - for(var c in contents)
                    li
                        - console.log(contents[c].title) // returns the correct item title
                        a.nav-link(href="#" data-content=""+c data-i18n="guide.sections."+i+".contents.title")=i18n.t("guide.sections."+i+".contents.title")

这只返回“guide.sections。”+ i +“。contents.title”

问题是"guide.sections."+i+".contents"是一个包含一个或多个项目的对象!

当我检查代码时,我得到以下内容:

<ul class="sub-section active"><li><a href="#" class="nav-link" data-content="0" data-i18n="guide.sections.the-basics.contents.title">guide.sections.the-basics.contents.title</a></li><li><a href="#" class="nav-link" data-content="1" data-i18n="guide.sections.the-basics.contents.title">guide.sections.the-basics.contents.title</a></li></ul>

我启用了.sub-section.i18n()

使i18next显示正确的翻译项目的正确方法是什么?

任何建议都非常感激。

1 个答案:

答案 0 :(得分:0)

guide.sections.the-basics.contents.title仅在您启用返回对象时才会返回对象{“p1”:“”,“p2”:“”,“p3”:“”}:

http://i18next.com/pages/doc_features.html#objecttree

解析这个对象(多个段落),你需要添加一个后处理器,将对象转换为有效的html:

http://i18next.com/pages/doc_features.html#postprocessing