1

I have the following setup in my app:

MyResources.resx // english strings
MyResources.zh-CN.resx // chinese strings

Due to lag in the translation process, some keys have english values but no chinese values. In other cases, the entire zh-CN resx file does not exist. By default, ResourceManager will fall back to the english value if the chinese value does not exist. This is acceptable for my use-case in most scenarios. However, I currently have a need to fetch a chinese resource string WITHOUT fallback to english.

My question is: what is the right way to do this?

I had initially thought that this could be done via the GetResourceSet method:

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 :-( */ }
4
  • 1
    Add values to to the zh-CN resource file
    – Fabio
    Commented Apr 26, 2016 at 13:10
  • @Fabio presumably you are suggesting some kind of stub values? I don't disagree that this is ideal; however the code in question is a library that is used by ~30 different codebases managed by different developers. I'd like to have a solution that doesn't rely on them all instantly updating their codebases with empty resx files whenever we start translating our resources into a new language. Commented Apr 26, 2016 at 13:17
  • How localizations strings are accessed in the code? By generated classes (Visual Studio will generate resources classes) ResourceNamespace.MyResourcesFileName.KeyofString or by using ResourceManager with hardcoded key: ResourceManager.GetString("KeyofString")?
    – Fabio
    Commented Apr 26, 2016 at 14:08
  • @downvoter why the downvote? Commented Apr 26, 2016 at 18:56

1 Answer 1

3

That's a one-off scenario since the intent is always to provide a fallback. But you could still work around it, something like this (slightly simplistic example). This only returns what the ResourceManager provides for its given culture if it's different from the default.

I just put some things in the constructor out of habit/convention. You could move the ResourceManager, desired CultureInfo, or both to method arguments.

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;
    }
}

It does not account for scenarios where there may be multiple levels of fallback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.