gettext.js Documentation

Similar documents
gettext.js Documentation

translationstring Documentation

turbo-hipster Documentation

TangeloHub Documentation

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Tornado-Babel Documentation

Indium Documentation. Release Nicolas Petton

Bitdock. Release 0.1.0

polib Documentation Release David Jean Louis

Lab 1 - Introduction to Angular

Django-CSP Documentation

Quick.JS Documentation

PROCE55 Mobile: Web API App. Web API.

django-sticky-uploads Documentation

Brunch Documentation. Release Brunch team

kaleo Documentation Release 1.5 Eldarion

There are two main workflows for working with Cordova projects, Web focused and Platform focused.

youckan Documentation

Getting started with Tabris.js Tutorial Ebook

The Lokalize Handbook. Nick Shaforostoff

pynojo Documentation Release unknown pynojo development team

Pulp Python Support Documentation

ALeF: Active Learning Framework for Readability Prediction

Stepic Plugins Documentation

Archan. Release 2.0.1

Django Dynamic Fields Documentation

g-pypi Documentation Release 0.3 Domen Kožar

APIs - what are they, really? Web API, Programming libraries, third party APIs etc

Tornado-Babel Documentation

BanzaiDB Documentation

Chapter 1 - Development Setup of Angular

How to Install (then Test) the NetBeans Bundle

PyCPUID Documentation

mongodb-tornado-angular Documentation

Enabling High-Quality Printing in Web Applications. Tanu Hoque & Craig Williams

Frontend UI Training. Whats App :

Package poio. R topics documented: January 29, 2017

Django PAM Documentation

IBM Image-Analysis Node.js

Web Development for Dinosaurs An Introduction to Modern Web Development

Cordova - Guide - Custom Plugins

Installing Design Room ONE

ZeroVM Package Manager Documentation

Cisco Spark Widgets Technical drill down

vsphere APIs and SDKs First Published On: Last Updated On:

dh-virtualenv Documentation

KonaKart Shopping Widgets. 3rd January DS Data Systems (UK) Ltd., 9 Little Meadow Loughton, Milton Keynes Bucks MK5 8EH UK

Flask-Migrate Documentation. Miguel Grinberg

Design Document V2 ThingLink Startup

django-baton Documentation

Xcode Encountered An Internal Logic Error >>>CLICK HERE<<<

Django-Select2 Documentation. Nirupam Biswas

Moodle Destroyer Tools Documentation

INF5750. Introduction to JavaScript and Node.js

Getting Started with ReactJS

Ampliación de Bases de Datos

pyramid_assetmutator Documentation

cget Documentation Release Paul Fultz II

Django Graphos Documentation

IN4MATX 133: User Interface Software

Unifer Documentation. Release V1.0. Matthew S

Introduction Introduction Architecture Overview LAMP Server Limesurvey Custom Made Development... 3

P a g e 1. Danish Technological Institute. Scripting and Web Languages Online Course k Scripting and Web Languages

Pusher Documentation. Release. Top Free Games

Django File Picker Documentation

GAVIN KING RED HAT CEYLON SWARM

Full Stack boot camp

maya-cmds-help Documentation

Symantec Endpoint Virtualization 6.1 SP8 Release Notes

Composer Best Practices Nils Private Packagist

Web API Lab. The next two deliverables you shall write yourself.

transliteration Documentation

djangotribune Documentation

Installing Design Room ONE

ThingLink User Guide. Andy Chen Eric Ouyang Giovanni Tenorio Ashton Yon

XPRNLS command tool and library

Building a Django Twilio Programmable Chat Application

AngularJS Intro Homework

django-session-security Documentation

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

python Adam Zieli nski May 30, 2018

django-app-metrics Documentation

JavaScript Fundamentals_

Installing Design Room ONE

DNS Zone Test Documentation

Node.js I Getting Started

Angular 2 Programming

pytest-benchmark Release 2.5.0

IBM LOT-408. IBM Notes and Domino 9.0 Social Edition Application Development Updat.

