GithubHelp home page GithubHelp logo

i18n-msg's Introduction

i18n-msg web component

Build Status

Custom element to do localization (i18n) message string replacement.

See the component page for more information.

i18n-msg's People

Contributors

damiangarro avatar ebidel avatar matthewlawson avatar roxus avatar soonick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

i18n-msg's Issues

My try to extend the concept

Update
As mentioned further down the thread, I created a new project called i18-n because I work with it in a quite big project and changes are also quite massive.

Just updating this first message to redirect you right away so you don't accidentally waste time on old stuff (aka the branch in my i18n-msg fork).

Cheers


Maybe you're interested in giving this a look and providing some feedback.
Kinda forward of me, sooo... sorry.

I have a new branch in my fork of your project called i18n-msg-domain

I tried to expand the hole i18n-msg stuff and created an additional i18n-msg-domain component.
It make it possible to declaratively define the location of the locale files, what language to use and additionally the ability to define language domains. The part of loading the locale files is moved over to the new component.

So now we can throw multiple i18n-msg-domain elements on the page, for different languages and even to target different sets of i18n-msg elements (by using the language domain).
i18n-msg was also modified big time by me to make it work together with the new i18n-msg-domain.

I also added HTML sanitizing (I created an element/behavior called dom-purify for this).

A demo is in the component page under the demo area so you can try it out quickly.

So if you have to time to look into this, I would be more than thankfull.

i18n-msg doesn't work in FF and Edge.

I've tested this component in FireFox (41.0.2) and Edge (20.10240) and noticed that language doesn't switch. Also when i've put it into another component it wasn't displayed.

console error if I18nMsg.url changed (still goes to default locales url?)

Just checking, shouldn't this NOT be happening? I mean the following error:

screen shot 2015-09-11 at 21 28 38

since I'm setting the I18nMsg.url to a custom one (just outside "../locales") and this new url is working because it's translating everything (meaning, it's correctly finding the locales folder etc.)

And still getting that error (I guess because it's trying to find the locales from the default location even though a new url was used?)

Bind to attributes

Is there ability to bind an i18n-msg to attribute? For instance:

<paper-dropdown-menu label="{{change-lang | i18n-msg}}" relative layout inline horizontal center tabindex="0">

Where change-lang is msgid id.

Use element content as msgid

What are your thoughts on this? Wouldn't it be more convenient to use the inner content of the element and get rid of the msgid attribute / make the msgid optional.

About attributes

Hello ebidel,

Thx for this component.

We use it in our components but we have a problem with the attributes. For example, how to translate the label of the paper-input ?

I try two solutions.

  • The first one is without change anything in i18n-msg :
<paper-input label="[[computeI18n('msgId', i18n)]]" value="[[value1]]"></paper-input>
this.computeI18n = (msgid, i18n) => i18n.getMsg(msgid);

This solution is ok but I have to pass the i18n element to the compute ! So I have to create the statement of i18n :

 attached() {
    /** Append at least one i18n-msg. */
    let i18nLocal = document.querySelector('i18n-msg');
    if (i18nLocal === null) {
      i18nLocal = document.createElement('i18n-msg');
      Polymer.dom(this.root).appendChild(i18nLocal);
    }
    i18nLocal.addEventListener('i18n-language-ready', () => {
      this.i18n = i18nLocal;
      this.currentLanguage = this.i18n.language;
    });
    let userLang = navigator.language || navigator.userLanguage;
    if (I18nMsg.lang !== userLang) {
      I18nMsg.lang = userLang;
      I18nMsg.url = 'locales';
    }
  },
  • My second solution is very simple :
<i18n-msg msgid="notices.name.capitalize" last-msg="{{lastMsg}}"></i18n-msg>
<paper-input readonly label="[[lastMsg]]" value="[[authorityIdentity.name]]"></paper-input>

I add lastMsg in your component like this:

lastMsg: {
  type: String,
  value: null,
  notify: true
}
...
instance.lastMsg = msg.message;

Have you a more usable solution for this case ?

Julien

Add support for placeholders

I need translated strings with placeholders, so I looked at the i18n chrome spec(https://developer.chrome.com/extensions/i18n-messages) and created this based on it:
#25

I'm trying to follow the spec without implementing all of it so it can probably be extended in the future.

Currently there are two ways we can use placeholders:

Placeholders in the json file

{
  "bye": {
    "message": "Come back to $our_site$ soon!",
    "placeholders": {
      "our_site": {
        "content": "Example.com",
      }
    }
  }
}

These work pretty much as documented by chrome. We have a placeholders attribute that contains the placeholders. In this case there is one called our_site with a content of Example.com. When we find this placeholder we can do a simple string replacement. Replace $our_site$ with with the content of the our_site placeholder.

Placeholders in a component attribute

{
  "bye": {
    "message": "Come back to $our_site$ soon!",
    "placeholders": {
      "our_site": {
        "content": "$1",
      }
    }
  }
}

In this case the content of the placeholder is $1. In this case I am currently just checking that the first character is a $. If it is then I assume you are looking for an attribute. In this example, since it is $1 it will look for the first placeholder passed to the component($2 is the second, and so on). The placeholders are passed as an array in an attribute called placeholders:

<i18n-msg msgid="seconds" placeholders='["Example.com"]'>PLACEHOLDER_STRING</i18n-msg>

The result in both cases is: Come back to Example.com soon!

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.