# 更改语言设置时时间格式不随之更改的修正 #
修改原因:在更换语言包的时候 DateFormatDropDownList 的值并不能随之更改。如:用户从zh-CN 换为 en-US 后 user.DateFormat 的
显示格式仍为yyyy年mm月dd日。
修改方法:将DateFormatDropDownList对象中Item的value改为Resource的名称,在保存的时候根据用户选择的语言获取相应的日期格式
step1 -- 为/Components/Components/ResourceManager.cs中的 GetResource 和 GetString 两个方法 分别添加一次接受Language
参数的重载
private static Hashtable GetResource (ResourceManagerType resourceType)
{
return GetResource(resourceType,null);
}
private static Hashtable GetResource (ResourceManagerType resourceType,string language)
{
ForumContext forumContext = ForumContext.Current;
Hashtable resources;
// string defaultLanguage = ForumConfiguration.GetConfig().DefaultLanguage;
// string userLanguage = Users.GetUser().Language;
// string cacheKey = resourceType.ToString() + defaultLanguage + userLanguage;
//-> chaozi 22/09/04 -->START
string defaultLanguage = ForumConfiguration.GetConfig().DefaultLanguage;
string userLanguage;
if(language == null)
userLanguage = Users.GetUser().Language;
else
userLanguage = language;
string cacheKey = resourceType.ToString() + defaultLanguage + userLanguage;
//-> chaozi 22/09/04 -->END
// Ensure the user has a language set
//
if (userLanguage == "")
userLanguage = defaultLanguage;
// Attempt to get the resources from the Cache
//
if (forumContext.Context.Cache[cacheKey] == null)
{
resources = new Hashtable();
// First load the English resouce, changed from loading the default language
// since the userLanguage is set to the defaultLanguage if the userLanguage
// is unassigned. We load the english language always just to ensure we have
// a resource loaded just incase the userLanguage doesn't have a translated
// string for this English resource.
//
LoadResource(resourceType, resources, "en-US", cacheKey);
// If the user language is different load it
//
if ("en-US" != userLanguage)
LoadResource(resourceType, resources, userLanguage, cacheKey);
}
return (Hashtable) forumContext.Context.Cache[cacheKey];
}
//************GetResource Method END*****************//
可以根据指定的语言获取资源
public static string GetString(string name)
{
return GetString(name,null);
}
public static string GetString(string name,string language)
{
// Hashtable resources = GetResource (ResourceManagerType.String);
//->chaozi 22/09/04
Hashtable resources = GetResource (ResourceManagerType.String , language);
if (resources[name] == null)
{
// LN 6/9/04: Changed to throw a forums exception
throw new ForumException(ForumExceptionType.ResourceNotFound, "Value not found in Resources.xml for: " + name);
}
return (string) resources[name];
}
//************GetString Method END*****************//
step 2 -- 修改/Controls/Utility/DateFormatDropDownList.cs 将Value直接设置为resource的名称
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F1Text"), "Utility_DateFormatDropDownList_F1"));
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F2Text"), "Utility_DateFormatDropDownList_F2"));
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F3Text"), "Utility_DateFormatDropDownList_F3"));
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F4Text"), "Utility_DateFormatDropDownList_F4"));
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F5Text"), "Utility_DateFormatDropDownList_F5"));
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F6Text"), "Utility_DateFormatDropDownList_F6"));
Items.Add(new ListItem(AspNetForums.Components.ResourceManager.GetString("Utility_DateFormatDropDownList_F7Text"), "Utility_DateFormatDropDownList_F7"));
step 3 --修改 /Controls/Utility/EditProfile.cs 的 Update_Click 方法
//-> chaozi 22/09/04
userToEdit.DateFormat = ResourceManager.GetString(dateFormat.SelectedValue,language.SelectedItem.Value);
/Admin/SiteSettings.aspx.cs
settings.DateFormat =ResourceManager.GetString(DateFormat.SelectedValue,"");