Olemis Lang
Olemis Lang

Reputation: 768

WPF Localize Extension : Translate window at run-time

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:

Thanks in advance .

Upvotes: 4

Views: 7344

Answers (3)

Shahin Dohan
Shahin Dohan

Reputation: 6922

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
};

Upvotes: 0

Olemis Lang
Olemis Lang

Reputation: 768

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.

Upvotes: 5

aKzenT
aKzenT

Reputation: 7895

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.

Upvotes: 1

Related Questions