我需要以编程方式列出resx文件组中的可用文化,但ResourceManager类似乎没有帮助。
我可能有:
Labels.resx
Labels.fr-FR.resx
Labels.ro-RO.resx
等等
但是,如何在运行时找到这三种(或者有多少种)文化?
答案 0 :(得分:7)
在应用程序的目录中查找附属程序集:对于每个子目录,检查其名称是否与文化名称相对应,以及它是否包含.resources.dll
文件:
public IEnumerable<CultureInfo> GetAvailableCultures()
{
var programLocation = Process.GetCurrentProcess().MainModule.FileName;
var resourceFileName = Path.GetFileNameWithoutExtension(programLocation) + ".resources.dll";
var rootDir = new DirectoryInfo(Path.GetDirectoryName(programLocation));
return from c in CultureInfo.GetCultures(CultureTypes.AllCultures)
join d in rootDir.EnumerateDirectories() on c.IetfLanguageTag equals d.Name
where d.EnumerateFiles(resourceFileName).Any()
select c;
}
答案 1 :(得分:2)
基于@ hans-holzbart在Programmatic way to get all the available languages (in satellite assemblies)的回答,但修复为不返回InvariantCulture并将其包装成可重复使用的方法:
public static ObservableCollection<string> GetAvailableLanguages()
{
var languages = new ObservableCollection<string>();
var cultures = GetAvailableCultures();
foreach (CultureInfo culture in cultures)
languages.Add(culture.NativeName + " (" + culture.EnglishName + " [" + culture.TwoLetterISOLanguageName + "])");
return languages;
}
使用该方法,您可以获得要添加到某个ComboBox的字符串列表,其中包含以下内容:
; This is a working hotkey example. Since the hotkey has the tilde (~)
; prefix, its own keystroke will pass through to the active window.
; Thus, if you type [btw (or one of the other match
; phrases) in any editor, the script will automatically perform an
; action of your choice (such as replacing the typed text):
~[::
Input, UserInput, V T5 L4 C, {enter}.{esc}{tab}, btw,otoh,fl,ahk,ca
if (ErrorLevel = "Max")
{
MsgBox, You entered "%UserInput%", which is the maximum length of text.
return
}
if (ErrorLevel = "Timeout")
{
MsgBox, You entered "%UserInput%" at which time the input timed out.
return
}
if (ErrorLevel = "NewInput")
return
If InStr(ErrorLevel, "EndKey:")
{
MsgBox, You entered "%UserInput%" and terminated the input with %ErrorLevel%.
return
}
; Otherwise, a match was found.
if (UserInput = "btw")
Send, {backspace 4}by the way
else if (UserInput = "otoh")
Send, {backspace 5}on the other hand
else if (UserInput = "fl")
Send, {backspace 3}Florida
else if (UserInput = "ca")
Send, {backspace 3}California
else if (UserInput = "ahk")
Run, http://ahkscript.org
return