0

我正在使用 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 显示正确翻译项的正确方法是什么?

任何建议非常感谢。

4

1 回答 1

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

于 2013-02-14T07:17:49.433 回答