Internationalization support in Erlang
Erlang
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
priv
src
.gitignore
.travis.yml
LICENSE
Makefile
README.md
TODO
ei18n.mk
rebar.config

README.md

ei18n

Internationalization support in Erlang.

This project is an alternative to https://github.com/etnt/gettext. The difference is that it implements an ability to use XML/SQL as an internationalization source and generate efficient implementation where partial internationalization information can be pre-compiled into bytecode based on the static or dynamic attribute of text items.

Format of the XML file

A sample XML file is shown below:

001 <translations default-lang="en" version="1.0">
002 <lang iso="en">
003    <text type="static" name="HELLO">Hello!</text>
004    <text value="Value example1"/>
005    <text>Value example2</text>
006 </lang>
007 <lang iso="ru">
008    <text name="HELLO">Привет!</text>
011    <text name="VALUE_EXAMPLE1" value="Пример значения"/>
012 </lang>
013 </translations>
  • <translations> node defines:
    • default-lang - ISO code of the default language
    • version - version of the XML file
  • <lang> node defines:
    • iso - ISO code of the language that the following text nodes represent
  • <text> node defines a text blob in a given language.
    • name - is the identifier of the blob. If absent, it's generated from the blob's content by uppercasing, and replacing spaces and hyphens with underscores.
    • type - represents blob's storage type. static implies that the blob should be looked up statically by being embedded in bytecode. dynamic means that it should be loaded to the translation server and looked up dynamically.
    • value - can optionally define the blob's content (see line 4 above). If absent the content must be defined in the node's text (see line 5).

Every text node specified in the non-default language section must have name attributes with ID's found in the default language section.

In the example above the following header file will be generated:

ei18n.hrl (generated by ei18n_xml_generate.erl)
=========
-define(HELLO, 1).

and additionally the following code files will be produced:

ei18n.erl (generated by ei18n_xml_generate.erl)
=========
get(ID, en) -> ei18n_en:get(ID);
get(ID, ru) -> ei18n_ru:get(ID).

ei18n_en.erl (generated by ei18n_xml_generate.erl)
============
get(?HELLO) -> <<"Hello!">>;
get(ID)     -> ei18n_trans_server:get(ID, en).

ei18n_ru.erl (generated by ei18n_xml_generate.erl)
============
get(?HELLO) -> <<"Привет!">>;
get(ID)     -> ei18n_trans_server:get(ID, ru).

The ei18n_trans_server.erl translation server will resolve the following internationalization blobs dynamically:

ei18n_trans_server:get(<<"VALUE_EXAMPLE1">>, en) -> <<"Value example1">>.
ei18n_trans_server:get(<<"VALUE_EXAMPLE2">>, en) -> <<"Value example2">>.
ei18n_trans_server:get(<<"VALUE_EXAMPLE1">>, ru) -> <<"Пример значения">>.

Generating ei18n* module targets from XML spec

  1. Include ei18n dependency in the rebar config file:

     {deps, [{ei18n, ".*", {git, "git://github.com/saleyn/ei18n.git", "master"}}]}.
    
  2. Add the following configuration options to rebar.config:

     {plugin_dir, "deps/ei18n/priv"}.
     {plugins, [ei18n_rebar_plugin]}.
     {ei18n_opts, [{xml_file, "path/to/specification/file.xml"}]}.
    

This will result in creation of the following files after running rebar compile:

* include/ei18n.hrl
* src/ei18n.erl
* src/ei18n_{LANG}.erl  (for each {LANG} defined in the XML specifications file)