开发者

What are the alternatives to MySQL + PHP for HTML/javascript i18n?

I am looking at supporting multiple languages for an HTML5 application which does not rely on PHP nor 开发者_StackOverflow中文版MYSQL. What are the best existing frameworks? Does anyone has experiences relying on sed?


The easiest way to support multiple languages without php nor mysql is to simply make a new page for every language. If you have all your javascript setup done in external files (like it should be done normally), then this method can be very useful


You have to prepare the localized text in your JavaScript file(s), then you can do it client side (ns.localize can be implemented better):

<!DOCTYPE html>
<html>
    <head>
        <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script>
        <script src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js'></script>
        <script>
            window.ns = window.ns || { };

            /* Begin file i18n.js */
            ns.language = 'en';
            ns.i18n = { };
            ns.localize = function () {
                jQuery.each(ns.i18n[ns.language], function (key, value) {
                    $('.' + key).text(ns.i18n[ns.language][key]);
                });
            };
            /* End file i18n.js */

            /* Begin file i18n.en.js */
            ns.i18n['en'] = { };
            ns.i18n['en']['title'] = 'Welcome';
            ns.i18n['en']['body'] = 'It works!';
            ns.i18n['en']['choice'] = 'Choose your language:';
            /* End file i18n.en.js */

            /* Begin file i18n.it.js */
            ns.i18n['it'] = { };
            ns.i18n['it']['title'] = 'Benvenuto';
            ns.i18n['it']['body'] = 'Funziona!';
            ns.i18n['it']['choice'] = 'Scegli la tua lingua:';
            /* End file i18n.it.js */

            $(document).ready(function () {
                ns.localize();
                $('.language').change(function () {
                    var language =$(this).val(); 
                    ns.language = language;
                    ns.localize();
                });
            });
        </script>
        <link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css' rel='stylesheet' />
        <title class='title'>i18n</title>
    </head>
    <body>
        <h1 class='title'>i18n</h1>
        <p class='body'>Test.</p>
        <p>
            <span class='choice'>Something:</span>
            <select class='language'>
                <option value='en'>English</option>
                <option value='it'>Italian</option>
            </select>
        </p>
    </body>
</html>


You'd need some dynamic (web server) language to replace the language, or you'd need to render all code once and serve a different copy for each language.

Zend_Translate may be helpful, if you want to go PHP.

You don't need to use MySQL, you could use arrays, CVS files, and a whole range of other methods.(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