Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localize Strings in Javascript

I'm currently using .resx files to manage my server side resources for .NET.

the application that I am dealing with also allows developers to plugin JavaScript into various event handlers for client side validation, etc.. What is the best way for me to localize my JavaScript messages and strings?

Ideally, I would like to store the strings in the .resx files to keep them with the rest of the localized resources.

I'm open to suggestions.

like image 619
SaaS Developer Avatar asked Sep 19 '08 17:09

SaaS Developer


1 Answers

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]; 
like image 113
Joel Anair Avatar answered Oct 06 '22 05:10

Joel Anair