C# 如何在不返回默认区域性的情况下获取资源字符串

C# 如何在不返回默认区域性的情况下获取资源字符串,c#,.net,internationalization,resx,resourcemanager,C#,.net,Internationalization,Resx,Resourcemanager,我的应用程序中有以下设置: MyResources.resx // english strings MyResources.zh-CN.resx // chinese strings 由于翻译过程的滞后,一些键有英文值,但没有中文值。在其他情况下,整个zh CN resx文件不存在。默认情况下,如果中文值不存在,ResourceManager将返回到英文值。在大多数情况下,这对于我的用例是可以接受的。但是,我目前需要获取一个中文资源字符串,而不必返回到英文 我的问题是:正确的方法是什么 我最初

我的应用程序中有以下设置:

MyResources.resx // english strings
MyResources.zh-CN.resx // chinese strings
由于翻译过程的滞后,一些键有英文值,但没有中文值。在其他情况下,整个zh CN resx文件不存在。默认情况下,如果中文值不存在,
ResourceManager
将返回到英文值。在大多数情况下,这对于我的用例是可以接受的。但是,我目前需要获取一个中文资源字符串,而不必返回到英文

我的问题是:正确的方法是什么

我最初认为这可以通过GetResourceSet方法实现:

var manager = MyResources.ResourceManager;

var set = manager.GetResourceSet(CultureInfo.GetCultureInfo("zh-CN"), createIfNotExists: true, tryParents: false);
if (set == null || set.GetString("key") == null) { /* not translated! */ }

// however, this has issues because resource set lookup is cached:

// this will force the association of the zh-CN culture with the
// English resource set unde the hood
manager.GetString("key", CultureInfo.GetCultureInfo("zh-CN"));

// now this returns the English resource set, thus breaking my check
var set2 = manager.GetResourceSet(CultureInfo.GetCultureInfo("zh-CN"), createIfNotExists: true, tryParents: false);
if (set == null || set.GetString("key") == null) { /* checks whether key exists in english :-( */ }

这是一个一次性的场景,因为其目的始终是提供后备方案。但您仍然可以解决这个问题,类似这样的(稍微简单化的示例)。如果
ResourceManager
与默认区域性不同,则只返回它为给定区域性提供的内容

出于习惯/惯例,我只是在构造函数中放了一些东西。您可以将
ResourceManager
、所需的
CultureInfo
或两者都移动到方法参数

public class NonFallbackResourceManager
{
    private readonly CultureInfo _desiredCulture;
    private readonly ResourceManager _resourceManager;

    public NonFallbackResourceManager(CultureInfo desiredCulture, ResourceManager resourceManager)
    {
        _desiredCulture = desiredCulture;
        _resourceManager = resourceManager;
    }

    public string GetString(string key)
    {
        var desiredCultureString = _resourceManager.GetString(key, _desiredCulture);
        var defaultCultureString = _resourceManager.GetString(key, CultureInfo.InvariantCulture);
        return string.Equals(desiredCultureString, defaultCultureString) 
            ? String.Empty 
            : desiredCultureString;
    }
}

它不考虑可能存在多个回退级别的场景。

这是一个一次性场景,因为其目的始终是提供回退。但您仍然可以解决这个问题,类似这样的(稍微简单化的示例)。如果
ResourceManager
与默认区域性不同,则只返回它为给定区域性提供的内容

出于习惯/惯例,我只是在构造函数中放了一些东西。您可以将
ResourceManager
、所需的
CultureInfo
或两者都移动到方法参数

public class NonFallbackResourceManager
{
    private readonly CultureInfo _desiredCulture;
    private readonly ResourceManager _resourceManager;

    public NonFallbackResourceManager(CultureInfo desiredCulture, ResourceManager resourceManager)
    {
        _desiredCulture = desiredCulture;
        _resourceManager = resourceManager;
    }

    public string GetString(string key)
    {
        var desiredCultureString = _resourceManager.GetString(key, _desiredCulture);
        var defaultCultureString = _resourceManager.GetString(key, CultureInfo.InvariantCulture);
        return string.Equals(desiredCultureString, defaultCultureString) 
            ? String.Empty 
            : desiredCultureString;
    }
}

它不考虑可能存在多级回退的情况。

向zh CN资源添加值file@Fabio想必您是在建议某种存根值?我并不反对这是理想的;然而,所讨论的代码是一个库,由不同的开发人员管理大约30个不同的代码库。我希望有一个解决方案,它不依赖于当我们开始将资源翻译成新语言时,所有人都立即用空的resx文件更新他们的代码库。如何在代码中访问本地化字符串?通过生成的类(Visual Studio将生成资源类)
ResourceNamespace.MyResourcesFileName.KeyOfsString
或使用ResourceManager和硬编码键:
ResourceManager.GetString(“KeyOfsString”)
?@downvoter为什么选择downvote?向zh CN资源添加值file@Fabio想必您是在建议某种存根值?我并不反对这是理想的;然而,所讨论的代码是一个库,由不同的开发人员管理大约30个不同的代码库。我希望有一个解决方案,它不依赖于当我们开始将资源翻译成新语言时,所有人都立即用空的resx文件更新他们的代码库。如何在代码中访问本地化字符串?通过生成的类(Visual Studio将生成资源类)
ResourceNamespace.MyResourcesFileName.KeyOfsString
或使用带有硬编码键的ResourceManager:
ResourceManager.GetString(“KeyOfsString”)
?@downvoter为什么进行向下投票?