Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i18n-behavior

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18n-behavior

Instant and Modular I18N framework for lit-html and Polymer

  • 3.0.0-pre.24
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35
decreased by-76.82%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Coverage Status npm Published on webcomponents.org

i18n-behavior

Instant and Modular I18N framework for lit-html and Polymer

API Docs, Demo

Conceptual Workflow

Browser Compatibility

Polymer 3.x & lit-html with the help of i18n-element

BrowserChromeFirefoxEdge 13+IE 11Safari 9+Chrome AndroidMobile SafariOpera
Supported
  • Polyfilled by @webcomponents/webcomponentsjs/webcomponents-{bundle|loader}.js

Install

    npm install i18n-behavior

Quick Tour with I18N-ready pwa-starter-kit

Import

    import { I18nBehavior } from 'i18n-behavior/i18n-behavior.js'

Usage

Run-time Automatic I18N (for development)

Apply BehaviorsStore.I18nBehavior or imported I18nBehavior for run-time automatic I18N.

Source Code:
  • Note: i18n-element package wraps i18n-behavior to support lit-html and Polymer class syntax
    // Legacy Polymer syntax
    Polymer({
      importMeta: import.meta,
      is: 'custom-element',
      _template: html`
        <span id="label">UI text label</span> <!-- no need to touch UI text strings -->
      `,
      behaviors: [
        I18nBehavior // Add this line for I18N
      ]
    });
I18N-ready preprocessed DOM at element registration:

Hard-coded UI text strings are automatically extracted and replaced with annotations bound to text object.

lang attribute specifies the current locale. By default, <html lang> attribute is observed and mirrored to those for I18N-ready element instances.

    <html lang="en"><!-- html lang attribute is observed and mirrored -->
      ...
      <custom-element lang="en">
        <span id="label">{{text.label}}</span><!-- UI texts are bound to text object -->
      </custom-element>
      ...
    </html>

If the containing element of the target text has id attribute, the string id is named with the id value. If not, the string id is automatically generated. It is recommended to put meaningful id to each string for robustness. When attaching id attribute is too much for the containing element, text-id attribute can be used instead.

    <span text-id="label">{{text.label}}</span>
text dynamic property:

this.text dynamic object property represents an object with UI text strings for the current locale.

    this.text = {
      "label": "UI Text Label"
    }

this.text dynamic object is SHARED among all the instances of the same custom element.

model dynamic property:

this.model is deepcopied from this.text.model per instance to store I18N target attribute values. UI text strings in I18N target attributes are automatically extracted and replaced with annotations according to the shared repository (i18n-attr-repo) of I18N target attributes per elements (like placeholder attribute of input element).

On lang-updated event, this.text.model is updated but this.model is NOT automatically updated and needs explicit update like this.

    listeners: {
      'lang-updated': '_langUpdated'
    },

    _langUpdated: function (event) {
      if (Polymer.dom(event).rootTarget === this) {
        this.model = deepcopy(this.text.model);
      }
    }
<json-data> for manual text definitions

Optionally, any JSON data can be manually added to I18N target strings via <json-data> element. This option is effective for manual extraction of hard-coded UI text strings in JavaScript literals.

  <dom-module id="my-element">
    <template>
      ... <!-- ordinary template for rendering -->
      <template><!-- containing template element to avoid rendering -->
        <json-data id="items">[
          "Responsive Web App boilerplate",
          "Iron Elements and Paper Elements",
          "End-to-end Build Tooling (including Vulcanize)",
          "Unit testing with Web Component Tester",
          "Routing with Page.js",
          "Offline support with the Platinum Service Worker Elements"
        ]</json-data>
        <json-data id="sender">{ "name": "Sam", "gender": "male" }</json-data>
      </template>
    </template>
    <script>
    ...
    this.text.items[0] === 'Responsive Web App boilerplate'
    this.text.sender.name === 'Sam'
    ...
    </script>
  </dom-module>
Localized text strings fetched from JSON:

While default text strings are extracted from the hard-coded strings in HTML template, localized text strings are asynchronously fetched from JSON files under locales directory at the server.

    /bundle.json
    /locales/bundle.ja.json
            /bundle.fr.json
            /bundle.zh-Hans.json
    
    /elements/my-list/my-list.json
                     /locales/my-list.ja.json
                             /my-list.zh-Hans.json
    
             /google-chart-demo/google-chart-demo.json
                               /locales/google-chart-demo.ja.json
                                       /google-chart-demo.fr.json

Build-time Automatic I18N (for production)

gulp-i18n-preprocess filter performs build-time automatic I18N and embeds UI texts as JSON.

I18N-ready Source Code preprocessed by gulp-i18n-preprocess:

    <dom-module id="custom-element">
      <template localizable-text="embedded">
        <span id="label">{{text.label}}</span>
        <template id="localizable-text">
          <json-data>{
            "label": "UI Text Label"
          }</json-data>
        </template>
      </template>
    </dom-module>

