views:

173

answers:

4

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?

A: 

Have you tryied :

public static Object GetLocalResourceObject (
    string virtualPath,
    string resourceKey,
    CultureInfo culture)

Try this link Click here

You can also try:

public static Object GetGlobalResourceObject (
    string classKey,
    string resourceKey,
    CultureInfo culture)

Try this link Click here

Simon
But then I still need to hand in a resourceKey like "MyStringID" and will learn only at runtime that the key does not exist right? Seems to be the same as GetString() to me.
newtogit
Oh well, I just understand what you want sorry. But well, I can't figure out how you can do this ... BTW, it's interesting to know if there's a way! I follow this thread!
Simon
A: 

ResourceSet has a method

public virtual IDictionaryEnumerator GetEnumerator()

that gives access to key-value pairs of the resource file.
E.g. (assuming we deal only with strings - N.B. the key-value pairs are of type object):

while (set.MoveNext())
     {
         string key = (string)set.Key;
         // string value = (string)set.Value; 
         string value = ResourceManager.GetString(key, neededCulturInfo); 
     }

This is not what you should do, because things become complicated - just to point it out.
You could create different resource files for different cultures and use a switch code block in a method that has a CultureInfo as parameter.

Gerard
A: 

You construct a class that looks inside the resource or use the Enumerator solution,look for the value and if it does not exist, make it use the value in the default language. But in compile time, it cannot be verified.

The easiest option is a try-catch and return the value in the general language in the catch.

Nevertheless, if we are using resources, all the keys must always be present in all the related files, even if you copy them with the general language values.

My solution is what it should be, all the resources should be consistent, if not we are using this great tool badly.

netadictos