npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jed-gettext-parser

v2.0.0

Published

JavaScript Gettext .mo file parsing for Jed.

Downloads

486

Readme

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.