views:

166

answers:

0

So I have a localized Winforms application with a bunch languages already. Some of those we localized are neutral cultures we fallback too. Everything is setup and working correctly there.

In the field we download new resource satellite assemblies on demand (like a poor mans on demand differed download you get from ClickOnce).

For logging and diagnosis reasons, I'm trying figure out what specific ResourceSet (or really the CultureInfo of that ResourceSet) that the ResourceManager decides to fall back to when I do a call like:

CultureInfo resourceCulture = Thread.CurrentThread.CurrentCulture;
...
return resourceManager.GetString("FileNotFoundMessage", resourceCulture);

I tried something like this and by trying to replicate the logic of how the ResourceManager works falling back through the ResourceSets, but I'm not sure if this makes any sense.

    public CultureInfo FindCultureOfObject(ResourceManager resourceManager, CultureInfo topLevelCulture, string resourceKey)
    {
        //sanity check - this object doesn't exist anywhere in this fallback tree... go away.
        if (resourceManager.GetObject(resourceKey, topLevelCulture) == null) 
            throw new Exception(); 

        ResourceSet rs = resourceManager.GetResourceSet(topLevelCulture, true, false);

        //If we have a ResourceSet for this culture, check to see if what we are looking for is here. 
        if (rs != null && rs.GetObject(resourceKey) != null)
            return topLevelCulture; 

        //Can't find it with the current culture's resource set, let try the culture that is above the current. (Invariant cultures return themselves as their parent) 
        if (topLevelCulture != topLevelCulture.Parent) 
            return FindCultureOfObject(resourceManager, topLevelCulture, resourceKey);

        return null;
    } 

Is there anyway better way to do what I'm looking to do?