GithubHelp home page GithubHelp logo

darksideofmo0n / app-localize-behavior Goto Github PK

View Code? Open in Web Editor NEW

This project forked from toshovski/app-localize-behavior

0.0 0.0 1.0 108 KB

Polymer behaviour to help internationalize your application

HTML 100.00%

app-localize-behavior's Introduction

Build status

Polymer.AppLocalizeBehavior

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 the polyfill yourself. An example polyfill can be found here.

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

Sample application loading resources from an external file:

<dom-module id="x-app">
  <template>
    <div>[[localize('hello', 'name', 'Batman')]]</div>
  </template>
  <script>
  class XApp extends Polymer.AppLocalizeBehavior(Polymer.Element) {
    static get is() {
      return "x-app"
    }
   
   static get properties() {
     return {
       language: {
         value: 'en'
       }
     }   
   }
   
   connectedCallback() {
     super.connectedCallback();  
     this.loadResources(this.resolveUrl('locales.json'));
   }
 }
 customElements.define(XApp.is, XApp);
 &lt;/script>
 </dom-module>

Alternatively, you can also inline your resources inside the app itself:

<dom-module id="x-app">
  <template>
    <div>[[localize('hello', 'name', 'Batman')]]</div>
  </template>
  <script>
  class XApp extends Polymer.AppLocalizeBehavior(Polymer.Element) {
    static get is() {
      return "x-app"
    }
   
   static get properties() {
     return {
       language: {
         value: 'en'
       },
       resources: {
         value: function() {
           return {
             'en': { 'hello': 'My name is {name}.' },
             'fr': { 'hello': 'Je m\'apelle {name}.' }
           }
         }
       }
     }   
   }
   
   connectedCallback() {
     super.connectedCallback();  
     this.loadResources(this.resolveUrl('locales.json'));
   }
 }
 customElements.define(XApp.is, XApp);
 &lt;/script>
 </dom-module>

Fallback Language

This language is used when no translation is found for default language.

Stuctured Files

The resouce file can also have a deep structure for instace:

"en": {
  "welcome": {
    "polite": "Welcome Sir!",
    "normal": "Welcome!"
  }
}

You can access these structures by extending you translation key with :.

<div>{{localize('welcome:polite')}}</div>

Language Changed Event

Using this component in an Structured Polymer app it can get anoying to bind language property downwards. Alternatively to oneway downwards binding you can fire an event that will be caugt by this behavior.

//change language
this.dispatchEvent(new CustomEvent('app-localize-language-changed'), {detail: {language: 'fr'}})
//change fallbackLanguage
this.dispatchEvent(new CustomEvent('app-localize-fallback-language-changed'), {detail: {fallbackLanguage: 'fr'}})

Region fallback

This repo wraps Format.js which means you should use a Language Key specific to a region to have everything formated for that region for instance en_US and en_UK. But this can get out of hands very quickly. Thats why this behavior fallsback to the language when region specific does not provide a result. With this you can only use region in your locales.json when needed:

"en": {
  "welcome": "Welcome" 
},
"en_US": {
  "color": "color" 
}
"en_UK": {
  "color": "colour" 
}
language = 'en_US';
localize("welcome");// Welcome
localize("color"); // color
language = 'en_UK';
localize("welcome"); // Welcome
localize("color"); // colour

app-localize-behavior's People

Contributors

abendigo avatar cadwmaster avatar darksideofmo0n avatar dfreedm avatar dgabrielli76 avatar e111077 avatar felixzapata avatar jab avatar josefjezek avatar joshuaklassen avatar monapochi avatar mvolkmann avatar notwaldorf avatar tedium-bot avatar toshovski avatar

Forkers

abassoftware

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.