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 == nu开发者_JS百科ll)
{
//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);
}
}
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 );
精彩评论