C# 切換語言 Asp.Net MVC使用Resource.resx 資源文件


1,在項目中添加 App_GlobalResources(全局文件資源) 文件夾。

 

2,在資源文件夾上面右擊,Add ——>New Item 添加需要的語言資源文件。

 3,向里面添加了2個資源文件,Resource.resx是英文資源,Resource.zh-cn.resx是中文資源文件

4,這里寫一個登陸頁面中切換語音,在資源文件中添加相應的語言內容,Name是key,Value是要顯示的內容

      保持2個資源文件中的key是一樣的,才好在英文和中文之間切換

 

 5,前端頁面中引用 @using Resources,引用后在需要顯示語音的位置處  @Resource.Key(如:"@Resource.userName 就會顯示用戶名)就可以

 

@{
    Layout = null;
}

<!DOCTYPE html>
@using Resources
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
    <div>
        
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">@Resource.userName</label> <!-----調用在資源文件中寫的key----->
                <div class="col-sm-10">
                    <input type="email" class="form-control" id="inputEmail3" name="inputEmail3" placeholder="Email">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">@Resource.password</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="inputPassword3" name="inputPassword3" placeholder="Password">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                        <label>
                            <input type="checkbox"> Remember me
                        </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">@Resource.login</button>
                </div><br />
                <!--------這里切換語音調用 Language控制器中的 Change方法,傳遞相應的語言參數過去--------->
                <div class="col-sm-offset-2 col-sm-10">
                    @Html.ActionLink("English", "Change", "Language", new { LanguageAbb = "en" }, null) &nbsp;&nbsp;
                    @Html.ActionLink("簡體中文", "Change", "Language", new { LanguageAbb = "zh-cn" }, null)
                </div>
            </div>
    </div>
</body>
</html>

6,Language控制器中的change方法代碼如下

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

            string login = Resources.Resource.login; //也可以在c#中調用資源文件的內容

            //可以更新用戶的語言設置到數據庫
            //if (true)
            //{
            //    var userID = GetUserID();
            //    var user = db.AspNetUsers.FirstOrDefault(x => x.ID == userID);
            //    if (user != null)
            //    {
            //        user.Language = LanguageAbb;
            //        db.SaveChanges();
            //    }
            //}


            //更新用戶語言
            HttpCookie cookie = new HttpCookie("Language");
            cookie.Value = LanguageAbb;
            Response.Cookies.Add(cookie);
            return Redirect(Request.UrlReferrer.ToString());
        }

 7,在頁面請求時設置語言,向Global.asax.cs  中添加方法

        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
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en"); //這里會默認選擇英文的資源文件
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
            }
        }

 8,最終運行登錄界面切換語音

 

 9,如果要在js文件中引用資源文件的內容,可以在前端頁面的js里設置全局變量

            //然后在其他的js文件中直接引用全局變量
            window.l_this_field_cannot_be_empty = '@Resource.prompt_statement';
            alert(window["l_this_field_cannot_be_empty"]);

            console.log('@Resource.prompt_statement');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM