Ayende @ Rahien

It's a girl

Handling javascript localization in Mono Rail

I run into the issue of having to alert the user of some error, and it brought home the fact that English & Hebrew are not easy to mix. In fact, code such as this is consider a very bad sign, usually code that you don't really want to touch, see or believe in:

image

So, we need some sort of a way to externalize those strings, even if the only language that this application is going to use is Hebrew, simply because of the pain of mixing the two together.

After thinking about it for a while, I decided to create this view (JavascriptResources/index.brail):

<%
for de in controller.Resources:
	resourceName = de.Key
%>
	var ${resourceName} = {
	<% for item in GetParameter(resourceName): %>
		${item.Key}: '${item.Value.Replace("'","\\'") }',
	<% end %>
		Empty: ''
	};
<%
end
%>

And here is the definition of the controller:

[Resource("Processes", "MyApp.Resources.Javascript.Processes")]
[Resource("Errors", "MyApp.Resources.Javascript.Errors")]
[Resource("Messages", "MyApp.Resources.Javascript.Messages")]
public class JavascriptResourcesController : Controller
{
	public void Index()
	{
		
	}
}

But what the hell does this do, anyway? Not much, actually, but it will iterate over all the registered resources for the controller, and output something that looks like this for each of those resources:

var Processes = {
 	EmploymentType: 'Employment Type',
	Details: 'Details',
	Dates: 'The dates',
	Empty: ''
};

The nice thing is that I can now write: alert(Errors.RecursiveError);*. As a side affect (as I said, the application is just in Hebrew so I don't care about that much), I also get the usual benefits of localization.

* You have to read Hebrew to get the joke, I am afraid.

Comments

Justin-Josef Angel [MVP]
10/07/2007 07:17 PM by
Justin-Josef Angel [MVP]

The horrer.

It's not strongly typed! it's evil!

This is string based reference - the anti christ!

Seriously, anything gained in "easy globalzation" is lost in "oops, I changed the name of this file/group/field and everything compiled OK".

Any managed code that's compiled to Javascript should be as strongly typed as possiable as javascript is far harder to debug then managed code.

Ayende Rahien
10/07/2007 07:28 PM by
Ayende Rahien

You get a null object if you mistype something, that about it.

I know that you did a lot of stuff with JS and strong typing, but I don't see the need for those in many many cases.

Debugging JS is very easy, usually requiring merely that I would be able to run the JS in FireBug.

Debugging JS in IE is to be avoided.

Will Sahatdjian
10/08/2007 06:09 PM by
Will Sahatdjian

Its 2007, your users deserve a higher level of usability than the obnoxious alert dialog. For all the time spent coming up with beautiful APIs you should round out all those amazing dev skills.

http://clientside.cnet.com/code-snippets/event-scripting/confirmer-widget/

With a longer delay and a better color choice you have yourself a big improvement.

Ayende Rahien
10/08/2007 06:38 PM by
Ayende Rahien

Will,

My users are using MS CRM, obviously usability isn't a concern.

That is actually fitting in to the standard UI practices in the application, not an insult, I am more direct with those.

Comments have been closed on this topic.