Search code examples
javascriptjquerylocalizationlocaleglobalization

localizing strings in javascript


We need to be able to localize strings in javascript - thinking for things like the app_offline.htm file etc.

jquery globalize is hectic and seems like total overkill. Is there a simple jquery plugin or anything really that will allow us to localize js strings?


Solution

  • At the risk of over simplifying:

    var globals = {
        en-US: {
            color:'color',
            cell:'cell phone'
        },
        en-GB: {
            color: 'colour',
            cell: 'mobile phone'
        }
    };
    

    To use:

    text = globals[lang].color;
    

    where lang = 'en-US' etc

    You can either generate that structure on the server and use resource files etc there, or just keep this object literal in a global.js or similar.