c# 语言资源文件,C# 多语言资源文件的访问

这篇博客探讨了两种处理和加载多语言资源的方法。首先,通过遍历程序集的嵌入资源,读取并存储资源信息。其次,利用ResourceManager根据当前线程语言加载资源,演示了如何设置线程文化信息并获取资源字符串。代码示例详细展示了实现过程。
摘要由CSDN通过智能技术生成

这两天一直研究关于多语言资源的处理方式,先上代码吧,

1、这是以传入DLL 文件的目录和名称的方式实现的

public IEnumerable DefaultResource(Assembly assembly)

{

var names = assembly.GetManifestResourceNames();

List resources = new List();

foreach (var name in names)

{

using (Stream stream = assembly.GetManifestResourceStream(name))

{

ResourceReader reader = new ResourceReader(stream);

IDictionaryEnumerator enumerator = reader.GetEnumerator();

var resource = new EmbededResource(enumerator) { Name = name };

resources.Add(resource);

}

}

return resources;

}

///

/// 内嵌资源类

///

public class EmbededResource

{

public IDictionary Values { get; set; }

public string Name { get; set; }

public EmbededResource ( )

{

Values = new Dictionary ( );

}

///

/// Constructor of EmbadedResource

///

public EmbededResource(IDictionaryEnumerator enumerator)

: this()

{

while (enumerator.MoveNext())

{

try

{

if (enumerator.Value == null || enumerator.Key == null)

continue;

Values[enumerator.Key] = enumerator.Value;

}

catch (System.Reflection.TargetInvocationException ex)

{

throw ex;

}

}

}

}

2、以加载程序集利用ResourceManager方式实现的,ResourceManager会根据当前的线程语言,到区域语言目录找到资源,如果没有找到,会以默认的方式返回

Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CN");

//System.Resources.ResourceManager RM = new System.Resources.ResourceManager("Layer1Multilangu.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());//这种方式的BaseName一直不好使,试了好多次,总是不得要领。

string  mname = Layer1Multilangu.Properties.Resources.ResourceManager.GetString("name");//

string rsfile = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + @"\test\zh-CN\Layer1Multilangu.resources.dll";             string mname = string.Empty;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值