Why is it better to call the ResourceManager class as opposed to loading resources directly by name?
Asked Answered
S

2

7

I was working on localizing a large project, and I was doing that by creating a large resource file manually, and calling each string by name in the code. Instead of calling the ResourceManager and using GetString (for dialog boxes, etc), I was simply replacing each string by Resources.ClassName_MethodName_StringName.

I have a feeling I'm supposed to be using the ResourceManager, but I want to understand why it's better before I change all of my code to use it.

Speechmaking answered 24/1, 2013 at 13:51 Comment(3)
No, you are supposed to go via Properties.Resources.NameOfStringResource. You're doing it right. That's why the Resources.Designer.cs is created for you.Canna
for example, in Razor, ressources are "strongly typed". That is you can use RessourceName.StringName instead of RessourManager.GetString("StringName"). An intelisense helps you.Propagandist
Well it's good to know I don't have to go back and change anything. MSDN says to call ResourceManager for everything, so that's why I thought I was going about it incorrectly.Speechmaking
M
9

Well, there's no reason to use the ResourceManager directly (some exceptions to that will apply), because if you use generated code from the resx-Files all it does is the following:

public static string MyResourceName {
    get {
        return ResourceManager.GetString("MyResourceName", resourceCulture);
    }
}

This is great, since you get Compile-Time validation of your resource-names for free!

Mussorgsky answered 24/1, 2013 at 13:56 Comment(0)
O
0

http://www.c-sharpcorner.com/uploadfile/prvn_131971/chapter-i-resources-and-localization/

Internally, the generated class uses an instance of the ResourceManager class, which is defined in the System.Resources namespace. The instance of this class is accessible through the generated class's ResourceManager property. Internally, the property procedures for accessing the embedded resources themselves are just wrappers around calls of one of the GetXxx methods (that is, GetString or GetStream) of this ResourceManager instance. For example, the resource that is accessible through the generated property procedure Resources.MyResourceStrings.

So calling resources directly by name you're using YourResources.ResourceManager.GetString() method anyway.

Obsolescent answered 24/1, 2013 at 14:3 Comment(1)
Does this still work if you switch your resource provider to for example a DB ResourceProvider?Afflictive

© 2022 - 2024 — McMap. All rights reserved.