How to get string resources from specific resource file in C#
15,829
Solution 1
I found the working solution.
First of all our resource files are ending like
Resource.resx
and not like Resources.resx
And in second we have resources in a separate assembly: ModelEntities
So the working code is following
var resourceManager = new ResourceManager(typeof(ModelEntities.Properties.Resource));
var genderString = resourceManager.GetString("Gender", ci);
Solution 2
You can use the resource manager class click here for more info about the class
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-GB");
ResourceManager resourceManager = new ResourceManager("YourResource", Assembly.GetExecutingAssembly());
string bodyResource = resourceManager.GetString("yourText", ci);
Solution 3
A simple way:
YorResourceName.ResourceManager.GetString("ResourceKeyName", System.Globalization.CultureInfo.GetCultureInfo("en-US"))
Solution 4
Folder structure:
MyApplicationSolution
/Resources
/Resource.resx
public static string GetLocalisedRes(string resourceName = "", string resourceNameKey = "")
{
string translate = string.Empty;
string baseName = "YourNameSpace.Resources." + resourceName + "";
try
{
Type resType = Type.GetType(baseName);
ResourceManager rm = new ResourceManager(baseName, resType.Assembly);
translate = rm.GetString(resourceNameKey);
}
catch {
translate = string.Empty;
}
return translate;
}
Usage
var age = GetLocalisedRes("Resource", "Age");
This working for me
Related videos on Youtube
Comments
-
NoWar almost 2 years
In some cases in our project we have to ignore current culture of application and get resources in English.
Basically, we do it like for all languages
<label>@ModelEntities.Properties.Resource.Gender</label>
How is it possible to get in English only?
I assume we have somehow created some reference to
Resource.resx
?I have used
ResourceManager rm = new ResourceManager("Resource.Strings", Assembly.GetAssembly(typeof(ModelEntities.Properties.Resource))); var s = rm.GetString("Age");
But it seems like not working.
Thank you!
-
Adel Mourad over 4 yearsthanks is works,, for the "ci" definition got it from the previous answer