Accessing resource strings with CultureInfo in .NET
Asked Answered
T

7

11

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?

Tirado answered 11/10, 2010 at 13:50 Comment(4)
You should use CurrentUICulture for accessing translatable strings, not CurrentCulture. CurrentCulture is responsible for formatting.Urita
Yes, thank you, naturally I am using CurrentUICultureTirado
If you don't know at compile time which language you want to show, how should the compiler be able to figure it out for you?Viyella
I'd be interested to know how this can be resolved elegantly because I have the same problem and don't particularly like the first or second way you described although they seem to be the only options available.Babs
D
1

(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.

Domash answered 15/11, 2010 at 16:36 Comment(0)
N
0

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

Necrolatry answered 15/10, 2010 at 19:47 Comment(2)
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.Tirado
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!Castorina
V
0

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.

Veliz answered 22/10, 2010 at 14:33 Comment(0)
U
0

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.

Unbent answered 30/10, 2010 at 21:35 Comment(0)
B
0

The generated Resources.MyResourceFile class has a static Culture property, which you can set to neededCultureInfo to override the current thread's CurrentUICulture.

Bolte answered 3/11, 2010 at 13:18 Comment(0)
I
0

1) At the start maybe could be useful to store the UICulture into a session, in order to change it when you want, at the begin you can change it from there.

2) You can override the UICulture in preRender and set it from there and than storing it into session.

You can store it in a cookie as well but is not the best solution for it.

Inscribe answered 10/11, 2010 at 11:53 Comment(0)
T
0

You can use WorkItems to send the messages asynchronously. Since you're now running on a different Thread, you should be able to modify the CurrentUICulture as needed.

P.S.: this is a good example why static dependencies are bad and everything should be interfaces & instances.

Threescore answered 12/11, 2010 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.