Skip to content

Repository files navigation

Gettext translator

Javascript gettext translations replacement to use with gettext/gettext. Use gettext/json to generate the json data.

Installation

npm install gettext-translator

Usage

Use the Json generator gettext/json library to export the translations to json:

useGettext\Loader\PoLoader;
useGettext\Generator\JsonGenerator;
//Load the po file with the translations$translations = (newPoLoader())->loadFile('locales/gl.po');
//Export to a json file
(newJsonGenerator())->generateFile($translations, 'locales/gl.json');

Load the json file in your browser

importTranslatorfrom'gettext-translator';asyncfunctiongetTranslator(){constresponse=awaitfetch('locales/gl.json');consttranslations=awaitresponse.json();returnnewTranslator(translations);}constt=awaitgetTranslator();t.gettext('hello world');//ola mundo

Variables

You can add variables to the translations. For example:

t.gettext('hello :who',{':who': 'world'});//ola world

There's also a basic support o sprintf (only %s and %d)

t.gettext('hello %s','world');//ola world

To customize the translator formatter, just override the format method:

t.format=function(text, ...args){//Your custom format here}

Short names

Like in the php version, there are the __ functions that are alias of the long version:

//Both functions does the samet.gettext('Foo');t.__('Foo');

API

Long nameShort nameDescription
gettext__Returns a translation
ngettextn__Returns a translation with singular/plural variations
dngettextdn__Returns a translation with domain and singular/plural variations
npgettextnp__Returns a translation with context and singular/plural variations
pgettextp__Returns a translation with a specific context
dgettextd__Returns a translation with a specific domain
dpgettextdp__Returns a translation with a specific domain and context
dnpgettextdnp__Returns a translation with a specific domain, context and singular/plural variations

About

Javascript gettext translator

Topics

Resources

Stars

24 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages