我在我的项目中使用了jamesarosen/ember-i18n。但是我不知道如何在我的代码中使用,这样我就可以从select菜单中切换语言。
例如
如果我将语言从English更改为选择菜单中的hindi
{{ hello }} // output is hello in english 应改为
{{ hello }} // output is नमस्कार in Hindi
如果有人能引用一个例子
发布于 2015-02-05 08:10:12
Ember I18n库目前无法实时更改语言。你唯一的选择就是重新加载整个页面。例如,可以将一种语言添加到http://localhost/en/app这样的URL中,然后在选择菜单中添加<a href="http://localhost/nl/app">Dutch</a>作为链接。这个链接将重新加载应用程序。
在应用程序的某个位置,您必须从URL中提取语言,并为Ember I18n库设置正确的语言。例如,您可以在Ember初始化器中这样做(请参阅这)。
示例
下面是一个用Ember初始化器设置翻译的示例(使用ember-cli)。
import Ember from 'ember';
export default {
name: 'i18n',
initialize: function(container, application) {
Ember.I18n.translations = {
// your translations
};
}
};我省略了从URL中提取语言的部分,因为这可能非常具体。您可以从location.pathname中提取它,这将在域名之后给出url的部分(所以url没有http://domainname.com部分)。
当您在url中定义语言时,您可能希望更改成员应用程序的根url。例如,如果您希望http://domainname.com/en/指向您的ember应用程序,您可以创建一个初始化程序,它设置应用程序的根url:
export default {
name: 'location',
initialize: function(container,application) {
application.Router.reopen({
rootURL: 'en/' // the extracted language value
});
}
};https://stackoverflow.com/questions/28337163
复制相似问题