问题描述
我正在使用 .resx 文件设计一个多语言应用程序.
I'm designing a multilingual application using .resx files.
我有一些文件,例如 GlobalStrings.resx、GlobalStrings.es.resx、GlobalStrings.en.resx 等.当我想使用它时,我只需要设置 Thread.CurrentThread.CurrentCulture.
I have a few files like GlobalStrings.resx, GlobalStrings.es.resx, GlobalStrings.en.resx, etc. When I want to use this, I just need to set Thread.CurrentThread.CurrentCulture.
问题:我有一个包含所有可用语言的组合框,但我是手动加载的:
The problem: I have a combobox with all the available languages, but I'm loading this manually:
comboLanguage.Items.Add(CultureInfo.GetCultureInfo("en"));
comboLanguage.Items.Add(CultureInfo.GetCultureInfo("es"));
我试过了
cmbLanguage.Items.AddRange(CultureInfo.GetCultures(CultureTypes.UserCustomCulture));
没有任何成功.还尝试了 CultureTypes 中的所有元素,但我只得到一个包含更多我没有使用的语言的大列表,或者一个空列表.
without any success. Also tried with all the elements in CultureTypes, but I'm only getting a big list with a lot more languages that I'm not using, or an empty list.
有没有办法只获得支持的语言?
Is there any way to get only the supported languages?
推荐答案
使用 Rune Grimstad 所说的,我最终得到了这个:
Using what Rune Grimstad said I end up with this:
string executablePath = Path.GetDirectoryName(Application.ExecutablePath);
string[] directories = Directory.GetDirectories(executablePath);
foreach (string s in directories)
{
try
{
DirectoryInfo langDirectory = new DirectoryInfo(s);
cmbLanguage.Items.Add(CultureInfo.GetCultureInfo(langDirectory.Name));
}
catch (Exception)
{
}
}
或者其他方式
int pathLenght = executablePath.Length + 1;
foreach (string s in directories)
{
try
{
cmbLanguage.Items.Add(CultureInfo.GetCultureInfo(s.Remove(0, pathLenght)));
}
catch (Exception)
{
}
}
我还是觉得这不是个好主意……
I still don't think that this is a good idea ...
这篇关于获取所有可用语言的编程方式(在附属程序集中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!