Skip to content

Knoema/Localization

Repository files navigation

How to install:

routes.IgnoreRoute("_localization/{*route}");
  • In web.config add base class for web pages to have an @R() extension:
 <system.web.webPages.razor>
    ...
    <pages pageBaseType="Knoema.Localization.Mvc.LocalizedWebViewPage">
    ...
  </system.web.webPages.razor>
  • For models localization add this code into Application_Start() in Global.asax:
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new ValidationLocalizer());
ModelMetadataProviders.Current = new MetadataLocalizer();
  • For localization if scripts add this to View:
@RenderLocalizationIncludes(User.IsInRole("Admin"))	 

Admins will see admin tool, where they can translate strings or import/export them.

How to use:

Localization in cshtml files:

<p>@R("Hello world!")</p>

With parameteres:

<p>@R("Hello {0}!", username)</p>

Localization in cs files:

"Hello world!".Resource(this)

Localization of models:

[Localized]
public class SignInViewModel
{
	[Required(ErrorMessage = "Please provide your e-mail")]
	[Display(Name = "E-mail")]
	public string EMail { get; set; }

	[Required(ErrorMessage = "Please type your password")]
	public string Password { get; set; }
}

Localization in javascript

$.localize(text, scope);

For scope you can use path to script file e.g. "~/scripts/shared/site.js".