C# 讀取Resource語言包


1,創建一個test.txt文件,里面是要翻譯的內容(key=value的形式,'Username’是key,'Email’是value),這里我是把登錄界面轉換成英文

  

 2,打開VS命令提示窗口,將txt文件中的資源轉換到resources文件

         a.進入到TXT文件目錄中(我的文件:D:\ResourceTest\test.txt)

         b.輸入 resgen test.txt test.resources 回車,就會發現資源文件test.resources已經生成

 

3,在controller中新建方法,解析資源文件

        [WebMethod]
        public string GetMulLanguage()
        {
            ResourceManager fileText=null;
            //這里把資源文件test.resources  放在“languageresources”文件夾中的
            if (System.IO.File.Exists(HttpRuntime.AppDomainAppPath + "\\languageresources\\test.resources"))
            {
                fileText = ResourceManager.CreateFileBasedResourceManager("test", HttpRuntime.AppDomainAppPath + "\\languageresources", null);
            }
           
            Hashtable ht = new Hashtable();
            ht.Add("lb_name", fileText.GetString("Username"));//調用3中的GetString方法,注意:GetString括號中的內容一定要和txt文件中的key一致(包括大小寫),
            ht.Add("btn_1", fileText.GetString("Login"));
            ht.Add("lb_password", fileText.GetString("Password"));
            return JsonConvert.SerializeObject(ht);
        }

 

4,通過Ajax在頁面上顯示翻譯內容

html代碼: 

<form id="form1">
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label" id="lb_name">郵件</label>
                <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" id="lb_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" id="btn_1">登錄</button>
                </div>
            </div>
        </form>
View Code

js代碼:

            $.ajax({
                type: "post",
                url: "GetMulLanguage",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {var mullanguage = data;
                    for (var key in mullanguage) {
                        var value = mullanguage[key];
                        
                            $("#" + key).html(value);
                        
                    }
                },
                error: function () {
                }
            })

 

 運行查看,登錄界面顯示的是英文

 

 

 

 

 

參考鏈接:https://blog.csdn.net/qq_40253245/article/details/86086974


免責聲明!

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



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