Heim  >  Fragen und Antworten  >  Hauptteil

javascript - ember-i18n支持多语言切换的问题

我在项目中使用ember.js作为Web前端框架,由于需要支持多语言界面,所以准备使用ember-i18n来实现,不过在ember-i18n的项目说明里没找到支持多语种切换的例子,请问哪位大侠有相关经验?希望能指教一二,非常感谢!~

PHP中文网PHP中文网2748 Tage vor617

Antworte allen(1)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-04-10 13:14:59

    ember-i18n这个测试了一下,真的没有搞明白,以下代码修改自ember-starter-kit,使用的是i18n-js

    App = Ember.Application.create();
    
    App.Router.map(function() {
      // put your routes here
    });
    
    App.IndexRoute = Ember.Route.extend({
      model: function() {
        return ['red', 'yellow', 'blue'];
      }
    });
    
    I18n.translations = {
      en: {
        hello: 'hello'
      },
    
      zh: {
        hello: '你好'
      }
    };
    
    I18n.locale = 'zh'; // 自定义方法修改local就能达到国际化效果
    
    Ember.Handlebars.registerHelper('i18n', function(property, options) {
      var params = options.hash,
          self = this;
    
      // Support variable interpolation for our string
      Object.keys(params).forEach(function (key) {
        params[key] = Em.Handlebars.get(self, params[key], options);
      });
    
      return I18n.t(property, params);
    });
    
    <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <title>Ember Starter Kit</title>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">
      </head>
      <body>
        <script type="text/x-handlebars">
          <h2>Welcome to Ember.js</h2>
          <h3>{{i18n hello}}</h3>
    
          {{outlet}}
        </script>
    
        <script type="text/x-handlebars" id="index">
          <ul>
          {{#each item in model}}
            <li>{{item}}</li>
          {{/each}}
          </ul>
        </script>
    
        <script src="js/libs/jquery-1.10.2.js"></script>
        <script src="js/libs/handlebars-1.1.2.js"></script>
        <script src="js/libs/ember-1.4.0.js"></script>
        <script src="js/libs/i18n.js"></script>
        <script src="js/app.js"></script>
        <!-- to activate the test runner, add the "?test" query string parameter -->
        <script src="tests/runner.js"></script>
      </body>
      </html>
    

    代码样例地址:ember-i18n-simple
    参考:i18n-in-ember

    Antwort
    0
  • StornierenAntwort