如何找出 Web 项目中支持哪些文化 -> 类 CultureInfo?
Posted
技术标签:
【中文标题】如何找出 Web 项目中支持哪些文化 -> 类 CultureInfo?【英文标题】:how to find out which culture's are supported in webproject -> Class CultureInfo? 【发布时间】:2011-01-13 09:39:30 【问题描述】:在我的项目中,我有两种支持的语言(德语 - “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】:您能否尝试加载指定语言的文化,如果不存在则加载默认语言?我已经在我的一个网络应用程序中做到了这一点。您可以在基本页面中使用类似的内容。
/// <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 );
【讨论】:
这正是我不想做的。如果要添加西班牙语或法语,则需要手动在代码中添加新语言。我想阅读资源文件中的语言。然后如果不存在则加载默认语言。 代码首先尝试将文化设置为此。CurrentCustomCulture 是每个用户变量,您不需要添加任何代码,只需为新语言添加新资源文件以上是关于如何找出 Web 项目中支持哪些文化 -> 类 CultureInfo?的主要内容,如果未能解决你的问题,请参考以下文章