通过在C#中不同语言/文化嵌入的资源循环 [英] Loop through embedded resources of different languages/cultures in C#

查看:180
本文介绍了通过在C#中不同语言/文化嵌入的资源循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题一个层次了,会是什么?的方式(通过和环路)存储所有可用的资源和相关的文化,让特定文化的用户选择

One level up from this question, what would be the way to store all (and loop through) available resources and associated cultures, to allow user selection of a specific culture?

进一步的解释:

假设三种资源文件:


  • GUILanguage.resx

  • GUILanguage。 fr.resx

  • GUILanguage.it.resx

我可以在每个称为一个字符串 LanguageName 。我将如何能够以编程循环通过不同的 LanguageName 值列出他们(在说一个列表框)?

I could have a string in each called LanguageName. How would I be able to programmatically loop through the different LanguageName values to list them (in say a list box)?

编辑:WinForms项目,嵌入的资源

WinForms project, embedded resources.

推荐答案

下面是一个解决方案,我认为对的WinForms作品:

Here is a solution I think works for Winforms:

// get cultures for a specific resource info
public static IEnumerable<CultureInfo> EnumSatelliteLanguages(string baseName)
{
    if (baseName == null)
        throw new ArgumentNullException("baseName");

    ResourceManager manager = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
    ResourceSet set = manager.GetResourceSet(CultureInfo.InvariantCulture, true, false);
    if (set != null)
        yield return CultureInfo.InvariantCulture;

    foreach (CultureInfo culture in EnumSatelliteLanguages())
    {
        set = manager.GetResourceSet(culture, true, false);
        if (set != null)
            yield return culture;
    }
}

// determine what assemblies are available
public static IEnumerable<CultureInfo> EnumSatelliteLanguages()
{
    foreach (string directory in Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory))
    {
        string name = Path.GetFileNameWithoutExtension(directory); // resource dir don't have an extension...

        // format is XX or XX-something, we discard directories that can't match.
        // could/should be replaced by a regex but we still need to catch cultures errors...
        if (name.Length < 2)
            continue;

        if (name.Length > 2 && name[2] != '-')
            continue;

        CultureInfo culture = null;
        try
        {
            culture = CultureInfo.GetCultureInfo(name);
        }
        catch
        {
            // not a good directory...
            continue;
        }

        string resName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location) + ".resources.dll";
        if (File.Exists(Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name), resName)))
            yield return culture;
    }
}



这里是你将如何使用它的WindowsFormsApplication1

And here is how you would use it for a WindowsFormsApplication1:

    List<CultureInfo> cultures = new List<CultureInfo>(EnumSatelliteLanguages("WindowsFormsApplication1.GUILanguage"));

这篇关于通过在C#中不同语言/文化嵌入的资源循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