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

llengues

v0.1.1

Published

*Llengues* is a NodeJS module which provides an internationalization (i18n) solution for `NodeJS` (`javascript` and `typescript` compatible).

Downloads

3

Readme

Llengues

Llengues is a NodeJS module which provides an internationalization (i18n) solution for NodeJS (javascript and typescript compatible).

With llengues, you will be able to generate automatically translation files based on json which then will be used to translate lines at running time.

Llengues performs two different tasks:

  • Sync: Generate translation files for each desired locale building an AST for your source files (like a lint tool) and looking for calls to the translation method (tr(...)).
  • Translate: Provides a runtime mechanism to perform translations to the desired locale when required.

Sync task must be performed every time a new translatable line is added to the codebase in order to include it to the translation files. Llengues provides a CLI in order to run syncs easily.

Translate process take place during application execution when tr() is called. Based on the desired locale, the translated line will be used instead. Translate process is request context aware (using AsyncLocalStorage) thus you can use different locales for each request in case your application is handling requests.

Getting Started

First install the package:

npm install llengues

Configuration

Llengues configuration is located in llengues.json file. And follows this interface:

  sources: string[];
  locales: {
    original: Locale;
    fallback?: Locale;
    available: Locale[];
  };
  outDir: string;
  • sources: Glob patterns used to get the files where look for translations
  • locales.original: Original locale used in your sources. This will save time as the locale translation files generated will be translated automatically
  • locales.fallback: Locale used when a translation or a locale is not available.
  • locales.available: Locale list of available locales.
  • outDir: Path where translation files will be persisted and loaded.

You can generate a default configuration file with:

node node_modules/.bin/llengues init

Creating translatable lines

Llengues exports the tr function:

export function tr(line: string, bindings?: Bindings): string;

This method translates the line to the desired locale and fills placeholders with values in it. For instance, having the following files:

#####en.json:

{
  "Greetings, :name:": "Welcome, :name:"
}
app.ts:
import { tr } from 'llengues';

console.log(tr('Greetings, :name:', { name: 'Enric'}));

The following text will be printed out if the configured locale is en:

Welcome, Enric

Any word which starts and ends with : is considered a placeholder and will be replaced in case bindings contains a key with the same name.

Sync

Sync process looks for calls to the tr("...") method in the files which match the sources configuration patterns. All new translations which are not already present in the translation files for each locale will be added. Notice original and fallback translation files are generated too.

Sync can be easily called with:

node node_modules/.bin/llengues sync

Example:

Using the following configuration:

{
  "sources": ["src/**/*.ts"],
  "locales": {
    "original": "en",
    "fallback": "ca",
    "available": ["ca", "es"]
  },
  "outDir": "lang"
}

And having this source file:

//src/app/app.ts
import { tr } from 'llengues'

tr("Greetings, :name:", { name: user.name });

Following files will be generated (or updated) in lang directory after call sync:

####en.json (Notice en translation file is also generated because is defined as original.)

{
  "Greetings, :name:": "Greetings, :name:"
}

As original locale, translation file is automatically filled.

####ca.json

{
  "Greetings, :name:": null
}

####es.json

{
  "Greetings, :name:": null
}

New translations detected will be filled with null for any locale which is not original locale.

Every translated line which is already in the translation file before sync remains immutable while is being used. If a translated line is not being used anywhere then it will be removed automatically.

A line is considered translatable when calls to tr() exported method. It also detects local variables thus following scenarios will be also detected:

import { tr } from 'llengues';
import { tr as alias } from 'llengues';
const tr = require('llengues').tr;
const alias = require('llengues').tr;

Translate

TODO