2018年7月10日 星期二

ASP.NET MVC Multi Language: 多國語言 功能

1. Establish Resource directory, add new item > resource files. (.resx) 




Note: Add the GlobalRes.resx file with all the content that you need to translate. Here I have a English version as GlobalRes.resx and Chinese version as GlobalRes.zh-TW.resx. Notice that you need to feel the ISO country code for the postfix data name. 

  1. Set type to Strings 
  2. Access Modifier as Public 
  3. In properties, set Build Action to Embedded Resource.


2. Controller for language change. (And view for Index())

using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace IrsaWebStore.Controllers
{
public class LanguageController : Controller
{
// GET: Language
public ActionResult Index()
{
return View();
}

public ActionResult Change(string LanguageAbbreviation)
{
if(LanguageAbbreviation != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbreviation);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbreviation);
}

HttpCookie cookie = new HttpCookie("Language")
{
Value = LanguageAbbreviation
};
Response.Cookies.Add(cookie);

return View("Index");
}
}
}




Note: Add new controller in Controller folder.

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@IrsaWebStore.Resources.GlobalRes.Language</h2>

<ul>
<li>@Html.ActionLink(@IrsaWebStore.Resources.GlobalRes.English, "Change", "Language", new { LanguageAbbreviation = "en" }, null)</li>
<li>@Html.ActionLink(@IrsaWebStore.Resources.GlobalRes.Chinese_tw, "Change", "Language", new { LanguageAbbreviation = "zh-TW" }, null)</li>
</ul>

3. In Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];

if (cookie != null && cookie.Value != null)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
}
else
{
HttpContext.Current.Response.AppendCookie(new HttpCookie("Language"));
HttpContext.Current.Request.Cookies["Language"].Value = "zh-TW";
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-TW");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-TW");
}

}

Note: Here I set zh-TW as default cookie value. Otherwise the default language would be the .resx file without country code. (e.g. GlobalRes.resx, instead of GlobalRes.zh-TW.resx)

4. Web.config settings.

<system.web>

<!--Globalization-->
<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true" />
<!--/Globalization-->

<!-- ... -->
<!-- ... -->

</system.web>


5. Route rules (App_start > routeConfig.cs)

routes.MapRoute("Language", "Language/{action}/{id}",
new { controller = "Language", action = "Index", id = UrlParameter.Optional }, new[] { "IrsaWebStore.Controllers" });


6. Modify Views (View > Shared >  _Layout.cshtml as example)

<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@Html.Action("PagesMenuPartial", "Pages")
@if (Request.IsAuthenticated)
{
<li><a href="/account/logout">@IrsaWebStore.Resources.GlobalRes.Logout</a></li>
}
else
{
if (ViewBag.Title == "Login")
{
<li class="active"><a href="/account/login">@IrsaWebStore.Resources.GlobalRes.Login</a></li>
}
else
{
<li><a href="/account/login">@IrsaWebStore.Resources.GlobalRes.Login</a></li>
}

if (ViewBag.Title == "CreateAccount")
{
<li class="active"><a href="/account/create-account">@IrsaWebStore.Resources.GlobalRes.Create_Account</a></li>
}
else
{
<li><a href="/account/create-account">@IrsaWebStore.Resources.GlobalRes.Create_Account</a></li>
}
}

<!-- ... -->


沒有留言:

張貼留言