Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetGlobalResourceObject or Resources.Resource - what's better?

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 http://img340.imageshack.us/img340/5562/langl.gif

like image 935
aron Avatar asked Dec 29 '22 08:12

aron


1 Answers

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.

like image 96
Eilon Avatar answered Jan 13 '23 14:01

Eilon