4

I'm in the process of translating a WPF window. I'm using WPF Localize Extension . So far I only have a Spanish translation for testing purposes in <Resource>.es.resx file . At design time translations work . So I guess I'm on the right track .

I have included menu items to translate the GUI dynamically at run time . Initially I tried this (naïve) command ...

public class CmdTranslateUI : ICommand
{
    bool ICommand.CanExecute(object parameter)
    {
        // TODO: Query available translations
        return true;
    }

    public event EventHandler CanExecuteChanged;

    void ICommand.Execute(object parameter)
    {
        LocalizeDictionary.Instance.Culture = new CultureInfo(
            (string) parameter);
    }
}

... and menu items for each language are bound to it in XAML this way .

<MenuItem Header="Español" CommandParameter="es-ES">
    <MenuItem.Command>
        <l:CmdTranslateUI />
    </MenuItem.Command>
</MenuItem>

The fact is that such approach is not working . Culture info remains set to "en-US" anyway . I read that on setting LocalizeDictionary.Instance.Culture its DictionaryEvent is triggered, so I thought this would update the GUI automatically . Obviously I'm wrong .

On the other hand , it seems current thread's culture won't influence library behavior either.

So I ask ...

Q:

  • What's the recommended approach to translate a window at run time with WPF Localize Extension ?
  • How could I list available translations ?

Thanks in advance .

Olemis Lang
  • 738
  • 7
  • 17

3 Answers3

5

It seems I introduced a typo by accident last time I compiled the library (or I had ghosts in my HDD / CPU) . Language switching is working now after setting LocalizeDictionary.Instance.SetCurrentThreadCulture .

Just for the record , this is what command class mentioned above should look like

public class CmdTranslateUI : ICommand
{
    bool ICommand.CanExecute(object parameter)
    {
        CultureInfo ci = new CultureInfo((string)parameter);

        foreach (CultureInfo c in ResxLocalizationProvider.Instance.AvailableCultures)
        {
            if (ci.Name == c.Name)
                return true;
            else if (ci.Parent.Name == c.Name)
                return true;
        }
        return false;
    }

    public event EventHandler CanExecuteChanged;

    void ICommand.Execute(object parameter)
    {
        LocalizeDictionary.Instance.SetCurrentThreadCulture = true;
        LocalizeDictionary.Instance.Culture = new CultureInfo(
            (string) parameter);
    }
}

... at least that's a simple approach that will work as long as resource files l10n provider is active.

Olemis Lang
  • 738
  • 7
  • 17
  • have you seen the LocalizeDictionary.Instance.MergedAvailableCultures property btw? – George Birbilis Aug 18 '15 at 11:20
  • Not really , but if you have an alternative solution I'd encourage you to share it . -- Regards – Olemis Lang Aug 19 '15 at 15:57
  • from that method's name, sounds like it gives you the available cultures (of the current provider I guess) ready to add them to a listbox – George Birbilis Aug 21 '15 at 15:09
  • just tried the MergedAvailableCultures, but seems it does something different. Before calling LocalizeDictionary.Instance.Culture = Thread.CurrentThread.CurrentCulture it just returns 1 culture, the invariant one, but after calling it returns 2 cultures, the invariant one and the current culture of the thread (the one the app was launched with from the OS). Not sure where that method is useful – George Birbilis Aug 21 '15 at 17:34
  • I ended up using the solution I posted at http://stackoverflow.com/questions/3226974/get-all-available-cultures-from-a-resx-file-group to get the available cultures – George Birbilis Aug 22 '15 at 23:02
1

The LocalizeExtension's Culture property is independent of the threads UI culture. Sometimes this may be desired, because the threads culture does affect a lot of things. We are using the extension in our own project and settings the threads culture manually to match the LocalizeDictionary's culture.

Normally this should update the UI automatically. Make sure you are using the LocText markup extension in your XAML, e.g:

<TextBlock Text="{lex:LocText Bla:Bla:Bla}" />

Update: To get the list of available translations you might try this:

Get all available cultures from a .resx file group

However I would recommend that you just provide a fix list of languages in code or if you are using an IoC container, register the available languages there.

Community
  • 1
  • 1
aKzenT
  • 7,775
  • 2
  • 36
  • 65
  • Yes I'm using [the add-in](http://wpflocalizeaddin.codeplex.com/wikipage?title=Screenshots%20Demo&referringTitle=Home) so `lex:LocText` is spread all over XAML document . This happens out-of-the-box – Olemis Lang Feb 04 '13 at 07:41
  • see my reply on how to get available cultures at the link http://stackoverflow.com/questions/3226974/get-all-available-cultures-from-a-resx-file-group that you posted. Couldn't make the answer that was there to work with WPF – George Birbilis Aug 22 '15 at 23:03
0

This does not answer OP's question, but thought I'd post it here anyway for those wandering here from Google since the title of the question is relevant.

I was having a problem where my application would be partially translated when changing the language in runtime. At first thought it was a convention problem since I had separate resources for each page, but the real problem was that the last control in the first page failed to translate, so it broke the chain and as a result all the subpages didn't get translated.

I was translating the FallbackValue of a textblock, I was using this property to show a default text when the binding was null.

Anyway, to catch errors like this, you can subscribe to the following event:

LocalizeDictionary.Instance.MissingKeyEvent += (sender, args) =>
{
    //Do something
};
Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58