Localization/Internationalization for Polymer 1.0

╄→尐↘猪︶ㄣ 提交于 2019-12-05 19:58:27

This may not be a straight answer to your problem but it's a solution nonetheless. We have made our own i18n behavior in Polymer 1.0.

After an element implements the behavior you can write the template as follows:

<template>
    <span>[[localize('myString', locale)]]</span>
</template>

and it will search for the myString key in the locale object and return the localized string.

You can also call the method programmatically using this.localize('myString', locale).

CookieMonster

It turns out that internationalization/localization in Polymer 1.0 is indeed possible with i18next if a set of functions are properly used and scoped within the function that triggers the language selection event.

Within the language selection function, here's the syntax that I typed (with more detail)

somefunction: function(){    
  var option = { resGetPath: '/localizeddata/english.json' };
  i18n.init(option);
  (function(ok) {
    i18n.init(function(t) {
      arg = {
        var value1 = i18n.t("buttons.value1"),
        var value2 = i18n.t("buttons.value2")
      }
      ok.secondaryfunction(arg);
     });
  })(this);
}
secondaryfunction:function(n){
    this.buttons_list = n
}

This way what I get from within secondaryfunction is scoped from the whole page and used in secondaryfunction where (if double bound correctly) the collection stays updated. Thing is, must be careful regarding when and how the functions load.

I just published a simple (heavily in development) element (see it on gitlab or read about it here). It loads the translation files asynchronously and the usage is fairly simple:

<!-- Import it in head -->
<link rel="import" href="bower_components/quaintous-i18n/quaintous-i18n.html">
<!-- initialize it in body -->
<quaintous-i18n locales-path="/locales/fa.json"></quaintous-i18n>

Now you can use it in various ways:

  • In computed properties: just add I18N as your element behavior and translate your data bindings, e.g. {{__('hello')}}
  • In global context just use I18N object, e.g. I18N.__('hello')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!