Code Monkey home page Code Monkey logo

iso639-2's Introduction

iso639-2

This ISO 639-2 JavaScript library works in NodeJS and modern web browsers.
It provides a function to get an array of all languages, sorted by alpha-3 code, alpha-2 code, native name, English name, or reading direction (LTR or RTL). It also provides a function to look up a language by the alpha-3 or alpha-2 code.

As of 14 July 2014, all 486 languages listed on the official Library of Congress web page are supported.

API

  • languages.getLanguages(sortBy, isAlpha2Code): Returns an array of sorted language objects. Default sortBy is "a3". Default isAlpha2Code is false.

  • languages.getLanguage(languageCode, isAlpha2Code): Returns a language object that matches the languageCode. Default isAlpha2Code is false.

  • languages.getLabel(propertyName): Returns the label associated with the propertyName. Valid property names are a3, a3t, a2, name, eng, dir.

  • languages.isValid(languageCode, isAlpha2Code): Returns true if a language object is found that matches the languageCode. Default isAlpha2Code is false.

  • languages.isRtl(languageCode, isAlpha2Code): Returns true if a language object is found that matches the languageCode and has a dir property value of rtl.

NodeJS Usage

    var languages = require('iso639-2');
    var langs = languages.getLanguages('eng');

    for (i=0, len=langs.length; i<len; i++) {
        lang = langs[i];
        console.log(lang.eng + ':');
        console.log('    ' + JSON.stringify(lang));
    }
    console.log('\n# of Languages: ' + i);

Browser Usage

<!doctype html>
<html>
<head>
    <title>ISO 639-2 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="iso639-2.js"></script>
    <style>
        body {
            background-color: #fff;
        }
        th {
            vertical-align: bottom;
            text-align: left;
            padding: 4px;
        }
        table {
            border-collapse: collapse;
        }
        td {
            vertical-align: top;
            border: 1px solid black;
            padding: 4px;
        }
    </style>
</head>
<body>
    <h2>ISO 639-2 Languages Module Test</h2>
    <div id="test"></div>
    <script>
        var i, len, lang,
        text = '<table><tr><th>' + languages.getLabel('eng') +
            '</th><th>' + languages.getLabel('name') +
            '</th><th>' + languages.getLabel('a3') +
            '</th><th>' + languages.getLabel('a3t') +
            '</th><th>' + languages.getLabel('a2') +
            '</th><th>' + languages.getLabel('dir') +
            '</th></tr>';


        var langs = languages.getLanguages('eng');

        for (i=0, len=langs.length; i<len; i++) {
            lang = langs[i];
            text += '<tr><td>' + lang.eng +
                '</td><td>' + lang.name +
                '</td><td>' + lang.a3 +
                '</td><td>' + lang.a3t +
                '</td><td>' + lang.a2 +
                '</td><td>' + lang.dir +
                '</td></tr>';

        }
        text += '</table>';

        text = '<h3># of Languages: '+i+'</h3>'+text;

        document.getElementById('test').innerHTML = text;

    </script>
</body>
</html>

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.