Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing resource strings with CultureInfo in .NET

Another simple questions. I have website with different languages. If I want to access a string from the resource file I would use it like this

Resources.MyResourceFile.MyStringIdentifier

Very easy. That way I know during compile time, that the resource string exists.

Now, this works only if I want to use the current Culture. Sometimes I need to specify a specific culture (let's say that the current user uses German as a language, but his action triggers messages to be sent to other users which will be in the recipient's language). Now, I see two options:

Resources.MyResourceFile.ResourceManager.GetString("MyStringIdentifier", neededCulturInfo)

The other would be to change the current thread's culture info which I would need to do several times.

Is there a third way? Something which tells me at compile time that the resources exist but without the need to change the thread's culture all the time?

like image 742
newtogit Avatar asked Oct 11 '10 13:10

newtogit


People also ask

What is the use of RESX file in C#?

Resources in . resx files. Unlike text files, which can only store string resources, XML resource (. resx) files can store strings, binary data such as images, icons, and audio clips, and programmatic objects.

What is. resx?

A . resx file contains a standard set of header information that describes the format of the resource entries, and specifies the versioning information for the XML code that parses the data. These files contain all the strings, labels, captions, and titles for all text in the three IBM Cognos Office components.


1 Answers

(For your scenario) the idea of the ResourceManager is to provide culture specific informations at runtime not at compile time (aka side-by-side with fallback).
So the answer is "NO", there isn't a buildin way to determinate the existance of those resource files at compile time - to do so you would require a kind of "hard coding" for all strings in every single langauge and also code to access to those. The side by side idea is exactly the opposite of hardcoding ;)

What you could do, is writng a unit test for the resources, that itterates your langauges and checks if the default or a localized value was used. Further if you are using a source control system that provides check-in policies (e.g. TFS) you could this unit test as part of the check-in policy.

like image 142
Jaster Avatar answered Sep 30 '22 04:09

Jaster