How to change CurrentCulture at runtime?

前端 未结 3 2169
滥情空心
滥情空心 2020-11-29 10:47

I need to change cultures at runtime according to resource files for each culture.

I need to change the attributes of the controls in my form, according to two cultu

相关标签:
3条回答
  • 2020-11-29 11:33

    max set me on the right path , nothing i haven't come across before , but it did help me make a minor adjustment to the msdn documentation on the matter :

    http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

        string defaultLanguage = Thread.CurrentThread.CurrentUICulture.ToString();                         
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected override void InitializeCulture()
        {
            if (Request.Form["ListBox1"] != null)
            {
                String selectedLanguage = Request.Form["ListBox1"];
                UICulture = selectedLanguage;
                Culture = selectedLanguage;
    
                Thread.CurrentThread.CurrentCulture = new CultureInfo(selectedLanguage);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(defaultLanguage);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(defaultLanguage);
            }            
            base.InitializeCulture();
        }   
    

    the list box contains different cultures the first and selected one is also the default culture , which i save on the page load , on other loads it as no effect because the listbox already as a value .

    0 讨论(0)
  • 2020-11-29 11:42

    I have not been able to get the "fallback" as described here to work. I'm using Global resource files for language and when the label is missing from the user selected culture file it does not default back to a label in default culture? I ended up creating a method to perform the fallback. I was searching for better ways to temp change the culture (when label not found) and stumbled on this post so I thought I'd and some content.

    In a utility class of mine: public String getLabelResource(String sLabelID, String sLangCd) {

            cLogger oLogger = new cLogger();
    
            try
            {
                Object sLabel;
                sLabel = HttpContext.GetGlobalResourceObject("{filename}", sLabelID);
                if (sLabel.ToString() == "") //label was not found in selected lang
                {
                    //default to US language resource label
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
                    sLabel = HttpContext.GetGlobalResourceObject("{filename}", sLabelID);
                    //switch global lang back to selected
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLangCd);
                }
                return sLabel.ToString();
    
            }
            catch (Exception ex)
            {
                oLogger.LogWrite("cUtils.cs", "getLabelResource", ex.Message, false);
                return String.Empty;
            }
        }
    
    0 讨论(0)
  • 2020-11-29 11:45

    Changing the current UI culture:

    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("he-IL");
    

    or better, retrieve a cached read-only instance of the he-IL culture:

    System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("he-IL");
    

    At run time, ASP.NET uses the resource file that is the best match for the setting of the CurrentUICulture property. The UI culture for the thread is set according to the UI culture of the page. For example, if the current UI culture is Spanish, ASP.NET uses the compiled version of the WebResources.es.resx file. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads the default resource file. In this example, the default resource file is WebResource.resx.

    0 讨论(0)
提交回复
热议问题