c# MVC多語言的解決方案

1.定義一個擴展類GlobalizationExtensions

代碼如下:

View Code
    public static class GlobalizationExtensions
{
/// <summary>
/// Render the globalize resource text
/// </summary>
/// <param name="helper">The HTML helper</param>
/// <param name="key">the reousece key</param>
/// <returns></returns>
public static string Global(this HtmlHelper helper, string key)
{
return Global(helper, null, key);
}

/// <summary>
/// Render the globalize resource text
/// </summary>
/// <param name="helper">The HTML helper</param>
/// <param name="classKey">the key specified the which resource shold load</param>
/// <param name="key">the resource key</param>
/// <returns></returns>
public static string Global(this HtmlHelper helper, string classKey, string key)
{
string ck = "language";
if (!string.IsNullOrEmpty(classKey))
ck = classKey;
try
{
return HttpContext.GetGlobalResourceObject(ck, key, CurrentCulture).ToString();
}
catch { return key; }
}

private static CultureInfo CurrentCulture
{
get
{
return System.Threading.Thread.CurrentThread.CurrentUICulture;
// HtmlUIExtensions.GetCultureInfo();
}
}}

2.在代碼中調用

<h3>@Html.Global("Login_DNAID")</h3>

posted @ 2012-03-14 20:15  無限遐想  阅读(920)  评论(0编辑  收藏  举报