Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

c# - Using the correct ".resx" file according to the culture info

I want to use localization in my project so I'm using ".resx" files.

I have two files "StringRes.resx" and "StringRes.fr.resx". As you can guess, I want that all the messages of my app change according to the CultureInfo of the user.

But when I do this :

public MainWindow()
{
     Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
     InitializeComponent();
}

It doesn't change anything when I do :

Console.WriteLine(StringRes.FirstName);

Indeed, it's always the string in StringRes which is displayed and not the string from StringRes.fr

The both are in the same namespace.

Thank you for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have created my own test projected an I used "fr-FR" as a culture tag. The test project can be found on my skydrive.

Works perfect.

Culture test

In short:

I set the culture in the Properties of the project to be en-US as that is what I would want my Resource.resx to be.

Then I created Resource.resx, Resource.fr-FR.resx and Resource.nl-NL.resx.

I filled them with the TestData string. And the created a window with 3 buttons with events hooked to them. Next to the buttons I created a textblock, the textblock is binded to a Text string field.

In the button events I change the culture and the Text field. And you can then see the change happen.


You first need to create your Resource.resx then in the same folder you need to create Resource.fr-FR.resx. The Resource.resx will get a codebehind file, all the cultured resource files should not. If your Resource.fr-FR.resx does have a culture file you did something wrong and it is best to delete that resources file and recreate it so it loses it's codebehind.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...