Search code examples
c#.netresourceslocalizationresx

Getting a string from a resource file in an untyped manner


I'm working on an ASP.Net project which has all of its translations in a Translations.resx file. Is there an easy way to get a translated string in an untyped manner?

I don't want to do

Translations.TranslateThisKey

but rather something like

Translations["TranslateThisKey"]

I need this because the key is a code coming from an external resource.


Solution

  • try this

    var Translations = new ResourceManager("MyResources", 
        Assembly.GetExecutingAssembly())
            .GetResourceSet(CultureInfo.CurrentCulture, false, true)
            .Cast<DictionaryEntry>()
            .Where(e => e.Value is string)
            .ToDictionary(e => e.Key, e => (string) e.Value);
    
    var result = Translations["TranslateThisKey"];