如何找出 webproject 支持哪些文化 ->班级文化信息?
在我的项目中,我有两种支持的语言(德语 - “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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否可以尝试加载指定语言的区域性,如果不存在则加载默认语言?我已经在我的一个网络应用程序中做到了这一点。您可以在基本页面中使用类似的内容。
如果您想从服务器读取可用的文化,您可以使用
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.
If you want to read available cultures from the server you can use