Change program locale at runtime in C#

[Change program locale at runtime][Change program locale at runtime]

This example lets you change program locale at runtime so you can test localized programs.

The example Test localized programs in C# explains how you can make a program select a specific locale when it starts so you can test different locales. This example shows how you can make that change at runtime.

The SetCulture method shown below selects a specific culture.

// Set the form's culture.
private void SetCulture(string culture)
    // Make the CultureInfo.
    CultureInfo culture_info = new CultureInfo(culture);

    // Make a ComponentResourceManager.
    ComponentResourceManager component_resource_manager
        = new ComponentResourceManager(this.GetType());

    // Apply resources to the form.
        this, "$this", culture_info);

    // Apply resources to the form's controls.
    foreach (Control ctl in this.Controls)
            ctl, ctl.Name, culture_info);

The code makes a new CultureInfo object to represent the desired culture. It then makes a ComponentResourceManager for the culture and uses its ApplyResources method to load the culture-specific resources for the form and its controls.

The following code shows how the example loads its resources for English or German when you select the corresponding radio button.

private void radEnglish_Click(object sender, EventArgs e)

private void radGerman_Click(object sender, EventArgs e)

This example actually isn’t localized for English but that’s what I used when making the default localization so that’s what you get if you select en-EN or any other culture for which it isn’t localized. For example, try fr-FR or es-MX.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in globalization, internationalization, localization and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Change program locale at runtime in C#

  1. Pingback: Make locale-aware methods use a new culture at runtime in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.