Git Product home page Git Product logo

jed-gettext-parser's Introduction

Jed Gettext Parser

JavaScript Gettext .mo file parsing for Jed.

CI

Introduction

Gettext is an old translation standard with implementations in many languages. It's one that localisation-aware programmers and translators are likely to be familiar with.

Jed provides a very nice interface for translation using Gettext in Javascript.

Jed doesn't supply Gettext translation file parsers, so this library can act as the bridge between Gettext binary files and Jed.

Note: Jed Gettext Parser is made to work with Jed, but is a third-party library. Please direct any support queries to this repository's issue tracker, and the author.

Install

Jed Gettext Parser can be loaded as a browser global, an AMD module, or in Node. It requires support for:

Node.js supports both since version 11.0.0.

Browser Global
<script src="jedGettextParser.js"></script>
<script>
// Use jedGettextParser
</script>
AMD Module
require(['jedGettextParser'], function(jedGettextParser) {
    // Use jedGettextParser
});
Node
npm install jed-gettext-parser
var jedGettextParser = require('jed-gettext-parser');
// Use jedGettextParser

Usage

Once you've loaded Jed and Jed Gettext Parser, they can can be used together:

var moBuffer = new ArrayBuffer();
// Fill the moBuffer with the contents of a .mo file in whatever way you like.

// locale_data is an object holding locale data as expected by Jed.
var locale_data = jedGettextParser.mo.parse(moBuffer);

// Now load using Jed.
var i18n = new Jed({
    'locale_data': locale_data,
    'domain': 'messages'
});

API

The library currently exposes only one function:

var data = jedGettextParser.mo.parse(buffer[, options]);
  • data: an object that can be used as the value of Jed's locale_data initialisation option.
  • buffer: an ArrayBuffer object that holds the contents of the .mo file to parse.
  • options: an object that can be optionally provided to specify some settings.

The options object has the following structure (default values given):

var options = {
    encoding: undefined,
    domain: 'messages'
}
  • encoding: The encoding to use when reading the .mo file. If undefined, the encoding given in the .mo file will be used. Otherwise, valid values are those given in the Encoding API specification.
  • domain: The domain under which the translation data should be stored.

If an issue is encountered during parsing, an Error object describing the problem will be thrown.

Motivation

There are two types of Gettext translation files: the .po files contain human-readable text that can be easily edited by translators, and the .mo files contain equivalent binary data. Some Gettext implementations use one, the other, or both.

While developing a Chromium Embedded Framework-based application (LOOT) which required localisation of strings in the C++ and the Javascript code, I decided that parsing the .mo localisation files in each language separately was the neatest and simplest way of achieving this. The only Javascript .mo file parser I could find was gettext-parser, and it's Node-only, so I wrote this little library.

I used gettext-parser to cross-check my understanding of the Gettext mo file spec, and as inspiration for this library's API, so thanks to Andris Reinman for writing it.

jed-gettext-parser's People

Contributors

greenkeeper[bot] avatar ortham avatar sinewyk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jed-gettext-parser's Issues

Document library error handling

At the moment, the library is pretty rough-and-ready: it works for me, and it doesn't really have a lot of error handling.

Improving the error handling will naturally occur as I work through #2, but I should also document the library's behaviour, so I'm opening this issue to remind myself to do that.

Document Node.js support

I've added support for loading this library as a Node.js module as part of #2, but it's not yet documented anywhere.

False negatives for Sauce Labs browser tests

I don't know why it's happening, but there seems to be some problem with the Sauce Labs browser tests for iOS 8.1 and Android 4.1 and 4.4. The Android tests themselves pass, but there's some issue with the testing system which means they appear to fail, and the iOS 8.1 tests won't even start.

I'm not sure that there's anything I can do about it, but I thought I might as well open an issue so that there is an explanation somewhere.

Continuous Integration with Travis CI

Those "build passing" badges are neat, I want one. At the moment, I have Travis CI integrated, but there aren't any tests for it to run. I would also like one of those browser support tables, but Travis doesn't do them and now that I'm trying to find out who does, I can't seem to spot a single one of those tables to click on...

Although this is intended as a browser module, Travis only provides testing for Node.js JS, so I added a package.json that pulls in the text-encoding polyfill, which should allow this library to run under Node.js.

The next step is to add testing. Mocha looks good for doing that, together with should.js. I need to find some gettext .mo files for a variety of languages in different encodings, with plurals and contexts, to test with.

Update for next Jed release

The required code changes were made in #6, but they rely on the changes submitted in messageformat/Jed#33, so package.json and test/get-files.sh were altered to point to the relevant fork.

When that Jed PR gets merged and another Jed release made, package.json and test/get-files.sh should be updated to point to the new Jed version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.