如何找出 webproject 支持哪些文化 ->班级文化信息?

发布于 2024-10-11 23:11:14 字数 2242 浏览 1 评论 0原文

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

是否可以动态读取当前 Web 项目支持哪些语言? 如果它们只是“de”和“en”,我不想手动验证它,就像我在以下代码中所做的那样:

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

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

我想访问网络项目语言,例如:CultureInfo.(谁知道)..。 如果我要添加一种新语言,fE“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);

            }
        }

in my project i have two supported languages (german - "de", and english - "en")
I have resource files like: Home.aspx.de.resx, Home.aspx.en.resx, Home.aspx.resx

is it possible to read dynamicly which languages are supported in current webproject?
I dont want to validate it manually if they are just "de" and "en" like I do in follow code:

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

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

I would like to get access of webproject languages like: CultureInfo.(Who knows)...
If I will add a new language, f.E. "fr" I need again to add it manually into my logic.

my Application_AcquireRequestState method look like this:

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);

            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-10-18 23:11:14

您是否可以尝试加载指定语言的区域性,如果不存在则加载默认语言?我已经在我的一个网络应用程序中做到了这一点。您可以在基本页面中使用类似的内容。

        /// <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 );

和 CultureInfo.GetCultures( CultureTypes.UserCustomCulture );

Can you try to load the culture for the specified language and if it does not exist then load the default? I have done that in one of my web apps. You would use something like this in the base page.

        /// <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;
        }
    }

If you want to read available cultures from the server you can use


using System.Globalization;

CultureInfo.GetCultures( CultureTypes.AllCultures );

and CultureInfo.GetCultures( CultureTypes.UserCustomCulture );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文