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

lang-translate

v1.2.6

Published

This library is designed to facilitate the management and translation of phrases for multi-language websites. It provides a simple interface for setting the default language and retrieving translations based on the current language.

Downloads

24

Readme

Lang-translate

This library is designed to facilitate the management and translation of phrases for multi-language websites. It provides a simple interface for setting the default language and retrieving translations based on the current language.

Features

  • Set and change the default language.
  • Retrieve all phrases or a specific phrase by its ID.
  • Translate phrases based on the current language setting.

Installation

Install lang-translate

  1. npm i lang-translate

Getting Started

1. Import the Library

First, import the LangTranslate class :

import LangTranslate from 'lang-translate';

3. Create a file phrases.js in a new folder data

Add this data to phrases.js

// You can add/remove languages according to your need.
// add all your phrases in phrases
const phrases = 
[
  {
    id: 1,
    en: 'Good morning', // English
    fr: 'Bonjour', // French
    nl: 'Goedemorgen', // Dutch
    de: 'Guten Morgen', // German
    es: 'Buenos días', // Spanish
    it: 'Buongiorno', // Italian
    pt: 'Bom dia', // Portuguese
    ru: 'Доброе утро', // Russian
    zh: '早上好', // Chinese
    ja: 'おはようございます', // Japanese
    ko: '좋은 아침', // Korean
    ar: 'صباح الخير', // Arabic
    hi: 'सुप्रभात', // Hindi
    he: 'בוקר טוב', // Hebrew
    tr: 'Günaydın', // Turkish
    sv: 'God morgon', // Swedish
    no: 'God morgen', // Norwegian
    da: 'God morgen', // Danish
    fi: 'Hyvää huomenta', // Finnish
    pl: 'Dzień dobry', // Polish
    el: 'Καλημέρα', // Greek
    cs: 'Dobré ráno', // Czech
    hu: 'Jó reggelt', // Hungarian
    th: 'สวัสดีตอนเช้า', // Thai
    vi: 'Chào buổi sáng', // Vietnamese
  },
  {
    id: 2,
    en: 'Goodbye', // English
    fr: 'Au revoir', // French
    nl: 'Tot ziens', // Dutch
    de: 'Auf Wiedersehen', // German
    es: 'Adiós', // Spanish
    it: 'Arrivederci', // Italian
    pt: 'Adeus', // Portuguese
    ru: 'До свидания', // Russian
    zh: '再见', // Chinese
    ja: 'さようなら', // Japanese
    ko: '안녕히 가세요', // Korean
    ar: 'مع السلامة', // Arabic
    hi: 'अलविदा', // Hindi
    he: 'להתראות', // Hebrew
    tr: 'Hoşçakal', // Turkish
    sv: 'Adjö', // Swedish
    no: 'Ha det', // Norwegian
    da: 'Farvel', // Danish
    fi: 'Näkemiin', // Finnish
    pl: 'Do widzenia', // Polish
    el: 'Αντίο', // Greek
    cs: 'Sbohem', // Czech
    hu: 'Viszlát', // Hungarian
    th: 'ลาก่อน', // Thai
    vi: 'Tạm biệt', // Vietnamese
  }
];

export default phrases;

2. Initialize the Library

Create an instance of the LangTranslate class, you need to pass the language and your phrases array:

import phrases from './data/phrases.js';
const lang = new LangTranslate('en', phrases);

3. Using the Library

Set the Default Language

Change the default language at any time using the setLanguage method:

// you can use any language you want as long as
// it exits in your `data/phrases.js`
lang.setLanguage('fr');

Retrieve All Phrases

Get all the phrases stored in the phrases.js:

console.log(lang.getAllPhrases());

Retrieve a Phrase by ID

Fetch a specific phrase by its ID:

console.log(lang.getPhraseById(1));

Translate a Phrase

Translate a phrase based on the current language setting:

lang.setLanguage('ko');
console.log(lang.translate(1)); // 좋은 아침

Example Usage

Here is a complete example of how to use the library:

import LangTranslate from 'lang-translate';

const phrases = 
[
  {
    id: 1,
    en: 'Hello',
    fr: 'Bonjour',
    nl: 'Hallo'
  },
  {
    id : 2,
    en: 'Goodbye', 
    fr: 'Au revoir', 
    nl: 'Tot ziens'
  }
];

// Initialize with default language as French
const lang = new LangTranslate('fr', phrases);

// Translate phrases
console.log(lang.translate(1)); // Bonjour

// Change the default language
lang.setLanguage('nl');
console.log(lang.translate(2)); // Tot ziens
console.log(lang.translate(3)); // Phrase with id 3 not found.

// Retrieve all phrases
console.log(lang.getAllPhrases());

// Retrieve a phrase by id
console.log(lang.getPhraseById(1));