views:

267

answers:

2

Hello, I have an application that is multilingual. I'm using the out-of-the-box .Net features for this. Each language has its own file in the App_GlobalResources (see iamge below)

In the code behind what is better?

  1. GetGlobalResourceObject("LocalizedText", "ErrorOccured")
  2. Resources.LocalizedText.ErrorOccured

The 2nd one uses less code and it's type safe, it will return an error during compile time and not run time.

alt text

+1  A: 

So use the second one, if you know up-front what the resource file and key will be.

The GetGlobalResourceObject() method is useful if you don't know what the resource file or (more likely) the key will be at compile time.

Jeremy McGee
+2  A: 

These are the advantages of each approach:

Advantages of GetGlobalResourceObject (and GetLocalResourceObject):

  1. You can specify a particular culture instead of using the CurrentCulture.
  2. You can use a late-bound expression (i.e. a string) to decide which resource to load. This is useful if you can't know ahead of time which resource you will need to load.
  3. It works with any resource provider type. For example, it works not only with the built-in default RESX-based provider but it'll work the same against a database-based provider.

Advantages of strongly-typed RESX types:

  1. You get compile-time errors if you access a resource that doesn't exist.
  2. You get Intellisense while working on the project.

So, as with many "which is best" questions, the answer is: It depends! Choose the one that has advantages that will benefit your particular scenarios the most.

Eilon