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

locale-kit

v1.1.3

Published

A simple library that allows to manage language packages and translate texts with Node.js

Downloads

6

Readme

Locale Kit

Locale Kit is a simple library that allows to manage language packages based on SQLite3 database. It provide also utilities that allow to translate texts as well as detect their language supporting Google and Yandex as service provider. The package is shipped with a CLI utility that allows to create and translate language packages, for more information and documentation check the file called "cli.md".

Installation

Before installing the package, make sure that the sqlite3 module is installed, if you are going to install the library through a package manager, the dependence will be installed automatically. To install this package through NPM just run this command:

npm install locale-kit

Usage: Package

The class "Package" allows to fetch the labels from the language package, before using it you need, of course, to set up the path to the package and the locale to use, the locale must be supported by the package, you can set a locale and allows to the library to switch to a fallback locale based on the language of the given locale code, for example, if you set "en-US" as locale but the package doesn't support it, the library will look for any locale matching with "en", unless the strict mode is enable, here you are an example:

let pack = new LocaleKit.Package();
//path, locale, strict mode
pack.setPackage('path/to/the/package.db', 'en-US', false).then((locale) => {
	//locale is a string containing the locale code selected, if different by the given code, it means that a fallback locale has been picked.
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

You can get a list of all the supported locales by using the following method:

//Get all locales.
pack.getSupportedLocales().then((locales) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});
//Check for a specific locale support.
//locale, strict mode
pack.isLocaleSupported('en-US', false).then((value) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

Now you are able to get the labels, you can use the following method passing an array containing the labels' IDs, the IDs can be represented as numbers or strings, according with the package, the labels will be returned as object having as key the label ID and as value its text, if a label were not found, it will be omitted, here's the example:

pack.getLabels([1, 2, 3]).then((labels) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

Of course you can fetch all the labels from the package using the following method:

pack.getAllLabels().then((labels) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

Usage: Translator

The class "Translator" allows to translate and detect the language of texts, as of now, it support only Google and Yandex as service provider, in order to use it, you need to get an API key from the provider that you are going to use, by default, Yandex is used because it offers a free plan that allows to translate up to 10000000 chars per month, you can get a free API key from Yandex here, here you are the setup example:

let translator = new LocaleKit.Translator();
//token, text format
translator.setupYandex('YOUR API TOKEN HERE', LocaleKit.Translator.HTML);

Now you can translate one or more texts using this method:

//text, target language, original language
translator.translateText(['Ciao mondo!'], 'en', 'it').then((texts) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

Note that you can omit the original language, in this case it will be automatically detected by the provider. The translated texts will be returned as object having as key the original text and as value the translated one. In a similar way you can detect the language of one or more texts, here you are an example:

//text, target language, original language
translator.detectLanguage(['Ciao mondo!']).then((detections) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

It will return an object having as key the original text and as value the code of the language detected. If you need to get a list of all the languages supported by the service provider you can use the following method:

translator.getSupportedLanguages('en').then((languages) => {
	//Your stuff here.
}).catch((ex) => {
	//Handle errors here.
});

Both the classes support data caching, you can set up cache using these methods:

//Setup cached for the package.
pack.setCache(true).setCacheHandler(cache);
//Setup cached for the translator.
translator.setCache(true).setCacheHandler(cache);

Data caching is provided by the module "tiny-cacher", as you can see in the examples, the variable "cache" is an instance of the class "TinyCacher" that allows to store data using different options such as Redis, Memcached and file. You can find more information about it on its repository on GitHub.

If you like this project and think that is useful don't be scared and feel free to contribute reporting bugs, issues or suggestions or if you feel generous, you can send a donation here.

Are you looking for the PHP version? Give a look here.