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

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;

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