2
votes

I've got a WPF application that I'm modifying to support localization. My program is part of a suite of products that my company offers. We have created a resource DLL that contains all of the strings that need to be translated for all of our products. I've added a reference to this DLL to my program, added string resources to it, and modified my code to use the string resources instead of hard-coded strings. Everything works fine when I run my program in English.

We have had the string resources translated into Spanish. I've created a resx file with the Spanish translations in it. I've rebuilt my application. Now I want to see the Spanish text appear in my program without having to change my computer's culture settings.

It's not necessary for the program to change culture settings on the fly. The program will run in one language only for a particular installation.

The program has an App.config file. Short of adding a custom setting with the culture information in it, how do I tell my program to run in Spanish?

2

2 Answers

8
votes

You could just define a key in your App.config like this

<configuration>
    <appSettings>
        <add key="DefaultCulture" value="es-CO" />
    </appSettings>
</configuration>

and in your application read that value and set the culture

CultureInfo culture = new CultureInfo(ConfigurationManager.AppSettings["DefaultCulture"]);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

In the example of the config I set it to Spanish-Colombia

This is a list of culture codes

-1
votes

I decided to add this answer because while @MauricioGarcia's answer works if you want to always display a particular language on a machine with multiple language packs installed on it, no matter what the current region settings on the machine are, we didn't implement it that way.

Instead, we just use whatever CultureInfo objects are set in the current Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture properties. When you set the region settings on your computer to a particular location and language, these properties are automatically changed. So they're always right & we don't have to add any code to change anything.

Interestingly, the object in the CurrentUICulture property is used to determine which language strings to display and the object in the CurrentCulture property is used to format numbers & DateTime.