Validations and Localization

Apr 30, 2008 at 6:47 AM
Since I'm not not a native English speaker I would want the validations to be in my own language. And I'll guess that you want it in yours.
I also guess that that goes for the whole web site.

I confess that I haven't worked that much with resource dlls, and that using a own solution might now be the ultimate way of doing things.
But! My solution can also support resource dlls (although I havent implemented it).

I've currently made a text file implementation which also uses FileSystemWatcher to reload the resources if the files have been changed on disc.

The textfile looks like this
1053: Swedish
  Welcome: Välkommen
  Goodbye: Hejdå
    Required: Fältet '{0}' är obligatoriskt.
    Integer: {0} måste vara ett heltal.
1033: US-English
  Welcome: Welcome
  Goodbye: Bye bye
    Required: {0} is required.
    Integer: {0} must be an integer.

The language is loaded into validator by using:
Validator.Language = LangMgr.GetCategory("Validator");

How do you use the localizations in the webs then?

It's easy, the language manager automatically checks which language that should be used. And the language handling is built into the ViewControllers (new class, see the other post) and Views.

A controller:
class UserController : ViewController
    public string Index()
      return Language["Hello"] + Id;
    public string Show()
      // will render the application layout plus the action layout called "Show"
      // it will also let the action layout use a variable called "user" with the contents of a user object
      return Render("user", AuthedUser);
The controller will check after a language category named the same as the controller, in this case "User". It will fallback to the default category if the text is not found.

The view
<%= Language["Goodbye"] + " " + user.FirstName %>

Or if you prefer haml:

= Language["Goodbye"] + " " + user.FirstName