MattWeiler / jsTalks

JavaScript Inernationalization Tool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JsTalks

JsTalks is a JavaScript inernationalization tool. Any JavaScript web-application that uses this tool will be able to be completly internationalized.

This tool will work while hosting your site from a web-server or running the files locally.

How To Use

Include the stringUtils.js, commonUtils.js and jsTalks.js files in folder js/jsTalks.

Language File (base - js/i18n/messages.js)
window.jsTalks.PRIVATE.loadedData = {
    "welcomeQuestion": "Hi, how are you?",
    "replyOptions": [
        "I'm good",
        "Not bad",
        "Ok",
        "Not so good",
        "Terrible"
    ]
}

HTML File
<!DOCTYPE html>
<html>
    <head>
        ...
        <!-- IE8 requires this to expose the JSON object. -->
        <!-- If you don't care about IE8, you can ommit this meta tag. -->
        <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
        ...
        <script type="text/javascript" language="javascript" src="js/jsTalks/stringUtils.js"></script>
        <script type="text/javascript" language="javascript" src="js/jsTalks/commonUtils.js"></script>
        <script type="text/javascript" language="javascript" src="js/jsTalks/jsTalks.js"></script>
        <script type="text/javascript" language="javascript" src="js/main.js"></script>
        ...
    </head>
    <body>
        ...
    </body>
</html>

JavaScript File (js/main.js)
var i18nMessages = null;

function i18nMessagesInitialized(successful)
{
    if ((successful === true) && i18nMessages.loaded)
    {
        var options = null;
        var tmpOptionIndex = 0;
        var tmpOptionValueName = 'replyOptions_' + (tmpOptionIndex + 1);
        var tmpOption = i18nMessages.getValue(tmpOptionValueName);
        while ((tmpOption !== null) && (tmpOption.trim().length > 0))
        {
            if (options === null)
            {
                options = tmpOption;
            }
            else
            {
                options += ', ' + tmpOption;
            }
            tmpOptionIndex++;
            tmpOptionValueName = 'replyOptions_' + (tmpOptionIndex + 1);
            tmpOption = i18nMessages.getValue(tmpOptionValueName);
        }
        prompt(i18nMessages.getValue('welcomeQuestion'), options);
    }
};

i18nMessages = window.jsTalks.loadLanguageConstants(null, 'js/i18n/', 'messages', '.js', i18nMessagesInitialized);

About

JavaScript Inernationalization Tool

License:MIT License


Languages

Language:JavaScript 100.0%