Multilingual applications

You can either create own classes to load the languages or use the only implemented version (yaml files). To create your own, look at languages\languagemanager and languagecategory interfaces.

The language implementation uses categories to divide language translations into more manageable classes. Each category can be loaded from different sources.

The ViewController contains some magic which makes it really easy to use the language. It uses the controller name to find the correct category and the threads locale to select the correct language.

public class ItemController : ViewController
{
  public string Welcome()
  {
    // renders public\views\item\welcome.haml which contains the variable "text"
    return Render("text", Language["Welcome"]);
  }

  public LanguageCategory Language
  {
    get { return _langMgr[ControllerName]; }
  }

  public string Submit()
  {
    // validate data
    Validator validator = new FormValidator(Request.Form["user"], Errors, Language);

    //true means that the field is required
    string userName = validator.LettersOrDigits("username", true); 

    // password is letters, digits and all chars specified in Validator.PasswordChars (public static string)
    string password = validator.Password("password", true); 


    if (validator.ContainsErrors)
      return RenderErrors();
    else
    {
      //do something with the validate data
    }
  }
}


The above code would produce errors like:
Fältet 'Användarnamn' kan endast innehålla siffror och bokstäver.
'Username' may only contain letters or digits.


depending on the language.

YAML files

They look something like this:

	1053: Swedish
		Login: Logga in
		Logout: Logga ut
		Validator:
			Required: Fältet '{0}' är obligatoriskt.
			Integer: '{0}' måste vara ett heltal.
			Email: '{0}' är inte en korrekt epost.
			Date: '{0}' är inte ett korrekt datum.
			Time: {0} är inte en korrekt tid.
		user:
			LoginFailed: Felaktigt användarnamn eller lörsenord.
			Goodbye: Tack och adjö.
			Username: Användarnamn
			Password: Lösenord
			Login: Logga in
		test:
			Add: Lägg till
			List: Lista
			SelectTest: Välj ett test
			CopyTest: Kopiera testet
			AddCase: Skapa testfall
			Reset: Nollställ
			IdMissing: Id saknas
			MissingState: Status är inte angivet.
			AvailableTests: Tillgängliga tester
			SelectItems: Välj de testfall som ska inkluderas.
			Name: Namn
			CreateTest: Skapa testet
		item:
			Name: Namn
			Description: Beskrivning
	1033: En-us
		Login: Login
		Logout: Log out
		Validator:
			Required: '{0}' is required.
			Integer: '{0}' must be an integer.
			Email: '{0}' is not a valid email.
			Date: '{0}' is not a correct date.
			Time: '{0}' is not a correct time.
		user:
			LoginFailed: Incorrect username or password.
			Goodbye: Welcome back anytime amigo!
			Username: Username
			Password: Password
			Login: Login
		test:
			Add: Add
			List: List
			SelectTest: Select a test
			CopyTest: Copy test
			AddCase: Create test case
			Reset: Reset 
			IdMissing: Id was not specified.
			MissingState: State was not specified.
			AvailableTests: Available tests
			SelectItems: Select the items to include
			Name: Namn
			CreateTest: Create new test
		item:
			Name: Name
			Description: Description

Last edited May 7, 2008 at 3:07 PM by jgauffin, version 3

Comments

No comments yet.