Home Start Get started Quick tour of Polymer Install the Release Candidate Polymer Feature overview What's new About Polymer 2.0 Upgrade guide Hybrid elements Release notes Custom elements Custom element concepts Define an element Declare properties Shadow DOM & styling Shadow DOM concepts DOM templating Style shadow DOM Custom CSS properties Events Handle and fire events Gesture events Data system Data system concepts Work with object and array data Observers and computed properties Data binding Helper elements Tools Tools overview Polymer CLI Document your elements Test your elements Optimize for production Advanced tools Services polymer.json specification Node support Resources Browser compatibility Glossary API Reference API Reference App Toolbox What's in the box? Using the Toolbox App templates Responsive app layout Routing Localization App storage Service worker Serve your app Case study Shop News Blog Community What's in the box?
Using the Toolbox
App templates Responsive app layout Routing Localization App storage Service worker Serve your app
Case study
Shop News

The localization behavior is prerelease. APIs may be subject to change.

Polymer.AppLocalizeBehavior wraps the format.js library to help you internationalize your application. Note that if you're on a browser that does not natively support the Intl object, you must load a polyfill yourself. An example polyfill can be found at github.com/andyearnshaw/Intl.js.

Polymer.AppLocalizeBehavior supports the same message syntax as format.js, in its entirety; use the library docs as reference for the available message formats and options.

Each element that displays content to be localized should add Polymer.AppLocalizeBehavior. All of these elements share a common localization cache, so you only need to load translations once.

AppLocalizeBehavior can be used directly in Polymer 1.x elements and Polymer 2.x hybrid elements. For class-style elements, use the mixinBehaviors method.

For 2.0 Release Candidate, use the 2.0-preview branch:

bower install --save PolymerElements/app-localize-behavior#2.0-preview

The main application is usually responsible for loading the localized messages and setting the current language.

Sample application (class style elements)

<dom-module id="x-app">
  <template>
    <!-- use the localize method to localize text -->
    <div>{{localize('hello', 'name', 'Batman')}}</div>
  </template>
  <script>
    class XApp extends Polymer.mixinBehaviors([Polymer.AppLocalizeBehavior], Polymer.Element) {
      static get is() { return 'x-app'}

      static get properties() {
        return {
          // set the current language—shared across all elements in the app
          // that use AppLocalizeBehavior
          language: {
            value: 'en'
          },

          // Initialize locale data
          resources: {
            value() {
              return {
                'en': { 'hello': 'My name is {name}.' },
                'fr': { 'hello': 'Je m\'apelle {name}.' }
              }
            }
          }
        };
      }
    }

    customElements.define(XApp.is, XApp);
  </script>
</dom-module>

More typically, the app loads resources from an external file, as shown in the next example.

Sample application (hybrid elements)

<dom-module id="x-app">
   <template>
    <!-- use the localize method to localize text -->
    <div>{{localize('hello', 'name', 'Batman')}}</div>
   </template>
   <script>
      Polymer({
        is: "x-app",

        // include the behavior
        behaviors: [
          Polymer.AppLocalizeBehavior
        ],

        // set the current language—shared across all elements in the app
        // that use AppLocalizeBehavior
        properties: {
          language: {
            value: 'en'
          },
        }

        // load localized messages
        attached: function() {
          this.loadResources(this.resolveUrl('locales.json'));
        },
      });
   </script>
</dom-module>

The main app is also responsible for loading the Intl polyfill (not shown above).

Each element that needs to localize messages should also add the Polymer.AppLocalizeBehavior and use the localize method to translate strings, as shown above.