当从 select 菜单更改语言时,有人能给我提供 jamesarosen/ember-i18n 的完整示例吗?

can anbody provide me complete example of jamesarosen/ember-i18n when changing language from select menu?

我在我的项目中使用了jamesarosen/ember-i18n。 但我不知道如何在我的代码中使用,以便我可以从 select 菜单切换语言。

例如

如果我从 select 菜单将语言从 English 更改为 hindi

{{ hello }} // output is hello in english 

应该改为

{{ hello }} // output is नमस्कार in Hindi

如果有人能举个例子就好了

Ember I18n 库目前无法实时更改语言。您唯一的选择是重新加载整个页面。例如,您可以通过向您的 URL 添加一种语言(例如 http://localhost/en/app),然后在您的 select 菜单中添加 <a href="http://localhost/nl/app">Dutch</a> 作为 link。 link 将重新加载应用程序。

在您的应用程序中的某个地方,您必须从 URL 中提取语言并为 Ember I18n 库设置正确的语言。例如,您可以在 Ember 初始化程序中执行此操作(请参阅 this)。

例子

下面是一个使用 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 中定义语言时,您可能想要更改 ember 应用程序的根目录 url。例如,如果您希望 http://domainname.com/en/ 指向您的 ember 应用程序,您可以创建一个初始化程序来设置应用程序的根 url:

export default {
    name: 'location',
    initialize: function(container,application) {
        application.Router.reopen({
            rootURL: 'en/' // the extracted language value
        });
    }
};