hablar
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

hablar.js

A JavaScript library useful for compiling i18n translations written in a DSL to JavaScript functions.

This is very much still a work in progress

With an API that is to be considered unstable.

DSL

The DSL is defined in JISON files (see src/parsers/grammars). But to illustrate the key parts of the language here is an example:

Hello $name, it has been {{formatDate($timeSinceLastVisit, "months"}} since your last visit.

This would roughly translate into the following javascript function:

function(parameters, functions) {
	return 'Hello ' + parameters.name + ', it has been ' + functions['formatDate'](parameters.timeSinceLastVisit, 'months') + ' since your last visit.';
}

It is worth noting that the actual function generated includes various rules regarding escaping output, that complicates the actual function.

Constraints

Sometimes we need to use a different translation string based on the input variables. Think situations where a you need to show the number of items in a shopping basket. If there are 0 items in the basket we might want to show something like You have no items in your basket.. If there is 1 item we might want to show You have one item in your basket., and otherwise You have $n items in your basket.

This is supported by adding constraints to the variables, basically allowing a series of if-statements to be generated inside the function.

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i hablar

    Weekly Downloads

    211

    Version

    0.2.1

    License

    Apache-2.0

    Unpacked Size

    248 kB

    Total Files

    116

    Last publish

    Collaborators

    • kve-orbit-online
    • jsn-orbit-online
    • khs-orbit-online
    • msc-orbit-online
    • renedalsgaard
    • bkc-orbit-online
    • bba-orbit-online
    • ops-orbit-online
    • secoya-as
    • aim-orbit-online
    • secoya-ansible