简体   繁体   English

I18Next和Aurelia动态翻译

[英]dynamic translation with I18Next and Aurelia

I have an aurelia app that I am integrating I18Next with, I have the Aurelia integration working and it was all going smoothly, until I came upon our menu. 我有一个要与I18Next集成的aurelia应用程序,我有Aurelia集成工作,并且一切顺利,直到出现菜单。 For normal translations we are just using the html attributes like so... 对于普通翻译,我们仅使用html属性,例如...

<a href="somelink" i18n="link1">Link 1</a>

then in translation.json files you have 然后在translation.json文件中

{
  link1: "some translation here"
}

and that works great, it translates perfectly. 而且效果很好,可以完美翻译。 The problem I can't figure out is how to handle dynamic stuff, like for example our menu. 我不知道的问题是如何处理动态内容,例如我们的菜单。 We are doing a repeat.for to make the menu, but how do I tell I18Next what key to use from the translations.json file? 我们正在做一个repeat.for来制作菜单,但是我如何告诉I18Next从translations.json文件中使用什么键呢? Here is a simplified example of what I mean. 这是我的意思的简化示例。

<li repeat.for="item of router.navigation">
  <a href.bind="item.href" class="${item.isActive? 'is_active' : ''}>
    ${item.title}
  </a>
</li>

Given the above layout, how can you possibly tell I18Next what keys to use for translation, since it is done in a loop. 给定上面的布局,由于它是循环完成的,您怎么可能告诉I18Next用于转换的键。 I have tried a few things, but all have failed. 我尝试了一些尝试,但都失败了。 I am really stuck here, I need to get this to translate using the translation.json files, but I have no idea how to tell it what key to use for the translation, is it "home", "about", "contact", ...? 我真的被困在这里,我需要使用translation.json文件来翻译它,但是我不知道如何告诉它用于翻译的键是“ home”,“ about”,“ contact” ,...? Any help would be really appreciated. 任何帮助将非常感激。 Thank you. 谢谢。

Just like any other attribute, you can bind to your i18n attribute with aurelia. 就像其他任何属性一样,您可以使用aurelia绑定到i18n属性。 Using the following syntax: 使用以下语法:

<a i18n.bind="'navigation.' + item.title"></a>

I18n will look for a key in your resources file with the name navigation.[item.title] , so if the item.title variable equals to "home", it will look for a key named navigation.home . I18n会在资源文件中寻找名称为navigation.[item.title] ,因此,如果item.title变量等于“ home”,它将寻找一个名为navigation.home的键。

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

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