可用文化列表 [英] List of available cultures
本文介绍了可用文化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个WinForms应用程序,可以通过卫星程序集将其本地化为resx文件.用户可以在运行时切换应用程序语言.
I have a WinForms application which can be localized through satellite assemblies for the resx-files. The user can switch the application language at runtime.
所以我的问题是:有什么方法可以动态地找出哪些文化随客户一起作为本地化资源提供?
So my question is: Is there any way to dynamically find out which cultures are shipped as localized resources with my client?
推荐答案
这是我使用的代码.它会在程序的子目录中搜索附属程序集
Here's the code that I use. It searches satellite assemblies in the program's subdirectories
private static ReadOnlyCollection<CultureInfo> GetAvailableCultures()
{
List<CultureInfo> list = new List<CultureInfo>();
string startupDir = Application.StartupPath;
Assembly asm = Assembly.GetEntryAssembly();
CultureInfo neutralCulture = CultureInfo.InvariantCulture;
if (asm != null)
{
NeutralResourcesLanguageAttribute attr = Attribute.GetCustomAttribute(asm, typeof(NeutralResourcesLanguageAttribute)) as NeutralResourcesLanguageAttribute;
if (attr != null)
neutralCulture = CultureInfo.GetCultureInfo(attr.CultureName);
}
list.Add(neutralCulture);
if (asm != null)
{
string baseName = asm.GetName().Name;
foreach (string dir in Directory.GetDirectories(startupDir))
{
// Check that the directory name is a valid culture
DirectoryInfo dirinfo = new DirectoryInfo(dir);
CultureInfo tCulture = null;
try
{
tCulture = CultureInfo.GetCultureInfo(dirinfo.Name);
}
// Not a valid culture : skip that directory
catch (ArgumentException)
{
continue;
}
// Check that the directory contains satellite assemblies
if (dirinfo.GetFiles(baseName + ".resources.dll").Length > 0)
{
list.Add(tCulture);
}
}
}
return list.AsReadOnly();
}
这篇关于可用文化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文