简体   繁体   English

从选择菜单更改语言时,有人可以提供我jamesarosen / ember-i18n的完整示例吗?

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

i have used jamesarosen/ember-i18n in my project. 我在项目中使用了jamesarosen/ember-i18n but I don't know how to use into my code so that I can switch languages from the select menu. 但是我不知道如何在代码中使用它,以便可以从选择菜单中切换语言。

for example 例如

If I change the language from English to hindi from the select menu 如果我从选择菜单将语言从English更改为hindi

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

should change to 应该更改为

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

Would be great if someone can quote an example 如果有人可以举一个例子,那就太好了

The Ember I18n lib currently has no way to real time change the language. Ember I18n库当前无法实时更改语言。 Your only option is to reload the whole page. 您唯一的选择是重新加载整个页面。 You can do this for example by adding a language to your URL like http://localhost/en/app , in your select menu you can then add <a href="http://localhost/nl/app">Dutch</a> as a link. 例如,您可以通过在URL中添加诸如http://localhost/en/app类的语言来完成此操作,然后在选择菜单中添加<a href="http://localhost/nl/app">Dutch</a>作为链接。 This link will reload the app. 此链接将重新加载应用程序。

Somewhere in your app you then have to extract the language from your URL and set the right language for the Ember I18n lib. 然后,您必须在应用程序中的某个位置从URL中提取语言,并为Ember I18n库设置正确的语言。 You can do this for example in a Ember Initializer (see this ). 您可以在Ember Initializer中进行此操作 (请参阅参考资料)。

Example

Below follows an example of setting your translation with an Ember Initializer (using ember-cli). 下面是一个使用Ember Initializer设置翻译的示例(使用ember-cli)。

import Ember from 'ember';

export default {
    name: 'i18n',
    initialize: function(container, application) {
        Ember.I18n.translations = {
            // your translations
        };
    }
};

I'm omitting the part of extracting the language from the URL as this might be very specific. 我将省略从URL提取语言的部分,因为这可能非常具体。 You can start by extracting it from location.pathname which will give you the part of the url after the domain name (so the url without the http://domainname.com part). 您可以从location.pathname中提取出来,这将为您提供部分域名后的URL(因此,该URL中没有http://domainname.com部分)。

When you define the language in the url, you probably want to change the root url of your ember application. 当您在url中定义语言时,您可能想要更改ember应用程序的根url。 For example if you want http://domainname.com/en/ to point to your ember application, you can for example create an initializer which sets the root url of the application: 例如,如果您希望http://domainname.com/en/指向您的ember应用程序,则可以创建一个初始化程序,该初始化程序设置应用程序的根URL:

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

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

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