如何找出Web项目中支持的文化 - > Class CultureInfo?

时间:2011-01-13 09:39:31

标签: asp.net-mvc culture cultureinfo

在我的项目中我有两种支持的语言(德语 - “de”和英语 - “en”) 我有资源文件,例如:Home.aspx.de.resx, Home.aspx.en.resx, Home.aspx.resx

是否可以动态地阅读当前webproject支持哪些语言? 我不想手动验证它,如果它们只是“de”和“en”,就像我在跟随代码中那样:

if (langName != "en" || langName != "de")
                    {
                        ci = new CultureInfo("en");

                        this.Session["Culture"] = ci;
                    }

我想访问webproject语言,例如:CultureInfo.(Who knows)..。 如果我要添加一种新语言,f.E。 “fr”我需要再次将其手动添加到我的逻辑中。

我的Application_AcquireRequestState方法如下所示:

protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                //Create culture info object 
                CultureInfo ci = (CultureInfo)this.Session["Culture"];

                //Checking first if there is no value in session 
                //and set default language 
                //this can happen for first user's request
                if (ci == null)
                {
                    //Sets default culture to english invariant
                    string langName = "en";

                    //Try to get values from Accept lang HTTP header
                    if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                    {
                        //Gets accepted list 
                        langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);

                    }
                    if (langName != "en" || langName != "de")
                    {
                        ci = new CultureInfo("en");

                        this.Session["Culture"] = ci;
                    }
                    else
                    {
                        ci = new CultureInfo(langName);
                        this.Session["Culture"] = ci;
                    }
                }

                //Finally setting culture for each request
                Thread.CurrentThread.CurrentUICulture = ci;
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

            }
        }

1 个答案:

答案 0 :(得分:2)

您是否可以尝试加载指定语言的文化,如果它不存在则加载默认值?我在我的一个网络应用程序中完成了这项工作。你可以在基页中使用这样的东西。

        /// <summary>
    /// Initializes culture for the page
    /// </summary>
    [VersionChange( "6.1.34.89", "24/12/2009", "Custom Cultures added" )]
    protected override void InitializeCulture()
    {
        try
        {
            CultureInfo oCultureInfo;

            try
            {
                oCultureInfo = CultureInfo.CreateSpecificCulture( this.CurrentCustomCulture );
            }
            catch ( ArgumentException )
            {
                //Get culture info based on Great Britain
                CultureInfo cultureInfo = new CultureInfo( "en-GB" );
                RegionInfo regionInfo = new RegionInfo( cultureInfo.Name );

                CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder( this.CurrentCustomCulture, CultureAndRegionModifiers.None );

                cultureAndRegionInfoBuilder.LoadDataFromCultureInfo( cultureInfo );
                cultureAndRegionInfoBuilder.LoadDataFromRegionInfo( regionInfo );

                // Custom Changes
                cultureAndRegionInfoBuilder.CultureEnglishName = this.CurrentCustomCulture;
                cultureAndRegionInfoBuilder.CultureNativeName = this.CurrentCustomCulture;

                cultureAndRegionInfoBuilder.Register();

                oCultureInfo = CultureInfo.GetCultureInfo( this.CurrentCustomCulture );
            }
            catch ( Exception )
            {
                throw;
            }

            Thread.CurrentThread.CurrentCulture = oCultureInfo;
            Thread.CurrentThread.CurrentUICulture = oCultureInfo;

            Page.Culture = oCultureInfo.Name;
            Page.UICulture = oCultureInfo.Name;

            base.InitializeCulture();
        }
        catch ( Exception )
        {
            throw;
        }
    }

如果您想从服务器读取可用的文化,可以使用


using System.Globalization;

CultureInfo.GetCultures( CultureTypes.AllCultures );

and CultureInfo.GetCultures( CultureTypes.UserCustomCulture );