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

babel-plugin-django-gettext

v1.2.1

Published

Babel plugin that simplifies using Django's JavaScript gettext functions

Downloads

97

Readme

babel-plugin-django-gettext

This plugin simplifies gettext-based localization in your Django-backed JavaScript codebases, giving you the ability to use word wrapping, template literals, and string interpolation, avoiding the mess of overly-long strings and sprinkled interpolate() calls.

Installation

$ npm install babel babel-plugin-django-gettext
$ babel --plugins django-gettext script.js

If you're using TypeScript, you will want to reference the types by including the following in your TypeScript files or in a global.d.ts file:

/// <reference types="babel-plugin-django-gettext"/>

Usage

There are two ways to work with this enhanced gettext support:

  1. You can call functions (like _(...), gettext(...), ngettext(...), etc.) and pass in strings or template literals, as you did before, like:

    const str = _('Ready to have fun?');
  2. You can use tagged templates, which looks cooler. See?

    const str = _`Ready to have fun?`;

In either case, you'll get automatic interpolation and controlled whitespace support for free. We'll go over that in a minute.

Gettext functions

First, let's cover all the gettext functions that can be directly called:

  • _ (alias for gettext)
  • N_ (alias for ngettext)
  • gettext
  • gettext_raw
  • gettext_noop
  • gettext_noop_raw
  • ngettext
  • ngettext_raw
  • pgettext
  • pgettext_raw
  • npgettext
  • npgettext_raw

And here's what you can use for a tagged template literal:

  • _ (alias for gettext)
  • N_ (alias for ngettext)
  • gettext
  • gettext_raw
  • gettext_noop
  • gettext_noop_raw

Whitespace Rules

The _raw versions will preserve any and all whitespace. For example:

// So  this:
const str = gettext_raw('   This  has  some\n    whitespace\n    going on ');

// Or   maybe   this:
const str = gettext_raw(`   This  has  some
    whitespace
    going on `);

// Turns    into:
const str = gettext("   This  has  some\n    whitespace\n    going on ");

Pretty obvious. Works the way you're used to today.

The non-raw versions will split your text into paragraphs (separated by one or more blank lines), collapse down whitespace and trim all leading and trailing whitespace in each paragraph, and trim the resulting string:

// So  this:
const str = gettext("   This  has  some\n    whitespace\n      going on.\n\nOh and here's another paragraph.\n\n    ");

// Or   maybe   this:
const str = gettext(`   This  has  some
    whitespace
      going on.

    Oh and here's another paragaraph.

       `);

// Turns into:
const str = gettext("This has some whitespace going on.\n\nOh and here's another paragraph.");

Much cleaner, and probably want you usually want in your message files.

The non-raw versions make it really easy to carefully compose your message across multiple lines without affecting the resulting string. The raw versions are there if that whitespace matters.

Automatic Interpolation

Whenever you use a template literal containing referenced variables or expressions, an interpolate(...) call will be made for you.

For example:

const subject = 'world';

// These are equivalent:
const str = _`Hello ${subject}!`;
const str = gettext(`Hello ${subject}!`);

// That becomes:
var subject = 'world';
var str = interpolate(gettext("Hello %(subject)s!"),
                      {subject: subject},
                      true);

This even works for ngettext() calls:

const count = 2;
const str = ngettext(`The button was only clicked ${count} time!`,
                     `The button was clicked ${count} times!`,
                     count);

// That becomes:
var count = 4;
var str = interpolate(ngettext("The button was only clicked %(count)s time!",
                               "The button was clicked %(count)s times!",
                               count),
                      {count: count},
                      true);

For everything but the ngettext/pngettext variants, you can also use expressions:

const count = 4;
const str = _`There are ${count + 1} lights!`;

// Giving us:
var count = 4;
var str = interpolate(gettext("There are %(value1)s lights!"),
                      {value1: count + 1},
                      true);

Works With Other Tagged Templates

Fan of the dedent tagged template plugin? Combine it with any of the raw gettext functions, like so:

const n = 4;
const str = gettext_raw(dedent`
    Here we've got lots of text, which may have
    newlines and
        ${n} space indentation
`);


// That becomes:
var n = 4;
var str = interpolate(
    gettext("Here we've got lots of text, which may have\nnewlines and\n    %(n)s space indentation"),
    {n: n},
    true);

Isn't that much nicer to maintain?

It's not just that one, either. Most tagged templates should be compatible (as long as they don't need to manage their own expressions/variable references, because this plugin will be preparing them for interpolation first).

Examples

Let's cover just a few more real-world examples, using the most common gettext methods:

_, gettext

// These are all equivalent:
const s = _`Let's localize!`;
const s = _`
      Let's  localize!
    `;
const s = _(`Let's localize!`);
const s = _("Let's localize!");
const s = gettext`Let's localize!`;
const s = gettext(`Let's localize!`);
const s = gettext("Let's localize!");

// So are these:
const s = _`i = ${i}`;
const s = _(`i = ${i}`);
const s = gettext`i = ${i}`;
const s = gettext(`i = ${i}`);
const s = interpolate(gettext("i = %(i)s"), {i: i}, true);

gettext_raw

// These are all equivalent:
const s = gettext_raw`  Let's
  localize!`;
const s = gettext_raw("  Let's\n  localize!");

// So are these:
const s = gettext_raw`  i  =  ${i}
`;
const s = gettext_raw("  i  =  ${i}\n");
const s = interpolate(gettext_raw("  i  =  %(i)s\n"),
                      {i: i},
                      true);

N_, ngettext

// These are all equivalent:
const s = N_('There is only one',
             'There are many',
             count);
const s = ngettext('There is only one',
                   'There are many',
                   count);

// So are these:
const s = N_(`There is only ${i}`,
             `There are ${i}`,
             count);
const s = ngettext(`There is only ${i}`,
                   `There are ${i}`,
                   count);
const s = interpolate(ngettext("There is only %(i)s",
                               "There are %(i)s",
                               count),
                      {i: i},
                      true);

Where is this used?

We use babel-plugin-django-gettext at Beanbag for our Review Board and RBCommons products, along with many of our other open source projects.

If you use this plugin, let us know and we'll add your project to this section!

License

Copyright (C) 2020 Beanbag, Inc. Released under the MIT license.