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

@alirezakeshvari/langjs

v1.0.1

Published

`langjs` is a lightweight multi-language support package for TypeScript/JavaScript projects. It allows developers to define their own translation files in JSON format, switch between languages dynamically, and fetch localized strings for their application

Downloads

30

Readme

langjs

langjs is a lightweight multi-language support package for TypeScript/JavaScript projects. It allows developers to define their own translation files in JSON format, switch between languages dynamically, and fetch localized strings for their applications.

Features

  • Dynamically load translation files (JSON format).
  • Easily switch between multiple languages.
  • Simple API to retrieve translations for any key.
  • TypeScript support for type safety.

Installation

You can install langjs from npm:

npm install @alirezakeshvari/langjs

Or using Yarn:

yarn add @alirezakeshvari/langjs

Usage

1. Setting up your translation files

Define your translations in JSON files for each language. For example, create en.json and fa.json like this:

  • en.json
{
  "hello": "Hello",
  "goodbye": "Goodbye"
}
  • fa.json
{
  "hello": "سلام",
  "goodbye": "خداحافظ"
}

2. Providing the paths to your translation files

In your project, use the setTranslations function to provide the paths to your translation files:

import { setTranslations, setLocale, _ } from "langjs";

// Set paths to your translation files
setTranslations({
  en: "./path/to/en.json",
  fa: "./path/to/fa.json",
});

3. Switching between locales

Use the setLocale function to switch between languages:

// Set locale to English
setLocale("en");
console.log(_("hello")); // Outputs: Hello

// Set locale to Farsi
setLocale("fa");
console.log(_("hello")); // Outputs: سلام

4. Fetching translations

Use the _ function to get translations based on the current locale:

console.log(_("goodbye")); // Outputs the translation for the current locale

If a translation key is not found, the key itself will be returned:

console.log(_("nonexistent_key")); // Outputs: nonexistent_key

API

setTranslations(localeFiles: { [locale: string]: string }): void

Sets the paths to your translation files. The object keys represent the locale code (e.g., en, fa), and the values represent the file paths to the respective JSON files.

  • localeFiles: An object where the key is the locale (e.g., "en", "fa") and the value is the file path to the JSON file.

setLocale(locale: string): void

Sets the active locale for translations. This locale will be used to fetch translations when calling _.

  • locale: The locale to switch to (e.g., "en", "fa").

_ (key: string): string

Fetches the translation for the given key from the currently set locale. If the key does not exist, it returns the key itself.

  • key: The key for which to get the translation (e.g., "hello").

Example Project Setup

import { setTranslations, setLocale, _ } from "langjs";

// Set the translation files for English and Farsi
setTranslations({
  en: "./translations/en.json",
  fa: "./translations/fa.json",
});

// Switch to English
setLocale("en");
console.log(_("hello")); // Outputs: Hello

// Switch to Farsi
setLocale("fa");
console.log(_("hello")); // Outputs: سلام

Running Tests

You can run the unit tests with Jest. First, ensure that you've set up your project with the required development dependencies:

npm install

Then, run the tests:

npm test

License

This project is licensed under the MIT License.