Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the correct ".resx" file according to the culture info

Tags:

c#

resx

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.

like image 359
Kyoros Avatar asked Aug 21 '13 07:08

Kyoros


People also ask

What is a .resx file and how do you use it?

The . resx resource file format consists of XML entries that specify objects and strings inside XML tags. One advantage of a . resx file is that when opened with a text editor (such as Notepad) it can be written to, parsed, and manipulated.

What type of information is available in .resx file in asp net?

You can use application resource files (. resx) to efficiently create a localizable Microsoft ASP.NET Web application. By using resource files, you can store localized values for controls. The localized values are based on a user's language and culture.

What is .resx file in C#?

Net Resource (. resx) files are a monolingual file format used in Microsoft . Net Applications. The . resx resource file format consists of XML entries, which specify objects and strings inside XML tags.


1 Answers

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.

like image 139
SynerCoder Avatar answered Nov 14 '22 22:11

SynerCoder