views:

5414

answers:

9

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.

+2  A: 

I would use an associative array:

var phrases=[]
phrases['fatalError'] ='On no!'

Then you can just swap the JS file, or use an Ajax call to redefine your phrase list.

Diodeus
Note that this isn't correct. phrases will be an Array, but you're just setting one of its properties to a string (eg phrases.fatalError = 'Oh no'; is equivalent to your second line). Use object literals for cleanliness instead.
millenomi
+1  A: 

Expanding on diodeus.myopenid.com's answer: Have your code write out a file containing a JS array with all the required strings, then load the appropriate file/script before the other JS code.

_Lasar
+5  A: 

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 est certain?',
        ...
    },

    ...
}

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

var locale='en/US';
var confirm=localizedStrings['confirmMessage'][locale];
Joel Anair
The problem with this approach is that you load all strings for all languages. However its likely the server will know either through browser provided clues or through user preferences what language is needed. Sending a single language file would be better.
AnthonyWJones
+2  A: 

Inspired by SproutCore You can set properties of strings:

'Hello'.fr = 'Bonjour';
'Hello'.es = 'Hola';

and then simply spit out the proper localization based on your locale:

var locale = 'en';
alert( message[locale] );
+3  A: 

With a satellite assembly (instead of a resx file) you can enumerate all strings on the server, where you know the language, thus generating a Javascript object with only the strings for the correct language.

Something like this works for us (VB.NET code):

Dim rm As New ResourceManager([resource name], [your assembly])
Dim rs As ResourceSet = 
    rm.GetResourceSet(Thread.CurrentThread.CurrentCulture, True, True)
For Each kvp As DictionaryEntry In rs
    [Write out kvp.Key and kvp.Value]
Next

However, we haven't found a way to do this for .resx files yet, sadly.

Erik Hesselink
+3  A: 

This looks quite neat:

Localize text in JavaScript files in ASP.NET

This is a good solution. Most solutions I've found require either passing all language strings to the client in order to pick one, or keeping a set of .js files in every language. This one avoids both of those pitfalls; all you need is a resource file in each language, which is the same thing you'd need if localizing C# code.
Kyralessa
+1  A: 

JSGettext does an excellent job -- dynamic loading of GNU Gettext .po files using pretty much any language on the backend. Google for "Dynamic Javascript localization with Gettext and PHP" to find a walkthrough for JSGettext with PHP (I'd post the link, but this silly site won't let me, sigh...)

A: 

Is there a way to localize Java Script without putting any of the localizations in the code? In other words the requirement is that the application needs to be translated into Spanish. So for example: a Spanish resource file is added, and you are done. The application translates, no need to crack open the code and add translations, or localization identifiers, in potentially a 100 different places that may have Java script validations.

Frustrated
+1  A: 

The MSDN way of doing it, basically is:

You create a separate script file for each supported language and culture. In each script file, you include an object in JSON format that contains the localized resources values for that language and culture.

I can't tell you the best solution for your question, but IMHO this is the worst way of doing it. At least now you know how NOT to do it.

BrunoSalvino