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

@shypes/language-translator

v2.0.15

Published

Helps with dynamic language transaltion

Downloads

455

Readme

@shypes/language-translator

npm (scoped) GitHub issues

Introduction

This is a simple light weight language parser to help with language key translations, built for speed using in-memory caching optimization technique.

Language file only get loaded once during your application life cycle

Install

npm install @shypes/language-translator

Usage

Follow the set by step guide below

Language Folder Setup

Create a dedicated folder to store your language template file.

Default folder is lang from your base directory configuration,

You will see how to set up your base directory below

Create the different sample files below in the language folder

ar.json

{
    "success": "نجاح",
    "email_phone_validation": "لا يمكن أن يكون البريد الإلكتروني والهاتف فارغين",
    "something_went_wrong": "هناك خطأ ما!",
    "missing_required_validation": "الحقول المطلوبة مفقودة",
    "missing_truck": "تم تعيين تجمع طلبات الشاحنات بالفعل على ${status}",
    "deliver_code":"مرحبًا ${name} ، إليك رمز otp ${code}"
}

en.json

{
    "success": "Success!",
    "email_phone_validation": "Email and phone cannot be empty",
    "something_went_wrong": "Something went wrong!",
    "missing_required_validation": "Missing required fields",
    "missing_truck": "Truck Request Pool has already been set to ${status}", 
    "deliver_code":"Hello ${name}, here is your otp code ${code}"
}

Initilaise the Application

const Language = require("@shypes/language-translator");

New version features

Simple usage from version 2.0.1

Version >= 2.0.1

Language._({
    __basedir : "./",
    langFolder : 'src/lang'
});

Loading with optional parameters

Language._({
    default_lang : "en",
    ext : ".json",
    __basedir : "./",
    langFolder : 'lang'
})

For Version <= 1.2.3

const Lang = Language();

Some Basic Configuration - Optional

Simple usage from version <= 1.2.3

// set your base directory
Lang.setBaseDir("./"));

// set your base directory
Lang.setLanguageDir('src/lang');

// set your default language
Lang.setDefaultLang('en');

// set the extention for yout language file, default is .json
Lang.setExtention(".txt");

Translation begin here

version <= 1.2.3

// set the language in which you need

Lang.setActiveLang('ar');

// get the text base on the defined language key

translated = Lang.get('email_phone_validation')

translated.then((text) => {
   console.log(text);
});

version > 2.0.3

// get the text base on the defined language key

translated = Language.get('email_phone_validation', 'ar')

translated.then((text) => {
   console.log(text);
});

Using a function

testTranslate = async (language, message) => {

    translated =  await Language.get(message, language)

    let data = {
        message: translated,
        language
    };

    console.log(data);
};

testTranslate('ar','something_went_wrong')

testTranslate('en','missing_required_validation')

testTranslate('ar','email_phone_validation')

In line Langauge Loading Supported, this help you load your language data directly with a file

avaliable in version >= 2.0.3


const data = {
    "success": "نجاح",
    "email_phone_validation": "لا يمكن أن يكون البريد الإلكتروني والهاتف فارغين",
    "something_went_wrong": "هناك خطأ ما!",
    "missing_required_validation": "الحقول المطلوبة مفقودة",
    "missing_truck": "تم تعيين تجمع طلبات الشاحنات بالفعل على ${status}",
    "deliver_code":"مرحبًا ${name} ، إليك رمز otp ${code}"
}

Language.load('ar', data)

Dynamic language template

It also support templated json strings

{
    "deliver_code":"مرحبًا ${name} ، إليك رمز otp ${code}"
}
translated = Language.get('deliver_code', 'ar', {'name':"John", 'code': 343923} )

translated.then((text) =>{
   console.log(text);
});

Output:

مرحبًا John ، إليك رمز otp 343923

Switch between Language Folder

await Language.setLanguageDir("lang/sms");

console.log(Language.getPath());

translated = await Language.get('deliver_code','ar', {'name':"John", 'code': 343923});

console.log(translated);

await Language.setLanguageDir("lang/email");

console.log(Language.getPath());

translated = await Language.get('deliver_code','ar', {'name':"John", 'code': 343923});

console.log(translated);

Check out the sample files in the test directory

License

MIT © Tosin Adesipe