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

kakyo

v1.1.0

Published

Kakyo is a revolutionarily simple and easy to use i18n library.

Downloads

5

Readme

Kakyo is an extremely minimal, highly performant and fully tested internationalization (i18n for short) library following the KISS principle. Kakyo was developed with the intention of developers being able to easily integrate i18n into any existing code base without having to heavily refactor the project or even rewrite any single bit of code.

Internationalization is an important step in making an application as accessible as possible, and Kakyo is here to make it as painless and straightforward as humanly possible!

Install

Installing Kakyo is as easy as running following command if you're using npm:

$ npm install kakyo

Alternatively you can also use yarn:

$ yarn add kakyo

Example Usage

Let's assume we have following folder structure:

kakyo-example
│
├── node_modules/
│   └── kakyo/
│
├── translations/
│   ├── english.json
│   └── japanese.json
│   
└── index.js

Translations can be abstracted into their own respective translation files if so desired, however it's also possible to put every translation into the same file grouped by their locale to stop unnecessary clutter if the project is lightweight enough.

english.json

{
    "en_US": {
        "WELCOME_MESSAGE": "Welcome, #{username}#!",
        "CURRENT_LEVEL": "Your level is #{level}#!"
    }
}

japanese.json

{
    "ja_JP": {
        "WELCOME_MESSAGE": "ようこそ、#{username}#!",
        "CURRENT_LEVEL": "あなたのレベルは#{level}#です!"
    }
}

Locales: Each language and thus its translations is grouped by its locale in the localisation files.

Response Keys: Each response must be keyed by a unique identifier which has to be identical amongst every language block.

Placeholders: Placeholders in translation files are initiated by an opening #{, followed by a data property name that's wished to be used, followed by a closing }# tag as shown above in the example files ( #{username}# and #{level}# ).

index.js

const Kakyo = require('kakyo');
const translations = new Kakyo(__dirname + '/translations', { defaultLocale: 'en_US' });

console.log(
    translations.get('WELCOME_MESSAGE', { username: 'John Doe' })
); /* ⇨ Welcome, John Doe! */

console.log(
    translations.get('WELCOME_MESSAGE', { username: 'Satoshi' }, 'ja_JP')
); /* ⇨ ようこそ、Satoshi! */

console.log(
    translations.get('CURRENT_LEVEL', { level: 45 })
); /* ⇨ Your level is 45! */

console.log(
    translations.get('CURRENT_LEVEL', { level: 37 }, 'ja_JP')
); /* ⇨ あなたのレベルは37です! */

Instantiation: Kakyo only requires an absolute folder path to where the localisation files are stored.

Default Locale: Providing a default locale upon instantiating Kakyo is fully optional and can be omitted.

Changing Languages: Getting a response in a different language than what was set as the default is as simple as providing the desired locale as a function parameter!

API

Kakyo's API overview

new Kakyo(folderPath [,options]) - Instantiating a new instance of Kakyo:

const translations = new Kakyo(__dirname + '/translations', { defaultLocale: 'en_US' });
  • folderPath String - An absolute file path of the directory that harbors all localisations.
  • options Object (optional) - Options for initializing Kakyo and overwriting its default behavior.
    • options.defaultLocale String (optional) - Set the default locale of this Kakyo instance.

get(key, data [,locale]) - Getting a translated, rendered response:

const result = translations.get('WELCOME_MESSAGE', { username: 'Bob' }, 'ja_JP');
  • key String - A response key identifier corresponding to responses saved in the localisation files.
  • data Object - An object containing the data which should be injected into the response template.
    • The data object properties MUST match the placeholder names used in the localisation files!
  • locale String (optional) - A locale specifying which language the response text should be rendered in.
    • Only optional if a default locale has been set upon instantiating Kakyo!

Returns: String - A fully rendered and ready to be used response string with the passed data values injected.

getDefaultLocale() - Get the currently active default locale:

const currentDefaultLocale = translations.getDefaultLocale();

Returns: String - The currently active default locale for this Kakyo instance.

setDefaultLocale(locale) - Set a new default locale:

translations.setDefaultLocale('ja_JP');
  • locale String - The new default locale that is wished to be set.
    • Head's up! Locales are cAsE sEnSiTiVe!

Returns: void - Setting a new default locale has no return value!

reload() - Reload Kakyo's localisation files cache:

Classic syntax:

translations.reload().then((successful) => {
    if (successful) {
        /* ⇨ Reload was successful! */
    }
}).catch((reloadingError) => {
    /* ⇨ Reload wasn't successful! */
});

async / await syntax:

const successfullyReloaded = await translations.reload();

if (successfullyReloaded) {
    /* ⇨ Reload was successful! */
}

Returns: Promise<Boolean> - Resolves with a truthy promise upon successful reload!

License

This repository makes use of the MIT License and all of its correlating traits.

While it isn't mandatory, a small credit would be highly appreciated if this repository was to be reused!