.NET中英文转换的实现

首先定义一个执行中英文转换的类

using System;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Diagnostics;
namespace CommonClass
{
    /// <summary>
    /// ResourcesCollection 的摘要说明。
    /// </summary>
    public class ResourcesCollection
    {
        private ResourceManager resourceManager;

        /// <summary>
        /// Constructs an instance using the specified Assembly. 
        /// </summary>
        /// <param name="assembly">The Assembly to be used.</param>
        public ResourcesCollection(string defaultLanguage,Assembly assembly)
        {
            resourceManager = new ResourceManager(
                assembly.GetName().Name + "." + defaultLanguage, assembly);
        }

        /// <summary>
        /// 根据此属性从资源文件中读取信息
        /// </summary>
        public string this[string name]
        {
            get 
            {
                return resourceManager.GetString(name);
            }
        }
    }
}


 

再定义一个Page的基类

using System;
using System.Text;
using System.Globalization;
using System.Reflection;
using System.Web;
using System.Web.UI;
using CommonClass;

public class PageBase : System.Web.UI.Page
{
    protected static string strDefaultLanguage;
    protected static ResourcesCollection resources;
    
    /// <summary>
    /// 重载 Oninit 方法
    /// </summary>
    /// <param name="e"></param>
    protected override void OnInit(EventArgs e)
    {
        base.OnInit (e);
                    
        // 根据不同的语言读取不同的资源文件。
        resources = new ResourcesCollection(
                GetDefaultLanguage(),Assembly.GetExecutingAssembly());

        // 设置当前请求的原始Url
        strRawUrl = GetRawUrl();

        //检查页面的权限
            //Common.CheckAuthority(this,Request);
        }
    /// <summary>
    /// 定义一个属性用于页面调用资源文件 该属性是静态属性 
    /// </summary>
    public static ResourcesCollection Resources
    {
        get
        {
            return resources;
        }
    }
    /// <summary>
    /// 判断默认语言。
    /// </summary>
    /// <returns></returns>
    protected string GetDefaultLanguage()
    {
        //判断Session 中默认语言的设置。
        if(Session["DEFAULTLANGUAGE"] != null)
        {
            strDefaultLanguage = Session["DEFAULTLANGUAGE"].ToString();
        }
        else
        {
            // 取得用户浏览器默认语言。
            string strLanguage = Request.UserLanguages[0];

            // 如果默认语言的前两个字符为“en”则为英语。
            if(strLanguage.Substring(0, 2).Equals("en"))
            {
                strDefaultLanguage = "en";
            }
            else
            {
                // 如果前两个字符不为“en”则设置默认语言为中文。
                strDefaultLanguage = "zh-cn";
            }

            // 将默认语言值写入Session 。
            Session["DEFAULTLANGUAGE"] = strDefaultLanguage;
        }

        return strDefaultLanguage;
        }
}


 

然后在页面中 页面继承PageBase 类 请注意引用资源文件的方式

  // 引用资源文件
  lblTitle.Text = Resources["ModuleName27"];
  lblRoleNume.Text = Resources["RoleName"];
  lblRemark.Text = Resources["Remark"];
  btnAdd.Text = Resources["Add"];

注:省略了刚进系统时的用户自定义的资源文件引用的判断Session["DEFAULTLANGUAGE"],读者可自行加上

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值