Asp.net Get all resources from .resx without specifying a culture (ResourceManager.GetResourceSet)

At first I came across a question similar to mine, here, when the stack overflowed: Scroll through all the resources in ResourceManager - C # . He only decided part of what I need to do. When you request an entry in the resource file for a specific culture, if there is no one present, it will by default be returned to the neutral culture resource file.

I need to skip every entry for a given resource file, and GetResourceSet requires culture. For example, I have a neutral resource file with 3 entries in it and a culture related resource file that accompanies a neutral file with 1 entry.

My neutral resource example file is MyResource.resx, and MyResource.en-gb.resx is an example for my specific resource file. The following code shows how I'm currently trying to loop through and access all resource records.

Dim cultInfo as New CultureInfo(culture) For Each entry As System.Collections.DictionaryEntry In myResourceManager.GetResourceSet(cultInfo, True, True) Next 

Neutral Resource File Entries

  • Full Name / Full Name
  • Phone Number / Phone Number
  • Condition / Condition

Entering a resource resource for a specific culture

  • State / County

When I call GetResourceSet for a specific culture, I return only 1 record. I expected (and want) to return all 3 records with a canceled record with one culture. Here is what I want to return:

  • Full Name / Full Name
  • Phone Number / Phone Number
  • State / County

Anyway, can I do this? Thank you

+6
source share
1 answer

The GetString method of the ResourceManager object correctly handles moving resource files to find the correct value for the given key based on the culture. The base / neutral / default resource file can be obtained using CultureInfo.InvariantCulture , which gives you all the possible keys for the resource file (if you configure the resource files this way).

Looping on DictionaryEntry objects found in the GetResourceSet ResourceManager method based on an invariant culture, and then calling GetString for each key using the specific culture that you passed, you will get the correct value for the given key based on the culture.

 For Each entry As DictionaryEntry In myResourceManager.GetResourceSet(CultureInfo.InvariantCulture, True, True) Dim strKey as String = entry.Key.ToString() Dim strValue as String = myResourceManager.GetString(entry.Key.ToString(), cultInfo) Next 

Hope this helps!

+9
source

Source: https://habr.com/ru/post/886653/


All Articles