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

jstrings-localization

v2.1.0

Published

localization based on jstrings files

Downloads

2

Readme

NPM version Downloads Build Status Coverage Status dependencies dev dependencies

JStrings Localization

A javascript singleton Localization for both Node.js and Web browsers. The localised files are based on JStrings format.

Table of Contents

Install

$ npm i -D jstrings-localization

Or:

$ npm install --save-dev jstrings-localization

About JStrings

JStrings are an extension of the String Resources format used inside Apple developments. This format is usefull during localisation process, human readable, and easy to shared. The Apple Strings Resources is in text format and contains pairs of key-value that are strings. Comments can be sets inside the text file.

/* A one line comment */
"My first key"="My first value";

/*
 *  A multi-lines comment
 *
 */
"My second key"="My second value";
 

The file format is very simple !

Composed with the filename itself, followed by optinals underscore and ISO code, terminated by the extension.

filename[_iso].extension

// Default file
common.jstrings.txt
// English localised file
common_en.jstrings.txt
// French localised file
common_fr.jstrings.txt
...etc

ISO code's format and extensions are whatever you'd like (See : ISO 639).

The default file is used when no localised file according to a code were found.

The JStrings introduces multi-lines values. It used a specifics values format, surrounded by brackets. The output result will be joined by a newline.

/* A multi-lines values */
"My 1st line..."=["My 1st line","followed by a 2nd line","","Ended by a 4th line"];

It's up to you to define the key you'd like to use. It must be unique along the files you're using. Best practive is to be verbose in your code.

You'd like to add some variables in your string, for example, like this :

    "Welcome %1 ! Rock\'n\'Roll !".localize().replace('%1',user.name);

There's no specific format for the variables. You can use any caracters you'd like at your own risk. By the way, you can combine strings together.

Usage

With Node.js

"use strict";

const Localization    = require("Localization");

const languages       = ["de","en","es","he","fr"];
let   lang            = "en";

Localization.language( languages, lang );

Localization.load("strings/common.jstrings.txt");
Localization.load("strings/myapp.jstrings.txt");

console.log( "Hello world !".localize() );

In a browser

    <script type="text/javascript" src="js/jq/jquery.min.js"></script>
    <script type="text/javascript" src="js/lib/Localization.cls.js"></script>
    <script type="text/javascript">
        var languages       = ["de","en","es","he","fr"];
        var lang            = "en";

        var page            = "welcome.html";

        Localization.language( languages, lang );

        lang = /fr/.test(navigator.language) ? 'fr' : 'en'; //Force 'fr' if ok 
        Localization.localize( lang );

        if( ! Localization.load("strings/common.jstrings.txt") )
            console.log("Localization common FAILED !");
            
        if( ! Localization.load("strings/myapp.jstrings.txt")  )
            console.log("Localization myapp  FAILED !");

        splash( "Hello world !".localize() );
        
        $("#content").load("inc/" + page.localize());
    </script>

Localization class

The implementation of the Localization Class is based on the Singleton. You don't need to instanciate it.

Fields

| Name | Type | Description | |:----------------|:------------:|:------------------------------------------------------| | version | string | The class version. | | preferedLanguage | array| The languages your application can handel. | | defaultLanguage | string| Default language used when unmatched user language. | | currentLanguage | string| The language in use. | | userLanguage | string| The user language preferences or system language |

You must not sets this values. Use the methods insteed. Keep in mind they are read only.

Methods

  • language( preferedLang ,defaultLang )

    Global settings of the Localization class. You would call it first.

    • preferedLang

      Type: array

      List of the languages your application handle.

    • defaultLang

      Type : string

      The default language used when user language's can't match any in the list.


  • localize( lang )

    Specified the language for the localisation. Updates also all the files previouly loaded to the specified language.

    • lang

      Type : string

      The language used for the comming localisations. When you changed the code at run time, keep in mind to refresh your application.

      Important ! There is no ordered list of the files. Don't think overwriting !


  • load( file )

    Load a file in path format, according to the localised settings.

    • file

      Type : string

      The path of the file to load. The format must not contains any iso code.

String prototype

  • localize()

    Localise the string value or the variable value. When no corresponding translation is available, the string remains the same.

      "localize me".localize()

License

MIT © guitarneck