C# 讀取Resource語言包

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

  

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

         a.進入到TXT文件目錄中(個人文件:D:\ResourceTest\test.txt)json

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

 

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

        [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在頁面上顯示翻譯內容post

html代碼: url

<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代碼:spa

            $.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 () {
                }
            })

 

 運行查看,登陸界面顯示的是英文.net

 

 

 

 

 

參考連接:https://blog.csdn.net/qq_40253245/article/details/86086974翻譯

相關文章
相關標籤/搜索