WEB页面多语言功能

本文介绍了一种使用JSON文件实现网站多语言支持的方法,详细展示了如何在C#中读取和解析JSON文件来获取不同语言的文本内容。通过具体代码示例,读者可以了解从JSON文件中提取翻译字符串的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前做的多语言功能是在数据库中,目前想做个Json文件格式的多语言功能

1.编辑en-US.json文件

页面中调用的方法
                        <div class="field">
                            <div class="form_logo"></div>
                        </div>
                        <div id="info">
                        <span class="titleesd">ESD</span><span class="titledesc">
                            @com.aaa.Translation.Language.GetText("Page.Title.Application")
                             </span>
                        </div>


                        <div class="field" style="margin-top: 30px">
                            <div class="field-left">
                               @com.aaa.Translation.Language.GetText("Page.Label.UserName")

                            </div>
                            <div class="field-right">
                                <input id="UserCode" type="text" class="text" name="user.logonname" value="admin" />
                                <span id="namemsg" class="red"></span>
                            </div>
                        </div>
下面是C#读取Json文件的方法,但是Jobject是否有更简洁的用法,目前我还没有找到

using com.aaa.DBUtility;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Globalization;
using System.IO;

namespace com.aaa.Translation
{
    public static class Language
    {
        private static int LanguageID = 2052;
        public static void SetLanguage(int LangID)
        {
           LanguageID = LangID;
        }     

        public static string GetJsonFile()
        {
            string currDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Content\Resources\Language\"; 
            string JsonFile = currDir + @"zh-CN.json"; 
            switch (LanguageID)
            {
                case 1033:
                    JsonFile = currDir+ @"en-US.json";
                    break;
                case 2052:
                    JsonFile = currDir + @"zh-CN.json";
                    break;
            }

            string result = "";
            using (StreamReader r = new StreamReader(JsonFile))
            {
                  result = r.ReadToEnd();
            }
            return result;
        }
        public static string GetText(string Node)
        {
            string result = "Unknow";
            if (Node != null)
            {
                string[] arr = Node.Split('.');
                JObject jobj = JObject.Parse(GetJsonFile()); 

                for (int x = 0; x < arr.Length; x++)
                {
                    if (x == arr.Length - 1)
                    {
                        result = jobj[arr[x]].ToString();
                        break;
                    }
                    jobj = (JObject)jobj[arr[x]]; 
                }
            } 

            return result;
        } 
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值