Default text values are immediately extracted from the embedded JSON without overheads of run-time traversal into the whole template.

Compatible Versions

i18n-behaviori18n-elementPolymerlit-html
3331
221-2-
1-1-

Quick Tour

Quick deployment of I18N-ready pwa-starter-kit

    npm install -g polymer-cli
    git clone https://github.com/t2ym/pwa-starter-kit
    cd pwa-starter-kit
    npm ci
    # Add Locales
    gulp locales --targets="de es fr ja zh-Hans"
    # I18N Process
    gulp
    # Translate XLIFF ./xliff/bundle.*.xlf
    # Merge Translation
    gulp
    # Dev build on http://localhost:8080
    polymer serve
    # Static build
    polymer build
    # Static build on http://localhost:8080
    cd build/{esm-unbundled|esm-bundled|es6-bundled|es5-bundled}
    python -m SimpleHTTPServer -p 8080

Changelogs

Next Release 3.0.0

ModulePackagerVersionDescription
i18n-elementnpm3.0.0I18N base element class
i18n-behaviornpm3.0.0Run-time I18N handler
i18n-formatnpm3.0.0I18N text formatter
i18n-numbernpm3.0.1I18N number formatter
gulp-i18n-preprocessnpm1.2.3Build-time I18N preprocessor
gulp-i18n-leveragenpm1.1.4L10N JSON updater
gulp-i18n-add-localesnpm0.1.1L10N JSON placeholder generator
xliff-convnpm/bower1.0.12XLIFF/JSON converter
live-localizernpm3.0.0L10N widget

Stable Release 2.0.0

  • Hybrid support of Polymer 1.x/2.x in the legacy syntax
  • i18n-element I18N base element class for Polymer 2.x
  • live-localizer L10N widget
ModulePackagerVersionDescription
i18n-elementnpm/bower2.0.0I18N base element class
i18n-behaviornpm/bower2.0.0Run-time I18N handler
i18n-formatnpm/bower2.0.0I18N text formatter
i18n-numbernpm/bower2.0.2I18N number formatter
gulp-i18n-preprocessnpm1.2.3Build-time I18N preprocessor
gulp-i18n-leveragenpm1.1.3L10N JSON updater
gulp-i18n-add-localesnpm0.1.0L10N JSON placeholder generator
xliff-convnpm/bower1.0.10XLIFF/JSON converter
live-localizernpm/bower2.0.1L10N widget (WIP)

Stable Release 1.1.0

ModulePackagerVersionDescription
i18n-behaviorbower1.1.0Run-time I18N handler
i18n-formatbower1.0.0I18N text formatter
i18n-numberbower1.0.1I18N number formatter
gulp-i18n-preprocessnpm1.1.0Build-time I18N preprocessor
gulp-i18n-leveragenpm1.0.13L10N JSON updater
gulp-i18n-add-localesnpm0.1.0L10N JSON placeholder generator
xliff-convnpm/bower1.0.1XLIFF/JSON converter

Stable Release 1.0.0

  • Instant I18N by one line addition of I18nBehavior
  • Minimal or no overhead for development: Run-time automatic extraction of hard-coded UI text strings from HTML templates
  • Optimal for production: Build-time automatic extraction and bundling of hard-coded UI text strings from HTML templates by gulp-i18n-preprocess preprocessor
  • Modular (per element) JSON support for storing and fetching localized UI text strings
  • Bundled (per app) JSON support for storing and fetching localized UI text strings
  • Automatic application of <i18n-format> with Unicode CLDR plural rules and Gender support
  • Polymer 1.2.0's Compound Bindings support with <i18n-format>
  • i18n-dom-bind template instead of dom-bind for instant I18N of bound templates
  • Dynamic on-demand fetching of localized UI text strings from JSON under locales directories
  • Real-time observation of <html lang> attribute value for UI text localization
  • Robust fallback of missing UI text strings to parent locales and finally to the default locale (e.g. "fr-CA" -> "fr" -> "en") with practical BCP47 support
  • this.text dynamic object shared among the same custom element to access localized strings
  • this.model object deepcopied from this.text.model object per instance to access localized attribute strings
  • i18n-attr-repo to maintain repository of I18N target attributes
  • gulp-i18n-leverage filter to merge changes in the default language in HTML templates into localized JSON resources.
  • gulp-i18n-leverage filter to put meta infomation, that is, L10N "TO DO" list, for the merged changes in JSON resources
  • Option to define I18N target strings manually by <json-data> elements
ModulePackagerVersionDescription
i18n-behaviorbower1.0.0Run-time I18N handler
i18n-formatbower1.0.0I18N text formatter
i18n-numberbower1.0.0I18N number formatter
gulp-i18n-preprocessnpm1.0.0Build-time I18N preprocessor
gulp-i18n-leveragenpm1.0.0L10N JSON updater
gulp-i18n-add-localesnpm0.1.0L10N JSON placeholder generator

License

BSD-2-Clause

Keywords

FAQs

Package last updated on 02 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc