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

@novigi/i18n

v1.0.0-1

Published

A versatile library for seamless string translation utilizing custom language files.

Downloads

2

Readme

npm (scoped) NPM Statements Branches Functions Lines

@novigi/i18n

A versatile library for seamless string translation utilizing custom language files.

🐿 Features

  • Translate a string word by word, providing a more granular level of translation.
  • Translate a string as a whole sentence, preserving the sentence structure and context during translation.
  • Flexibility to customize the translation process by providing customizable options.

📦 Getting Started

  1. Install the dependency
npm install @novigi/i18n
  1. Import the library
const lib = require('@novigi/i18n');

📖 Documentation

i18n

Represents an internationalization (i18n) module.

This library provides methods for translating a string using a JSON file that contains translated versions of words or sentences.

The language file should be a flat JSON file.

The content of the sample file used in the following usage example is as follows ( si-LK.json ) :

{
 "hello": "හෙලෝ",
 "world": "ලෝකය",
 "Hello World":"හෙලෝ ලෝකය"
}

Translation can be implemented as follows:

const I18n = require('@novigi/i18n')


const i18n = new I18n('/path/to/si-LK.json')

const translatedText = i18n.translate('Hello World')

console.log(translatedText);

const translatedSentence = i18n.translateBySentence('Hello World')

console.log(translatedSentence);

//"හෙලෝ ලෝකය"

//"හෙලෝ ලෝකය"

i18n~I18n

Represents an Internationalization (i18n) utility class for translating strings.

Kind: inner class of i18n

new I18n(filePath, [options])

Constructs a new instance of the I18n class.

Returns: I18n - A new instance of the I18n object.
Throws:

  • I18nParameterError - When the filePath is not provided or an empty string.
  • I18nParameterError - When the filePath is not an absolute path.

| Param | Type | Default | Description | | --- | --- | --- | --- | | filePath | string | | The file path of the translation source file. | | [options] | object | { removeUnavailable: false,removePunctuation: false,skip: [],defaultValue: null } | Optional parameters for customizing the translation. | | [options.removeUnavailable] | boolean | false | Determines whether to remove or keep words that don't have a translation in the language file. | | [options.removePunctuation] | boolean | false | Determines whether to remove or keep punctuation marks. Punctuation marks include: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ | | [options.skip] | Array.<string> | [] | An array of words that should be skipped during translation. | | [options.defaultValue] | string | "''" | The default value to be returned when there is no mapping for a word. |

Example

// Create an instance of I18n with the absolute source file path
const i18n = new I18n('/path/to/language-file.json')

// Create an instance of I18n with the absolute source file path and optional parameter options
const i18n = new I18n('/path/to/language-file.json', options)

i18n.translate ⇒ string

Translates the given text using the specified translation options.

Kind: instance property of I18n
Returns: string - The translated text.
Throws:

  • I18nTranslateError - If the text is not a string.

| Param | Type | Default | Description | | --- | --- | --- | --- | | text | string | | The text to be translated. | | [translateOptions] | object | {removeUnavailable: false,removePunctuation: false,skip: []} | The translation options specify the configuration settings for the translation process. These options are similar to the class-level options, but they are scoped to the functional level, providing flexibility for individual translation operations. | | [options.removeUnavailable] | boolean | false | Determines whether to remove or keep words that don't have a translation in the language file. | | [options.removePunctuation] | boolean | false | Determines whether to remove or keep punctuation marks. Punctuation marks include: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ | | [options.skip] | Array.<string> | [] | An array of words that should be skipped during translation. |

Example

// Translate a string
const translatedText = i18n.translate('Hello')

// Translate a string with optional parameter options
const translatedText = i18n.translate('Hello', options)

i18n.translateBySentence ⇒ string

Translates a sentence using the provided translation dictionary.

Kind: instance property of I18n
Returns: string - The translated sentence if available in the dictionary, otherwise returns the original sentence or an empty string if the sentence is falsy.
Throws:

  • I18nTranslateError When the sentence is not a string.

| Param | Type | Description | | --- | --- | --- | | sentence | string | The sentence to translate. |

Example

// Translate a sentence
const translatedSentence = i18n.translateBySentence("Hello, how are you?");

This is an auto generated file. Please don't make changes manually