1

我最近一直在考虑本地化我的网站,但对 Google 搜索有点空白。我一直在寻找如何本地化应用程序,但不是网站或 GeoLocation,这有点太具体了。

我希望能够主要使用纯 HTML(5) 和 CSS(3) 进行本地化。我希望能够本地化页面中的部分,而不是为每个国家/地区设置单独的 URL。

对此的任何帮助都会非常有用,因此在此先感谢。

编辑:如果没有纯 HTML(5) 或 CSS(3) 方式来执行此操作,则任何服务器端、js 或其他语言的示例都会很有用。

4

1 回答 1

0

This solution is one I use in an ajax application with almost no html (everything is generated from the data client side).

The first part ensures you can specify the language in the url (by adding &lang=it for example) and that it is stored in local storage (permanent user preferences in the browser) :

myapp.available_langs = ['en', 'fr', 'it']; // the first one is the default language
// Exemples :
//   {en:'hi', fr:'Bonjour'}
//   {fr:'Bonjour'}
myapp.loc = function(map) {
    if (map[myapp.lang]) return map[myapp.lang];
    for (var i=0; i<myapp.available_langs.length; i++) {
        if (map[myapp.available_langs[i]]) return map[myapp.available_langs[i]];
    }
    return "-label not found-";
};

myapp.getUrlParameter = function(name, defaultValue) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var results = new RegExp( "[\\?&]"+name+"=([^&#]*)" )
         .exec( document.location.href );
    if( results == null ) return defaultValue;
    else return decodeURIComponent(results[1]);
};

myapp = {};
myapp.lang = localStorage['lang'] = myapp.getUrlParameter('lang', localStorage['lang']||myapp.available_langs[0]);

And it is used like this at some other place :

$('body').append('<div product id=product_header>\
    ...
        <tr>\
            <td> '+myapp.loc({en:'cut',fr:'date'})+': <span id=product_cutdate>?</span></td>\
            <td> '+myapp.loc({en:'grade',fr:'qualité'})+': <span id=product_grade>?</span></td>\
            <td> '+myapp.loc({en:'length',fr:'longueur'})+': <span id=product_length>?</span> m</td>\
            <td> '+myapp.loc({en:'thickness',fr:'épaisseur'})+': <span id=product_thickness>?</span> mm</td>\
            <td> '+myapp.loc({en:'width',fr:'largeur'})+': <span id=product_width>?</span> m</td>\
        </tr>\

You might choose to use also the user agent but in real corporate use I find it about useless. Note that i18n solutions can be very various. I don't advocate to use this, this is simply a pattern I like. Many people prefer to extract the strings in external properties files but in my experience it leads to more bad translations and is harder to manage (as long as only one team speak all the involved languages and that you don't have 10 of them).

于 2012-11-29T21:07:31.627 回答