简体   繁体   中英

Mysterious Resources /CultureInfo behaviour in WPF App

i have two Resources files in the Properties folder of a WPF-project (VS 2008):

  • Resources.resx
  • Resources.de-DE.resx

Selecting the culture "de-DE" does not work (no error, but always the strings from "Resources.resx" are used):

public App()
    {
        UntitledProject2.Properties.Resources.Culture = new CultureInfo("de-DE");
    }

BUT: if I rename "Resources.de-DE.resx" to "Resources.fr-CA.resx" or "Resources.en-US.resx"

and then set it via

UntitledProject2.Properties.Resources.Culture = new CultureInfo("fr-CA");

it works!! But why!? Mysterious...

By default, WPF will always use "en-US"; at least it did the last time I checked (which was .net 3.5). If you want WPF to instead use the culture currently set by the system, you would execute this code block:

FrameworkElement.LanguageProperty.OverrideMetadata(
  typeof(FrameworkElement),
  new FrameworkPropertyMetadata(
      XmlLanguage.GetLanguage(
      CultureInfo.CurrentCulture.IetfLanguageTag)));

This will override the default value used for the FrameworkElement's Language dependency property, which, again, is "en-US" by default.

Execute this code once and early on in the lifetime of your application. AFter that, you shouldn't have to worry about it again, unless you expect your user to be switching the culture in the middle of program execution...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM