テンプレートエンジンとしてブレードを使用するウェブサイトの i18nにi18nextを使用するこの 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": ""}
}
]
}
}
}
私の JavaScript では、インデックス番号に応じて、コンテンツ リストにアクセスしようとしています。
これは私がこれまでに持っているものです:
// Load page
$('a.nav-link').on('click', function( event ) {
event.preventDefault();
var $this = $(this);
var section = $this.closest('ul').prev('h3').find('span[data-content]').data('content');
var subSection = $this.attr("data-content");
// we can now genrate the section content and push this into the tmpl.
blade.Runtime.loadTemplate("guide_section.blade", function(err, tmpl) {
tmpl({
'sections': {
'section': section,
'subsection': subSection
}
}, function(err, html) {
if(err) throw err;
console.log(html);
$('.mainarticle > .content').empty().html(html);
});
});
});
そして私のテンプレートでは:
#guide_section
- var i = sections.section
- var c = sections.subsection
h5(data-i18n="guide.sections."+i+".title")=i
h4(data-i18n="guide.sections."+i+".contents.title")=c
上記のコードは jade のテンプレートに非常に似ています。
タイトルを返すというH5
点で問題なく動作しますが、コンテンツ リストの n 番目の要素を返す方法がわかりません。
例えば:
h5(data-i18n="guide.sections."+i+".title")=i
としてレンダリングします
<h5 data-i18n="guide.sections.the-basics.title">the-basics</h5
これは正しいです。i18next が を検索し、値にtranslation.json
基づいて正しい翻訳を設定しますが、data-i18n
data-i18n="guide.sections."+i+".contents.title"
としてレンダリングします
<h4 data-i18n="guide.sections.the-basics.contents[0]">0</h4
ただしcontents
、この場合、次のような 2 つのエントリがあります。
[{
"title": "Introduction",
"content": {"p1": "", "p2": "", "p3": ""}
},{
"title": "A Chapter in the Zeitgeist Movement",
"content": {"p1": "", "p2": "", "p3": ""}
}]
contents
では、インデックスを指定してリストのタイトルにアクセスできるようにスクリプトを変更するにはどうすればよいでしょうか。