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?
-
this is a nice approach http://stackoverflow.com/questions/381070/asp-net-mvc-localization-best-practice/12936708#12936708 – kbvishnu Nov 13 '12 at 02:21
6 Answers
Create your own Html helper and use it like <%= Html.Resource("Name") %>
Details are in blog post.

- 16,540
- 9
- 51
- 97

- 549
- 3
- 8
-
9Just so everyone is aware, the post Ihar links to now has an update. See: http://blog.eworldui.net/post/2008/10/ASPNET-MVC-Simplified-Localization-via-ViewEngines.aspx – Robert Claypool Aug 31 '09 at 14:13
-
Am already using a solution similar to this, but isn't this type of solution adding an extra overhead in terms of performance? – jsan Mar 22 '12 at 05:07
There is good solution for this available here
this article covers all aspects of localization asp.net mvc app

- 101
- 1
- 2
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; }
}

- 14,760
- 19
- 71
- 101
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/

- 201
- 2
- 2
-
1Why the -1 on this answer? There are [several](http://afana.me/post/aspnet-mvc-internationalization.aspx) [reputable](http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx) [examples](http://brianreiter.org/2011/03/23/simple-asp-net-mvc-globalization-with-graceful-fallback/) of this pattern and when it's indicated. The answerer here has a point: managing large text content in a .resx adds some friction, and may have layout or sizing impact on a largely textual view. This is a fundamental tradeoff, as illustrated in the links. – mcw Mar 14 '12 at 05:37
-
By FAR the worst solution - ever! If you need to scale across lot of languages and locales, this becomes a pretty big mess and is super painfull to maintenance. Better approach is to put all stuff in a localization DB, load the required content on app start (dont forget an update-cache-button on some page) + use some type of browser-interface/frontend to edit the strings. Also, this system scales well across a team of translators and "content-maintainers" (in your soution instead, they would need to be aware of HTML/CSS/JS, at least to a level at which they do not destroy stuff in the views). – johngrinder Apr 27 '14 at 15:58
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.

- 324
- 3
- 12
If the string to be localized is generated by the view (eg a label in front of a text field), then its localization should be in the View.
If the string is generated by the Controller, its localization should be there as well.

- 5,020
- 2
- 32
- 35