claudetech / node-static-i18n

HTML static pages i18n tool
https://www.npmjs.com/package/static-i18n
MIT License
104 stars 21 forks source link

node-static-i18n Build Status

A simple utility to translate static HTML files.

Supports JSON and YAML dictionaries.

Installation

For global installation, needed for CLI uage, run

$ npm install -g static-i18n

Note that you can also use this as a Grunt plugin or a gulp plugin.

Example

A simple example should be easy to understand.

With the following files:

www/index.html:

<html>
  <head>
    <script src="https://github.com/claudetech/node-static-i18n/raw/master/js/app.js"></script>
  </head>
  <body>
    <h1 data-t="my.key"></h1>
    <p data-t>other.key</p>
    <input type="submit" data-attr-t value-t="other.ok">
  </body>
</html>

locales/en.json

{
  "my": {
    "key": "Hey"
  },
  "other": {
    "key": "man",
    "ok": "confirm"
  }
}

locales/fr.json

{
  "my": {
    "key": "Salut"
  },
  "other": {
    "key": "mec",
    "ok": "confirmer"
  }
}

The following command

$ static-i18n -l en -i en -i fr www

will generate:

i18n/index.html

<html>
  <head>
    <script src="https://github.com/claudetech/node-static-i18n/raw/master/js/app.js"></script>
  </head>
  <body>
    <h1>Hey</h1>
    <p>man</p>
    <input type="submit" value="confirm">
  </body>
</html>

i18n/fr/index.html

<html>
  <head>
    <script src="https://github.com/claudetech/node-static-i18n/raw/master/./js/app.js"></script>
  </head>
  <body>
    <h1>Salut</h1>
    <p>mec</p>
    <input type="submit" value="confirmer">
  </body>
</html>

Interpolation

You can optionally specify that interpolation should be applied to the custom attributes on an element. This will translate only the parts of an element wrapped in curly braces {{ }} and leave the rest alone.

With the following files:

www/index.html:

<html>
  <head>
    <link rel="canonical" data-attr-t data-attr-t-interpolate href-t="{{links.baseAbsolute}}filename.{{links.extension}}" />
  </head>
</html>

locales/en.json

{
  "links": {
    "baseAbsolute": "http://www.example.com/",
    "extension": "html"
  }
}

locales/ja.json

{
  "links": {
    "baseAbsolute": "http://www.example.com/ja/",
    "extension": "htm"
  }
}

The following command

$ static-i18n --locale en --locales en --locales ja www

Will generate:

i18n/index.html

<html>
  <head>
    <link rel="canonical" href="http://www.example.com/filename.html" />
  </head>
</html>

i18n/ja/index.html

<html>
  <head>
    <link rel="canonical" href="http://www.example.com/ja/filename.htm" />
  </head>
</html>

Configuration

This tool has several configuration options to adapt to most common use cases.

When using the CLI, these options can be passed by using the kebab-case version of the key. For example. attrSelector can be passed with

static-i18n --attr-selector my-attr-t ...

When using the CLI, outputOverride and i18n options are parsed as JSON.