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

@marcoparrone/i18n

v1.0.6

Published

Internationalization library for JavaScript.

Downloads

3

Readme

i18n

Internationalization library for JavaScript

Installation

npm install @marcoparrone/i18n

Usage

After the installation, to import the module:

import I18n from '@marcoparrone/i18n';

Read further for more information.

Initialization

The constructor accepts these optional parameters:

  • callback_after_translation: a callback function which will be called after the translation (for example, a function which will reload all the strings in the web page);
  • language: the target language: if not provided, it will be loaded from localStorage or from the user browser, or it will default to 'en';
  • messages_path: path where to find the localization files, which must be named language.json: for example, en.json, it.json, etc... defaults to "i18n/";
  • localstorage_prefix: the prefix for loading and saving the language setting in the browser localStorage. defaults to "i18n_lib_" (the language will be saved to the "i18n_lib_language" localStorage item);
  • supported_languages: an array containing the codes for the supported languages. defaults to:
 ['en', 'af', 'sq', 'am', 'ar', 'hy', 'az', 'eu', 'be', 'bn', 'bs', 'bg', 'ca', 'ceb', 'ny', 'zh-CN', 'zh-TW', 'co', 'hr', 'cs', 'da', 'nl', 'eo', 'et', 'tl', 'fi', 'fr', 'fy', 'gl', 'ka', 'de', 'el', 'gu', 'ht', 'ha', 'haw', 'iw', 'hi', 'hmn', 'hu', 'is', 'ig', 'id', 'ga', 'it', 'ja', 'jw', 'kn', 'kk', 'km', 'rw', 'ko', 'ku', 'ky', 'lo', 'la', 'lv', 'lt', 'lb', 'mk', 'mg', 'ms', 'ml', 'mt', 'mi', 'mr', 'mn', 'my', 'ne', 'no', 'or', 'ps', 'fa', 'pl', 'pt', 'pa', 'ro', 'ru', 'sm', 'gd', 'sr', 'st', 'sn', 'sd', 'si', 'sk', 'sl', 'so', 'es', 'su', 'sw', 'sv', 'tg', 'ta', 'tt', 'te', 'th', 'tr', 'tk', 'uk', 'ur', 'ug', 'uz', 'vi', 'cy', 'xh', 'yi', 'yo', 'zu', 'he', 'zh']

In this example, this.saveState is a function which reloads the translated strings:

this.i18n = new I18n(() => {this.forceUpdate()});

Changing language

For changing the language, it's possible to call the change_language_translate_and_save_to_localStorage method, which accepts the language code as parameter.

In this example, event.target.value cotains the code of the selected language (for example, 'en', 'it', etc...):

this.i18n.change_language_translate_and_save_to_localStorage(language);

Providing the translations

The translations must be available in the messages_path path which was configured during the initialization, for example:

$ ls public/i18n/
af.json  bn.json   cy.json  es.json  fy.json  haw.json  hu.json  iw.json  kn.json  lo.json  ml.json  ne.json  pl.json  sd.json  so.json  sw.json  tl.json  uz.json     zh-TW.json
am.json  bs.json   da.json  et.json  ga.json  he.json   hy.json  ja.json  ko.json  lt.json  mn.json  nl.json  ps.json  si.json  sq.json  ta.json  tr.json  vi.json     zh.json
ar.json  ca.json   de.json  eu.json  gd.json  hi.json   id.json  jw.json  ku.json  lv.json  mr.json  no.json  pt.json  sk.json  sr.json  te.json  tt.json  xh.json     zu.json
az.json  ceb.json  el.json  fa.json  gl.json  hmn.json  ig.json  ka.json  ky.json  mg.json  ms.json  ny.json  ro.json  sl.json  st.json  tg.json  ug.json  yi.json
be.json  co.json   en.json  fi.json  gu.json  hr.json   is.json  kk.json  la.json  mi.json  mt.json  or.json  ru.json  sm.json  su.json  th.json  uk.json  yo.json
bg.json  cs.json   eo.json  fr.json  ha.json  ht.json   it.json  km.json  lb.json  mk.json  my.json  pa.json  rw.json  sn.json  sv.json  tk.json  ur.json  zh-CN.json
$ cat  public/i18n/en.json
{
    "text_appname": "Notes",
    "text_add_label": "add a note",
    "text_settings_label": "settings",
    "text_importexport_label": "import and export notes",
    "text_help_label": "help",
    "text_about_label": "about"
}

Using the translations

In the following examle you can see how to use the libray.

//...
import I18n from '@marcoparrone/i18n';
//...
const defaultText = require ('./en.json');

//...
class Board extends React.Component {
//...
  constructor(props) {
//...
        this.i18n = { language: 'en', text: defaultText};
//...
  }
//...
  componentDidMount() {
//...
    // Localize the User Interface.
    this.i18n = new I18n(() => {this.forceUpdate()});
//...
  }
//...
  handleSettingsChange(/* ... */ language) {
//...
    if (this.i18n.language !== language) {
      this.i18n.change_language_translate_and_save_to_localStorage(language);
//...
    }
//...
  }
//...
  render() {
//...
    return (
			<AppWithTopBar refprop={this.notesListRef} lang={this.i18n.language} appname={this.i18n.text['text_appname']}
			  icons={[{label: this.i18n.text['text_add_label'], icon: 'add', callback: () => this.addNote()},
//...
								{label: this.i18n.text['text_about_label'], icon: 'info', callback: () =>  open_dialog(this.notesListRef, 'about')}]} >
...
        </AppWithTopBar>
    );
  }
}
//...