I need help with the best practice to localize asp mvc apps, I saw Oxite having a base method named Localize in the BaseController, but is the Localization a task for the view or the Controller? Or should I use resx files / or use db tables?
6 Answers
Create your own Html helper and use it like <%= Html.Resource("Name") %>
Details are in blog post.
2 Comments
There is good solution for this available here
this article covers all aspects of localization asp.net mvc app
Comments
Since this a one year question and I don't know the scope of my answer over here. Recently I faced a situation like this , ie I need to implement the localization for different languages in my mvc site.
I considered using Resource file.
its very easy to implement, but the issue is that during the phase of development, we need to specify the localized strings. So if it a multi language support, we need to make the resource file for every language. If client what to change or add a new language, its very difficult and we need to provide a build.
Second I consider the Satelite Assemblies.
Its also similar to Resource, but it gives a freedom to edit the assemblies out side and put it back to bin folder. This also requires a lot of effort to client/developer.
Third I considered storing in db. This approach is fine and we have some mechanism to read data from the server. This requires one time effort and client doesn't have any dependable .
I override a custom DisplayNameAttributre and from the constructor I will pass DB and get the data to render
Based on your requirement it should display the view to you.
Resource Manager
/// <summary>
/// Extended display attribute which will handles the request
/// It will call every time when the property is rendered (return View() - from controller)
/// </summary>
public class ResourceManagerAttribute : DisplayNameAttribute
{
public ResourceManagerAttribute(string resourceKey, string resourceNameSpace = "")
: base(GetDisplayName(resourceKey, resourceNameSpace))
{ }
private static string GetDisplayName(string resourceKey, string resourceNameSpace = "")
{
// get the browser's prefered language.
string browserLanguage = HttpContext.Current.Request.UserLanguages.First();
// Get the locale data for that property and displays.
switch (browserLanguage)
{
case "en-US": return "Eng " + resourceKey;
// calls db based on resource key
case "hi": return "Hin " + resourceKey;
}
return "-- Not Implemented Now -- ";
}
ViewModel
public class HomeViewModel
{
//calls the resource
[ResourceManager("MID")]
public int MID { get; set; }
[ResourceManager("Name")]
public string Name { get; set; }
[ResourceManager("Addess")]
public string Addess { get; set; }
}
Comments
MVC is really more about using the right view for the right job. Putting everything in a resource file is extremely paintfull. It's good to use resource files for small things, but for larger pages like your description pages, its better to have a view in each culture with a lot of content. For example using the following structure: ~/Views/en-US/Home/Index.aspx ~/Views/pt-BR/Home/Index.aspx or this structure: ~/Views/Home/Index.en-US.aspx ~/Views/Home/Index.en-US.aspx
read the blog for how to do it: http://blog.oimae.com/2011/02/20/cultured-view-engine-for-mvc/
2 Comments
I would better go for creating a custom MetadataProvider and using a convention for the models. Something like 1 resource file by model namespace and a convention like ModelName.PropertyName -> value
For validators, common buttons and so a resource file.
For views text i am actually trying to find a good way. Maybe a view pre-process before compilation and a custom Scope for localized texts, so the pre-process can create the resource file for each view with the default language.