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

lokales

v1.2.4

Published

Lightweight barebones i18n localization written in Typescript.

Downloads

47

Readme

Lokales

Barebones i18n localization written in TypeScript. Similar to y18n but with a few improvements, handles errors better preventing corrupt or empty locale files. Other than that about the same.

New Version 1.1.x

Fixed graceful exit, ensures write before exit. Fix issue where directory isn't created, refactored simplified methods. Add method "sync" to synchronize all locales from a primary locale. This ensures your primary locale (probably "en") and it's keys exist in all other locales known on your system.

Quick Start

Initialize using ES5 or ES6/TypeScript Imports.

import { Lokales } from 'lokales';
const lokales = new Lokales({ /* your options */ });

OR ES5

const Lokales = require('lokales').Lokales;
const lokales = new Lokales({ /* your options */ });

Singular

Singular example passing key and format values.

lokales.__('Hello my name is %s.', 'Joe');
// Result > Hello my name is Joe.

Plural

Plural example passing singular key, plural key, count and format values.

lokales.__n('I have %d cat', 'I have %d cats', 2, 'Joe');
// Result > I have 2 cats.

Literal

Template literal example.

const color = 'blue';
lokales.__`My favorite color is ${color}.`;
// Result > My favorite color is blue.

Options

Options can also be set using the lokales.setOption(key, value) method.

API

Simple API for plural or singular localization along with get and set for options.

Advanced Usage

In some cases it may be useful to call a translate API after your primary localization file is updated. For example upon updating the en.json locale you may with to update perhaps the es, fr, ru or it locales. Here's an example using Google Translate.

IMPORTANT

A quick note about the below example. One key piece missing below for the sake of brevity is some sort of method to serialialize and deserialize the string to be translated. This is because the translate API won't typically know what to do with %s or %d. You can simply iterate the string and replace these tokens with tokens your translate API will ignore. The restructure the data after translation.

import { Lokales } from 'lokales';
import { translate } from '@google-cloud';
import { writeFile } from 'fs';
import { join } from 'path';

const lokales = new Lokales({ onUpdate: onUpdate });
const trans = translate({
  key: 'YOUR_API_KEY'
});

/**
 * On Update called when process queue updated locale.
 *
 * Updated Object Contains:
 * singular: string;  // The singular key string.
 * plural: string;    // The plural key if provided or null.
 * count: number;     // The count when plural is used.
 * args: [];          // The original format arguments passed.
 * options: {}        // Snapshot of options. (see options above for details)
 */
function onUpdate(err, updated, instance) {

  if (err)
    throw err; // do something with error.

  const value = updated.singular // you might check for plural here but for simplicity.
  const options = updated.options;
  const lang = 'es'; // the lang we want to translate to.
  const translated: any = {}; // object to store translated values.

  trans.translate(value, lang, (err, res) => {

    if (err)
      throw err;

    if (!Array.isArray(res)) // ensure our result is an array.
      res = [res];

    res.forEach((s, i) => {
      translated[s] = s; // see above important note.
    });

    const savePath = path.join(options.directory, lang + '.json');
    const serialized = JSON.stringify(translated, null, 2);

    fs.writeFile(savePath, serialized, (err) => {
      // do something with/if error.
      // or
      // log success etc.
    });

  });

}

Docs

See https://origin1tech.github.io/lokales/

License

See LICENSE.md