【问题标题】:How to read data from multiple (multi language) resource files?如何从多个(多语言)资源文件中读取数据?
【发布时间】:2014-02-24 13:56:56
【问题描述】:

我正在尝试应用程序中的多语言功能。我已经创建了资源文件 GlobalTexts.en-EN.resx GlobalTexts.fr-FR.resx 和一个设置文化并返回文本的类(我不会详细介绍所有细节,只显示结构):

public class Multilanguage
{
    ...
    _res_man_global = new ResourceManager("GlobalResources.Resources.GlobalTexts", System.Reflection.Assembly.GetExecutingAssembly());
    ...
    public virtual string GetText(string _key)
    {
        return = _res_man_global.GetString(_key, _culture);
    }
}
...
Multilanguage _translations = new Multilanguage();
...
someText = _translations.GetText(_someKey);
...

这很好用。 现在,我想在另一个基本上扩展它(更多窗口等)的解决方案中使用这个应用程序,它也有资源文件 ExtendedTexts.en-En.resx ExtendedTexts.fr-FR.resx 和一个新类,如:

public class ExtendedMultilanguage : Multilanguage
{
    ...
    _res_man_local = new ResourceManager("ExtendedResources.Resources.ExtendedTexts", System.Reflection.Assembly.GetExecutingAssembly());
    ...
    public override string GetText(string _key)
    {
        string _result;
        try 
        {
            _result = _res_man_local.GetString(_key, _culture);
        }
        catch (Exception ex)
        {
            _result = base.GetText(_key);
        }
}
...
ExtendedMultilanguage _translations = new Multilanguage();
...
someText = _translations.GetText(_someKey);
...

这个想法是,如果在 ExtendedTexts 中找不到密钥,该方法将调用正在查看 GlobalTexts 的基类。我这样做是为了在代码中的任何地方都使用调用 GetText(wantedKey) 而不必关心资源的位置(我不想在 GlobalTexts 文件中包含扩展名的翻译);只是使用的类因项目而异。

我面临的问题是当异常引发时 try/catch 非常慢 - 我等待几秒钟以填充一个窗口。我通过直接调用进行了测试并且工作速度更快,但是我需要一直关心资源所在的位置......

问题是:是否有另一种方法可以做到这一点(将资源分散在各种文件中,并且只有一种方法可以提供所需资源而不会引发错误)?

【问题讨论】:

    标签: c# resources multilingual resx


    【解决方案1】:

    最后,我采取了一种解决方案,并将资源文件的所有内容加载到字典中。这样我就可以使用 ContainsKey 并查看密钥是否存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2016-12-04
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      相关资源
      最近更新 更多