Management Tools. Management Tools. About the Management GUI. About the CLI. This chapter contains the following sections:

Network Programmability with Cisco Application Centric Infrastructure

Need to Node: Profiling Node.js Applications

55249: Developing with the SharePoint Framework Duration: 05 days

React(.js) the Domino Way High-Performance Client for Domino. Knut Herrmann

Enabling High-Quality Printing in Web Applications

django-dajaxice Documentation

django-cron Documentation

Lab 6: Testing. Software Studio DataLab, CS, NTHU

CodeHub. Curran Kelleher 8/18/2012

termite Release 0.0.2

Transcription:

gettext.js Documentation Release 1.0 Jonas Obrist Jul 23, 2017

Contents 1 Installation 3 1.1 Dependencies............................................... 3 1.2 Installation................................................ 3 2 Usage 5 2.1 Workflow................................................. 5 2.2 Compiler................................................. 5 2.3 Runtime................................................. 6 3 Reference 9 3.1 CLI.................................................... 9 3.2 Javascript................................................. 9 4 Changelog 11 4.1 1.2.................................................... 11 4.2 1.1.................................................... 11 4.3 1.0.................................................... 11 5 Indices and tables 13 i

ii

gettext.js Documentation, Release 1.0 gettext.js provides a GNU gettext like interface for use in browsers. For better performance and compatibility, mo files are compiled into JSON or JavaScript files using a Python command line tool. These files can then be used in the browser to provide internationalization capabilities. While the tool to compile mo files to JavaScript or JSON is written in Python, the runtime has no Python dependencies. The JavaScript runtime can be used in either the browser or node. Contents 1

gettext.js Documentation, Release 1.0 2 Contents

CHAPTER 1 Installation Dependencies Compiler Python 3.5 or higher Javascript runtime No dependencies Installation Compiler pip3 install gettextjs Javascript runtime You can install gettext.js via npm or download the file from the dist folder in the repository. The npm package includes the jsnext:main key, so the library can be included via rollupjs and the node-resolve plugin if you re building an ES6 project. For node project, there is also a common-js file. If you wish to use gettext.js in the browser, make sure to use the gettext.iife.js file. 3

gettext.js Documentation, Release 1.0 4 Chapter 1. Installation

CHAPTER 2 Usage Workflow 1. Create your po files using your current workflow. 2. Create your mo files using your current workflow. 3. Use gettextjs to compile those mo files to.mo.js or.mo.json files. 4. Serve the output directory of the gettextjs call in your web app. 5. Use the Javascript runtime to translate your web app. The only thing that really changes from your current workflow is that after compiling the mo files, you then compile them into something that can be used in Javascript. Compiler Let s assume you have the following directory tree: /app/ - locale - en - LC_MESSAGES - messages.mo - messages.po The compiler takes a locale directory as created by gettext ( <locale>/lc_messages/<domain>.mo) as input, and writes the result to an output directory (<locale>/lc_messages/<domain>.mo.<json js>). This output directory should be served by your web server if you use json files. If you use js files you can host them anywhere you want, as you ll be responsible for loading them on your page. In it s most basic form, you simply use gettextjs /app/locale/. Your directory tree now looks like this: 5

gettext.js Documentation, Release 1.0 /app/ - locale - en - LC_MESSAGES - messages.mo - messages.mo.json - messages.po To instead generate JS files, use the --js flag. The JS files will have a single variable in them: <locale>_<domain>, with both of those values in upper-case. So the English messages.mo file will become a variable called EN_MESSAGES. For debugging, you can use the -v/--verbose flag for some more feedback on what is going on. --indent flag can be used to indent the JSON/JS file being generated. You may optionally provide a separate directory for the resulting compiled files. The -i/ Runtime This section assumes you have the locale directory containing the files compiled by gettextjs available at https://example.com/locale/. So for example the English messages.mo.json is available via https://example.com/locale/en/lc_messages/messages.mo.json. Loading (JSON) To load a catalog from a JSON file, use Gettext.load(), so for example to load the English messages domain, you would use the following: Gettext.load('https://example.com/locale', 'en', 'messages').then(function(gettext){ // start using gettext }, function(error){ // handle the error }); Loading (JS) Simply load the file via a script tag: <script src="https://example.com/locale/en/lc_messages/messages.mo.js"></script> Using gettextjs exposed two methods on the Gettext() class: gettext() and ngettext(). They re equivalent to gettext(3) and ngettext(3). gettext() takes a single msgid and returns the translation for it, if it finds one, or the msgid. ngettext() is used for translations which may have plurals. Here s an example usage: Gettext.load('https://example.com/locale', 'en', 'messages').then(function(gettext){ gettext.gettext("hello world!"); gettext.gettext("i know %(number)s language", "I know %(number)s languages", 1); 6 Chapter 2. Usage

