How to use texts from different language resx files in xaml windows of a wpf application?

17 Views Asked by At

An existing application (WPF) is to be extended for several languages. There are separate RESX language files for each window (e.g. MainWndResource_en-US.resx) in the namespace of the corresponding window. At the start of the program, the OnStartup() displays the last language (e.g. "en-US") taken from the registry and stored in the thread sedate. During the runtime, it should be possible to change the language via menu.

In the code behind this all works fine using resMgr.GetString("key"), but how can I extract the texts from the resx_Dateien, using their keys in XAML code of the Windows?

namespace RPMgr { public partial class App : Application { public static CultureInfo currCultureInfo;

public static string lastCultureName;
public static string currResourceFile;
public static string currResourceFileSpec;

public static ResourceManager resMgr;

// Prevention of multiple starts
private static Mutex mutex1;

protected override void OnStartup (StartupEventArgs e)
{
  bool createdNew;

  base.OnStartup(e);

  SetCulture(GetLastCultureInfo()); // sets the desired culture in the thread

  currResourceFile = "MainWndResource_" + lastCultureName;
  currResourceFileSpec = "RPMgr." + currResourceFile;

  resMgr = new ResourceManager(currResourceFileSpec, Assembly.GetExecutingAssembly());

  mutex1 = new Mutex(true, constAppName, out createdNew);
  if (!createdNew)
  { 
    string message = resMgr.GetString("doubleStart");
    string caption = resMgr.GetString("MsgCapt-5");
    MessageBox.Show(message, caption,
                    MessageBoxButton.OK,
                    MessageBoxImage.Information);
    Application.Current.Shutdown();
  }
  base.OnStartup(e);
}

...
...

public void SetCulture (CultureInfo currCultureInfo)
{
  if (currCultureInfo != null)
  {
    Thread.CurrentThread.CurrentCulture = currCultureInfo;
    Thread.CurrentThread.CurrentUICulture = currCultureInfo;
    lastCultureName = currCultureInfo.Name;
  }
}
...
...

} ... ... }

I would be very thankful for help.

0

There are 0 best solutions below