C# 国际化mvc3-哪种方法是最好的?会话,cookie?
亲爱的各位,我正在开发一个mvc应用程序,该应用程序需要支持两种或两种以上的语言,并涵盖不同国家的用户界面。 我希望收到一些关于存储语言和门户的用户选择的最佳方法的指导。 我一直在阅读,似乎:C# 国际化mvc3-哪种方法是最好的?会话,cookie?,c#,asp.net-mvc-3,internationalization,C#,Asp.net Mvc 3,Internationalization,亲爱的各位,我正在开发一个mvc应用程序,该应用程序需要支持两种或两种以上的语言,并涵盖不同国家的用户界面。 我希望收到一些关于存储语言和门户的用户选择的最佳方法的指导。 我一直在阅读,似乎: 它可以存储在cookie中,将这些值作为永久数据读取,并在某些值更改时重新创建cookie 另一种情况是,他们更喜欢在会话中的httpcontext中设置信息 另一种方法是使用基本控制器更改数据,同时考虑区域设置选择 任何意见,赞成或反对者将不胜感激。提前谢谢。 brgds。会话/cookies是。我
- 它可以存储在cookie中,将这些值作为永久数据读取,并在某些值更改时重新创建cookie
- 另一种情况是,他们更喜欢在会话中的httpcontext中设置信息
- 另一种方法是使用基本控制器更改数据,同时考虑区域设置选择
- 在某个时候,你必须通过电子邮件发送一个链接到一个选择了语言的页面
- 搜索引擎优化
- 在开发和翻译过程中很容易在语言之间切换
- 负载平衡器没有问题
- 如果可以在url中定义语言,前端测试会更稳定
public class EnumResourceAttribute : DescriptionAttribute
{
public Type ResourceType { get; private set; }
public string ResourceName { get; private set; }
public int SortOrder { get; private set; }
public EnumResourceAttribute(Type ResourceType,
string ResourceName,
int SortOrder)
{
this.ResourceType = ResourceType;
this.ResourceName = ResourceName;
this.SortOrder = SortOrder;
}
}
创建另一个静态类,该类将为GetString和GetString提供扩展方法
public static class EnumHelper
{
public static string GetString(this Enum value)
{
EnumResourceAttribute ea =
(EnumResourceAttribute)value.GetType().GetField(value.ToString())
.GetCustomAttributes(typeof(EnumResourceAttribute), false)
.FirstOrDefault();
if (ea != null)
{
PropertyInfo pi = ea.ResourceType
.GetProperty(CommonConstants.ResourceManager);
if (pi != null)
{
ResourceManager rm = (ResourceManager)pi
.GetValue(null, null);
return rm.GetString(ea.ResourceName);
}
}
return string.Empty;
}
public static IList GetStrings(this Type enumType)
{
List<string> stringList = new List<string>();
FieldInfo[] fiArray = enumType.GetFields();
foreach (FieldInfo fi in fiArray)
{
EnumResourceAttribute ea =
(EnumResourceAttribute)fi
.GetCustomAttributes(typeof(EnumResourceAttribute), false)
.FirstOrDefault();
if (ea != null)
{
PropertyInfo pi = ea.ResourceType
.GetProperty(CommonConstants.ResourceManager);
if (pi != null)
{
ResourceManager rm = (ResourceManager)pi
.GetValue(null, null);
stringList.Add(rm.GetString(ea.ResourceName));
}
}
}
return stringList.ToList();
}
}
其中Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberHigh
&
Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberRoutine
是资源文件中的常量,或者您可以说其值在不同区域性中可用的字符串
如果您要在MVC体系结构中实现web应用程序,请创建一个属性
private IList result;
public IList Result
{
get
{
result = typeof(Priority).GetStrings();
return result;
}
}
在.cshtml
文件中,您可以将枚举绑定到dropdownlist,如:
@Html.DropDownListFor(model => Model.vwClinicalInfo.Priority, new SelectList(Model.Result))
嗨,达林,非常感谢你。处理此方法的任何建议或教程?。我最关心的是如何在我的应用程序中持久保存用户选择数据,考虑到他们不仅可以选择语言,还可以选择国家,因为它将覆盖不止一次。brgds@sebastian_h,你注意到我答案中的链接了吗?uauu,不,我读起来像文本一样。。抱歉@Darin_Dimitrov:S。我在过去读过他的帖子,甚至hi也发布了一个新的帖子,不鼓励使用这种方法,而是把重点放在路线上。。。但它似乎更关注url生成和seo问题。我会再读一遍。谢谢好的,看来url详细说明了国际化的要点。非常感谢你。!sebasitan@Malcom_Frexner但您在global asax中只使用了一条路线?很棒的信息@swati我将尝试您的解决方案。brgds!
@Html.DropDownListFor(model => Model.vwClinicalInfo.Priority, new SelectList(Model.Result))