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

@tuicom/l10n-tools

v5.0.3

Published

Catalog tools for tuicom/l10n, a lightweight translation library. To be installed as dev dependency.

Downloads

1

Readme

l10n-tools – catalog tools for the tuicom/l10n library

This package is a collection of tools to work with the tuicom/l10n library.

Installation

This package is available via NPM:

npm install --save-dev @tuicom/l10n-tools

Usage

The catalog manager is a CLI tool and has two tasks:

  1. It extracts all translatable strings into a .po catalog.
  2. It builds translation tables as a lightweight JSON objects.

The catalog manager needs PHP >= 7.1 in your development environment. Why do we use PHP in a JavaScript module? Because of the great Gettext library by Oscar Otero which provides very powerful tools to analyze source code and to manage translations. We weren’t able to find something similar written in JavaScript, so for now we will be using PHP.

The npx l10n tool is the CLI frontend to the catalog manager.

Extracting message strings

First, we need to go through our code and find all translatable strings, i.e. the ones we’ve wrapped in our l10n.t, l10n.n and l10n.x functions.

Luckily, we have a tool for this, but first, we must specify the desired target languages. Simply add the following entry to your project’s package.json file:

{
  "l10n" : {
      "directory" : "l10n",
      "locales" : [
          "de-DE",
          "fr-FR"
      ],
      "extract" : [
        "main.js",
        "other.js",
        "|src/.*|"
      ]
  }
}

The directory key specifies where the translations catalogs will be stored.

The locales key specifies the locales into which your package should be translated. The format for locales is: two lowercase letters for the language, followed by a hyphen (not an underscore!), followed by two uppercase letters for the region/country. NOTE: This tool assumes the en-US locale as default, therefore you don’t need to add it.

The extract key contains a list which specifies the files to be considered for the catalog. Each item in this list can either be a verbatim file name or a regular expression (PCRE).

When you’re done configuring your locales and catalog source files, you can run the extractor. It will find all occurences of our translation functions and add them to one catalog per locale:

npx l10n extract

Assuming you are using the configuration from the above example, the extractor will create or update the catalogs for German and French. Catalogs would be stored in the ./l10n directory. So after running the command for the first time, you will find the new files ./l10n/de-DE.po and ./l10n/fr-FR.po in your project. Don’t forget to put them under version control.

The *.po files can be given to a human translator or be run through a translation tool which supports this format (there are lots of them). After the .po files have been updated, we can create the JSON translation tables.

Creating translations tables

In order to use the translations in your code, you must transform the .po files into JSON, which is done by the tables subcommand.

So, again we start with some configuration. In your package.json file, add a tables key to the l10n entry. The tables key contains an object, where each entry is a target file for the JSON table mapped to a list of source files. Each item in this list can either be a verbatim file name or a regular expression (PCRE). Each JSON file will contain the translations for all languages for all strings in the referenced source files. For example:

{
    "l10n": {
        "tables": {
            "l10n/translations.json": [
                "first.js",
                "second.js",
                "third.js"
            ]
        }
    }
}

Now run the following command to create the l10n/translations.json:

npx l10n tables

After that, you can use the l10n/translations.json in your module, as described in the l10n documentation.

NOTE: In the above example, we have only one JSON target file. But you could have multiple JSON tables per project as well. Why is that? Because you may want to create multiple subsets of translations, e.g. when parts of your application are lazy-loaded.