18

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?

1

6 Answers 6

14

Create your own Html helper and use it like <%= Html.Resource("Name") %>

Details are in blog post.

Sign up to request clarification or add additional context in comments.

2 Comments

Just so everyone is aware, the post Ihar links to now has an update. See: blog.eworldui.net/post/2008/10/…
Am already using a solution similar to this, but isn't this type of solution adding an extra overhead in terms of performance?
10

There is good solution for this available here

this article covers all aspects of localization asp.net mvc app

Comments

4

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

3

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

Why the -1 on this answer? There are several reputable examples 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.
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).
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.