现在的位置: 首页 > 综合 > 正文

c# MVC多語言的解決方案

2011年12月18日 ⁄ 综合 ⁄ 共 1004字 ⁄ 字号 评论关闭

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>

抱歉!评论已关闭.