locked
Localizing mvc resources RRS feed

  • Question

  • User-738291421 posted

    I downloaded and installed the 1.5mb file on this page, the french localization of System.Web.Mvc.resources.

    I added it as a reference to my project, added it into the assemblies of web.config, and added <globalization uiCulture="fr" culture="fr-FR" /> into the system.web section.  Still, built in validation messages are coming up english.  What am I missing?  What did I do wrong?

    Monday, May 16, 2011 5:48 PM

Answers

  • User-1011137159 posted

     

     public class BaseController : Controller
        {
            private Menuservices dbService = new Menuservices();
            protected override void OnActionExecuted(ActionExecutedContext filterContext)
            {
    
    
                if (this.ControllerContext.Controller.GetType().Name != "HomeController")
                {
                    if (Session["UserID"] != null && Session["UserID"].ToString().Length > 0)
                    {
    
                    }
                    else
                    {
                        filterContext.Result = RedirectToAction("Index", "Home");
    
                    }
                }
                base.OnActionExecuted(filterContext);
    
         
            }
    
            public ActionResult Menu() 
            { 
                    var menuItems = from t in dbService.GetMenu(0)
                            select t; 
     
                return PartialView("Menu",menuItems);
              
            }
    
    
            protected override void ExecuteCore()
            {
                if (Session["iLanguageId"] == null)
                {
                    string browserLanguage = Request.UserLanguages[0];
    
                    if ((browserLanguage.ToString() == "en-US") || (browserLanguage.ToString() == "en-US") || (browserLanguage.ToString() == "en"))
                    {
                        Session["iLanguageId"] = "1";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
                    }
                    else if ((browserLanguage.ToString() == "nl-NL") || (browserLanguage.ToString() == "nl-nl") || (browserLanguage.ToString() == "nl"))
                    {
                        Session["iLanguageId"] = "2";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
    
                    }
                    else
                    {
                        Session["iLanguageId"] = "1";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
                    }
                }
                else
                {
                    if (Session["iLanguageId"].ToString() == "1")
                    {
                        Session["iLanguageId"] = "1";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
                    }
                    else
                    {
                        Session["iLanguageId"] = "2";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
    
                    }
                }
                
                base.ExecuteCore();
    
               
    
            }
    
        }

    you can set your locallization culture in your every page by ading base controller which is inherited in every controler class...

    and selecting language you need set your session....

    and while fetching value... in validation you can handle it with below....

     public int TeamId { get; set; }
            [Required(ErrorMessageResourceType = typeof(AP.Common.AP_Resources), ErrorMessageResourceName = "TeamName")]
            public string Name { get; set; }
    
    
    //// OR
    
    oMenu.DisplayName = AP_Resources.ResourceManager.GetString("Home"); //oResourceManager.GetString("<Keyname>");
                oMenu.DisplayName = AP_Resources.Home
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2011 2:11 AM
  • User-1011137159 posted

    to set different language you need to set different culture in current thread in each page is initiallization..

    Hence in my code I have added basecontroller that is exectutes in each page and set culture based in session ID.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2011 9:27 AM

All replies

  • User1682618242 posted

    Have you installed it as described here:

    http://haacked.com/archive/2011/05/10/localized-releases-of-asp-net-mvc-3-tools-update.aspx ?

    Tuesday, May 17, 2011 2:03 AM
  • User-1011137159 posted

     

     public class BaseController : Controller
        {
            private Menuservices dbService = new Menuservices();
            protected override void OnActionExecuted(ActionExecutedContext filterContext)
            {
    
    
                if (this.ControllerContext.Controller.GetType().Name != "HomeController")
                {
                    if (Session["UserID"] != null && Session["UserID"].ToString().Length > 0)
                    {
    
                    }
                    else
                    {
                        filterContext.Result = RedirectToAction("Index", "Home");
    
                    }
                }
                base.OnActionExecuted(filterContext);
    
         
            }
    
            public ActionResult Menu() 
            { 
                    var menuItems = from t in dbService.GetMenu(0)
                            select t; 
     
                return PartialView("Menu",menuItems);
              
            }
    
    
            protected override void ExecuteCore()
            {
                if (Session["iLanguageId"] == null)
                {
                    string browserLanguage = Request.UserLanguages[0];
    
                    if ((browserLanguage.ToString() == "en-US") || (browserLanguage.ToString() == "en-US") || (browserLanguage.ToString() == "en"))
                    {
                        Session["iLanguageId"] = "1";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
                    }
                    else if ((browserLanguage.ToString() == "nl-NL") || (browserLanguage.ToString() == "nl-nl") || (browserLanguage.ToString() == "nl"))
                    {
                        Session["iLanguageId"] = "2";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
    
                    }
                    else
                    {
                        Session["iLanguageId"] = "1";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
                    }
                }
                else
                {
                    if (Session["iLanguageId"].ToString() == "1")
                    {
                        Session["iLanguageId"] = "1";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
                    }
                    else
                    {
                        Session["iLanguageId"] = "2";
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
    
                    }
                }
                
                base.ExecuteCore();
    
               
    
            }
    
        }

    you can set your locallization culture in your every page by ading base controller which is inherited in every controler class...

    and selecting language you need set your session....

    and while fetching value... in validation you can handle it with below....

     public int TeamId { get; set; }
            [Required(ErrorMessageResourceType = typeof(AP.Common.AP_Resources), ErrorMessageResourceName = "TeamName")]
            public string Name { get; set; }
    
    
    //// OR
    
    oMenu.DisplayName = AP_Resources.ResourceManager.GetString("Home"); //oResourceManager.GetString("<Keyname>");
                oMenu.DisplayName = AP_Resources.Home
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2011 2:11 AM
  • User-738291421 posted

    I have set the culture in the web.config, and I installed the english version of mvc from the platform installer.  I cannot access the other languages from the platform installer, so I installed the resource packages from the individual download page.  However, after adding references to the localized resources, the engine still does not switch to using those strings for default .net strings, like if I use an out of the box validator.  The validation message keeps coming in as english, even though I can check the current culture and ui culture, and they are both set to french, and I have the french resource dll referenced.

    Tuesday, May 17, 2011 9:21 AM
  • User-1011137159 posted

    to set different language you need to set different culture in current thread in each page is initiallization..

    Hence in my code I have added basecontroller that is exectutes in each page and set culture based in session ID.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2011 9:27 AM