gettext.js Documentation, Release 1.0 }); gettext.gettext("i know %(number)s language", "I know %(number)s languages", 2); String interpolation gettext.js does not provide string interpolation. Use libraries like sprintf.js to do this. When doing string interpolation, make sure you call the approprate gettext.js function first. You should always use named arguments when internationalizing strings, as different languages may have different word orders. The example above would become: sprintf(gettext.gettext("i know %(number)s language", "I know %(number)s languages", 2), {'number': 2}); 2.3. Runtime 7

gettext.js Documentation, Release 1.0 8 Chapter 2. Usage

CHAPTER 3 Reference CLI locale_dir Location of your locale directory with your.mo files. Must be in the standard <locale>/lc_messages/ <domain>.mo structure. out_dir Output directory. Defaults to the same as locale_dir. -i, --indent Indent the generated JSON/JS. -v, --verbose Print what files have been compiled. --json Generate JSON files instead of JS files. Javascript class Gettext(catalog) gettext(msgid) Arguments msgid (string) Message ID to look up. Returns Translated string (or input string if not found). ngettext(msgid, msgid_plural, count) Arguments 9

gettext.js Documentation, Release 1.0 msgid (string) Message ID for the singular string. msgid_plural (string) Message ID for the plural string. count (number) Used to detect whether plural or singular form should be used. Returns Translated string (or one of the input strings if not found). set_catalog(catalog) Set a catalog as the currently active, global translation catalog. This can be either an instance of Gettext or the JSON object produced by the compiler. gettext(msgid) Arguments msgid (string) Message ID to look up. Returns Translated string (or input string if not found). Uses the catalog set by set_catalog() to translate a message ID. ngettext(msgid, msgid_plural, count) Arguments msgid (string) Message ID for the singular string. msgid_plural (string) Message ID for the plural string. count (number) Used to detect whether plural or singular form should be used. Returns Translated string (or one of the input strings if not found). Uses the catalog set by set_catalog() to translate a message ID. 10 Chapter 3. Reference

CHAPTER 4 Changelog 1.2 Release date: January 20, 2017 Removed eval from Javascript runtime. Use yarn for development Javascript dependencies. 1.1 Release date: September 24, 2016 Made the JavaScript runtime an NPM package. Removed the Gettext.load API. If you want to load catalogs via AJAX, please use the AJAX libary of your choice. Added global APIs set_catalog(), gettext() and ngettext(). Removed docopt as a dependency for the compiler. The compiler now only depends on Python 3.5. 1.0 Release date: Dec 25, 2015 Initial release. 11

gettext.js Documentation, Release 1.0 12 Chapter 4. Changelog

CHAPTER 5 Indices and tables genindex modindex search 13

gettext.js Documentation, Release 1.0 14 Chapter 5. Indices and tables

Index Symbols json gettextjs command line option, 9 -i, indent gettextjs command line option, 9 -v, verbose gettextjs command line option, 9 G gettext() (built-in function), 9, 10 Gettext() (class), 9 gettextjs command line option json, 9 -i, indent, 9 -v, verbose, 9 locale_dir, 9 out_dir, 9 L locale_dir gettextjs command line option, 9 N ngettext() (built-in function), 9, 10 O out_dir gettextjs command line option, 9 S set_catalog() (built-in function), 10 15