Localize Strings in Javascript

A basic JavaScript object is an associative array, so it can easily be used to store key/value pairs. So using JSON, you could create an object for each string to be localized like this:

var localizedStrings={
    confirmMessage:{
        'en/US':'Are you sure?',
        'fr/FR':'Est-ce que vous ĂȘtes certain?',
        ...
    },

    ...
}

Then you could get the locale version of each string like this:

var locale="en/US";
var confirm=localizedStrings['confirmMessage'][locale];

Leave a Comment