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

i18n-abide

v0.0.26

Published

Express/connect module for Node i18n and l10n support

Downloads

2,631

Readme

i18n-abide

This module abides by the user's language preferences and makes it available throughout the app.

This module abides by the Mozilla L10n way of doing things.

The module abides.

Status

Used in production systems, such as the Mozilla Persona service in 40+ languages.

Also used on other websites including:

  • Mozilla Webmaker

Supported Localization Technologies

This module supports several localization backends:

  • Gettext PO files (default and documented below)
  • Plist files
  • Transifex key-value-JSON files

This module supports client side as well as server side localization.

Usage

npm install i18n-abide

In this README, we'll use express and EJS templates, but other integrations are possible.

In your app where you setup express:

var i18n = require('i18n-abide');

app.use(i18n.abide({
  supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
  default_lang: 'en-US',
  debug_lang: 'it-CH',
  translation_directory: 'i18n'
}));

This block sets up the middleware and views with gettext support. We declare support for English, German, Spanish, and two debug locales (more on this later).

In your routes, you can use the gettext function in .js files.

exports.homepage = function(req, resp) {
  resp.render('home', {title: req.gettext("Hey, careful, man, there's a beverage here!")});
};

In your layout files, you can add

<!DOCTYPE html>
<html lang="<%= lang %>" dir="<%= lang_dir %>">
  <head>
    <meta charset="utf-8">
    ...

In your templates files, you can use the gettext function in .ejs files:

<p><%= gettext("This will not stand, ya know, this aggression will not stand, man.") %></p>

i18n-abide also provides a format function for string interpolation.

This module provides both server side translations and client side translations. Server side works out of the box and is the most common use case.

If you also want to do client-side translations, i18n-abide provides lib/gettext.js and you can do the same in .js and .ejs files.

Setting Language via HTTP Header

The i18n-abide module uses the accept-language HTTP header to determine which language to use.

See API docs for overriding this via URL or the API directly.

Translation files

The i18n-abide module currently supports three file formats.

  1. PO/POT files, which get transformed to JSON via provided command line tools.

  2. PLIST (i.e., XML) files, which require no transformation prior to use.

  3. Transifex JSON (JavaScript Object Notation) a key-value JSON type, which require no transformation prior to use.

PO/POT files

This is the default and assumed for documentation in this README.

PO files can be compiled to .json or Gettext binary .mo files.

For use on the client side, PO files are compiled to JavaScript for easy inclusion into your page or build script.

NOTE: The PO/POT files are also transformed into .JSON, but do not follow the same layout as the Transifex JSON files.

See GETTEXT.md for more details.

Other file formats

See API for configuration and details around using Plist or Transifex localization files.

Debugging and Testing

db-LB is a special debug locale. To trigger it, set your Browser or Operating System language to Italian (Switzerland) which is it-CH. This fake locale db-LB will be triggered, it is David Bowie speak for the region of Labyrinth.

Oh, hell ya a "The Dude" / Bowie Mashup. That just happened.

Now, start up your Node server and visit a page you've wrapped strings in Gettext...

Tutorial

Mozilla Hacks blog has a three part introduction.

Docs

  • See USAGE for full details.
  • API docs has more advanced config options and APIs
  • GETTEXT documents how to use PO/POT files