user156888
user156888

Reputation:

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?

Upvotes: 2

Views: 2088

Answers (2)

James Westgate
James Westgate

Reputation: 11444

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.

Upvotes: 2

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201508

The Globalize.js library, previously known as jquery-global or jQuery Globalize, is relatively small, but if you only need string localization (and not date and number localization as well), then it does not offer much more than a general setup: locale (culture) object containing the property messages. It is initialized to an empty object, and you are supposed to add properties to it, corresponding to your strings to be localized. And it has the simple method Globalize.localize() that selects a localized string for a key

To implement simple string localization, you do not necessarily need any library or plugin. You could just code some simple approach like that of Globalize.js; the general code is fairly simple, much less work than defining the actual localizations for each string. On the other hand, if you have localization needs, you might just as well use Globalize.js, preparing for other kinds of localization in the future.

Upvotes: 1

Related Questions