Resources: How to retrieve localized resource?

A

Axel Dahmen

Hi,

I've created a class library assembly containing several string resource
files, like:

- TableColumns.resx
- TableColumns.de.resx
- General.resx
- General.de.resx

I don't know how to address each of these having automatic language
fall-back enabled.

If I write...

new
ResourceManager("TableColumns",...GetExecutingAssembly()).GetString("someID");

....I always only get strings from TableColumns.resx, but not from
TableColumns.de.resx, although CurrentCulture.Name yields "Germany".

What am I doing wrong?

TIA,
Axel Dahmen
 
S

Steven Cheng[MSFT]

Hi Axel,

For the resource retrieving question you mentioned, it is due to the
"ResourceManager.GetString" has two versions, one take a single string
input parameter which will retrieve the resource string depend on your
application's current Culture setting. This can be configured by setting
currrent Thread's UICulture. e.g.

=========
//set the UICulture first
CultureInfo culture = new CultureInfo("de-De");
System.Threading.CurrentThread.CurrentUICulture = culture;

//use ResourceManager to retrieve string
========

the following article also mention this:

#Internationalize Your ASP.NET Applications (Part 2 of 3)
http://www.devx.com/dotnet/Article/6999

Or if you want to supply the CultureInfo(determine which culture specific)
when loading the resource item, you can use another signature of the
"GetString" method:

#ResourceManager.GetString Method (String, CultureInfo)
http://msdn2.microsoft.com/en-us/library/aa330255(VS.71).aspx

therefore, the resource loading is depend on the second "CultureInfo"
parameter instance.

Here are some other reference on .NET localization:

#Resources in Applications
http://msdn2.microsoft.com/en-us/library/f45fce5x(VS.71).aspx

#Resources and Localization Using the .NET Framework SDK
http://msdn2.microsoft.com/en-us/library/aa309421(VS.71).aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
A

Axel Dahmen

Hi Steven,

thanks for your reply.

As you can see from my original posting I've been using the one-parameter version of GetString(). Still it's just getting the neutral version. Is there anything wrong with my code below?

AFAIK, .NET 2.0 (in contrast to .NET 1.0) automatically sets the current thread's CurrentCulture property. Am I wrong with this assumption?

TIA,
Axel Dahmen
 
A

Axel Dahmen

Perhaps the source code might help finding the problem. Here's the (test) code:

---------
string str;

str = System.Threading.Thread.CurrentThread.CurrentCulture.ToString(); //
yields "de-DE"

str = new
ResourceManager("MyProject.Classes.Ressources.TableColumns",System.Reflection.Assembly.GetExecutingAssembly()).GetString(tableName
+ "." + columnName); // erroneously yields an English expression

System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo("de-DE");

str = new
ResourceManager("MyProject.Classes.Ressources.TableColumns",System.Reflection.Assembly.GetExecutingAssembly()).GetString(tableName
+ "." + columnName); // still yields an English expression
---------

Both, class and resource files reside in a class library project
("MyProject.Classes") in a folder called "Ressources":

MyProject.Classes
|-- Ressources
|-- GetMultiLanguageTexts.cs
|-- TableColumns.de.resx
|-- TableColumns.resx
|-- ...
 
A

Axel Dahmen

Hi Steven,

I've found out what the problem was (or is):

ASP.NET doesn't automatically add the UICulture="auto" attribute to the
@Page directive. I needed to add this attribute manually.

Strange, but here we go... Now everything works as expected.

Thanks again for trying to help.

Best regards,
www.axeldahmen.de
Axel Dahmen
 
S

Steven Cheng[MSFT]

Thanks for your reply Axel,

Glad that you've figured it out. BTW, I haven't expected that your code is
used in ASP.NET web app context and assumed that you used it in winform
app. That's why I just ask you check the Thread's Current Culture settings.

Thanks again for posting!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: =?Utf-8?B?QXhlbCBEYWhtZW4=?= <[email protected]>
References: <[email protected]>
Subject: RE: Resources: How to retrieve localized resource?
Date: Tue, 4 Dec 2007 11:05:00 -0800
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top