Code Monkey home page Code Monkey logo

languages.js's Introduction

languages.js

A basic and lightweight replacement for Globalize. Stores ISO 639-1 language database in a JSON object, accesible as node module or by browser javascript interpret. Without any dependencies.

Features

  1. The same file can be use from the server side in nodejs to the client side in javascript browser interpret.
  2. Also includes the json file for using in other languages, for example PHP.
  3. Supports 138 languages
  4. Very lightweight, only 6.7K or 3.7K gzipped.
  5. Very basic too, but enough for a lot of projects: only return the ISO 639-1 language codes of supported languages, the English name, the nativeName and his own direction.

Use

From javascript the JSON object is not accesible directly. You must use this public functions:

  • languages.isValid(langcode): Return boolean value, true if langcode is supported.
  • languages.getAllLanguageCode(): Return an array with all the language codes supported.
  • languages.getLanguageInfo(langcode): Return object {"name": name of the language in English, "nativeName", "direction"}. If langcode isn't supported return {}.

See the test folder for use examples:

From nodejs

// From node the module is accesible with a simple require
var languages = require ('../languages.min.js');
var num_languages = 0;

// languages.getAllLanguageCode() return an array of all ISO 639-1 language code supported
var langscodes = languages.getAllLanguageCode();
// iterate this array
for (num_languages=0; num_languages<langscodes.length; num_languages++) {
  // show a string representation of the object return by languages.getLanguageInfo(langcode)
	console.log(langscodes[num_languages]);
	console.log("   "+JSON.stringify(languages.getLanguageInfo(langscodes[num_languages])));
}
// show the number of languages supported
console.log("Languages supported: "+num_languages);
// test languages.isValid(langcode) function
console.log("¿isValid 'kaka' language code? "+languages.isValid('kaka'));
console.log("¿isValid 'es' language code? "+languages.isValid('es'));

From browser

<!doctype html>
<html>
<head>
    <title>Test languages module</title>
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
    <script src="../languages.min.js"></script>
    <style>
    body {
        background-color: #eee;
    }
    .centrador {
        width:700px;
        margin:10px auto;
        border:1px solid #ccc;
        padding:20px;
        background-color:#fff;
    }
    </style>
</head>
<body>
    <div class="centrador">
        <h1>Test languages module (Browser client side)</h1>
        <hr />
        <div id="test"></div>
    </div> <!-- .centrador-->
    <script>
        var num_languages = 0,
        text = '';

        // languages.getAllLanguageCode() return an array of all ISO 639-1 language code supported
        var langscodes = languages.getAllLanguageCode();
        // iterate this array
        for (num_languages=0; num_languages<langscodes.length; num_languages++) {
            // save in text variable a string representation of the object return by languages.getLanguageInfo(langcode)
            var langcode = langscodes[num_languages];
            text+='<b>'+langcode+'</b> '+JSON.stringify(languages.getLanguageInfo(langcode))+'<br />';
        }
        // save the number of languages supported
        text = '<h2>Languages supported: '+num_languages+'</h2>'+text;
        // write the test result in DOM element with id='test'
        document.getElementById('test').innerHTML = text;
    </script>
</body>
</html>

languages.js's People

Contributors

joker-x avatar

Stargazers

 avatar  avatar Noor Marar avatar 底なし沼の魔女 avatar Jeremy Chaufourier avatar Konstantin Vyatkin avatar wen avatar Manan avatar Badger avatar  avatar Valentin Vichnal avatar gwendall avatar Chelsea avatar Gerard Bosch Creus avatar 傅长路 avatar Hugh Francis avatar  avatar Robin van Boven avatar TintinJS avatar Quentin Hayot avatar rudzon avatar Adam Bar avatar Suriyaa Sundararuban avatar Khoa Nguyen avatar Chris Hart avatar Alexander Kunz avatar cfpperche avatar Davide Gaspar avatar Anatoly avatar Fredrik Forsmo avatar Lior Cohen avatar Julien Evano avatar Fred K. Schott avatar  avatar Malte Legenhausen avatar Eusthace Corin avatar

Watchers

Alexander Kunz avatar Eusthace Corin avatar James Cloos avatar  avatar Badger avatar J Sreepati Rao avatar

languages.js's Issues

languages.getLanguageInfo().direction always returns ltr

Thanks for making this language util available!

I ran into the following challenge:

languages = require('languages');
info = languages.getLanguageInfo('ar');
console.log(info.direction); // returns 'ltr'. 'rtl' was expected

consider replacing line 30 in languages.js:

"rtl": ["ar","dv","fa","ha","he","ks","ku","ps","ur","yi"]

with

"rtl": {ar:1,dv:1,fa:1,ha:1,he:1,ks:1,ku:1,ps:1,ur:1,yi:1}

and replacing lines 222-224 in languages.js

  var result = "ltr";
  if (langs.rtl.indexOf(langcode) != -1) result = "rtl";
  return result;

with

  return (langs.rtl[langcode]) ? 'rtl' : 'ltr';

This should be faster, reduce code foot print and correct the bug :-)

License

Hi,

did you have a specific reason to use the GPL or is it possible for you to change the license for example to MIT or BSD? I would like to use your module in my application, but I don't want to use the GPL too.

Publish to bower

Since it is both a server and client module it would be nice to have it on bower also, what do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.