WPF Localization Extension: Translate Window at Runtime
I am working on translating a WPF window. I am using WPF Localize Extension . So far, I only have a Spanish translation for testing purposes in a file <Resource>.es.resx
. When designing time, translations work. So I think I am on the right track.
I have included menu items for dynamic translation of the GUI at runtime. I tried this (naive) command first ...
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 the menu items for each language are bound to it in XAML in this way.
<MenuItem Header="Español" CommandParameter="es-ES">
<MenuItem.Command>
<l:CmdTranslateUI />
</MenuItem.Command>
</MenuItem>
The point is, this approach doesn't work. In any case, the information about the culture remains equal "en-US"
. I read that installing it LocalizeDictionary.Instance.Culture
starts it DictionaryEvent
, so I thought this would automatically update the GUI. Obviously I am wrong.
On the other hand, it seems that the current threading culture does not affect the library's behavior either.
So, I ask ...
Q:
- What's the recommended approach for translating a window at runtime with WPF Localize Extension ?
- How can I list the available translations?
Thanks in advance.
source to share
It seems I entered a typo by accident the last time I compiled the library (or I had ghosts on my HDD / CPU). Language switching now works after installation LocalizeDictionary.Instance.SetCurrentThreadCulture
.
Just for the record, this is what the above command class 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 simple approach that will work as long as the l10n resource provider resource is active.
source to share
The property LocalizeExtension
Culture
is independent of the threading UI culture. This can sometimes be desirable because flow culture affects many things. We use the extension in our own project and manually adjust the thread culture to match the culture LocalizeDictionary
.
This should usually update the UI automatically. Make sure you are using a markup extension LocText
in your XAML, for example:
<TextBlock Text="{lex:LocText Bla:Bla:Bla}" />
Update: For a list of available translations, you can try the following:
Get all available cultures from a group of .resx files
However, I would recommend that you just provide a list of language fixes in your code, or if you are using an IoC container, register the available languages there.
source to share
This doesn't answer the OP's question, but I thought I would post it here anyway for those wandering around here from Google as the title of the question is relevant.
I am having an issue where my application will be partially translated when changing the language at runtime. At first I thought it was a convention issue since I had separate resources for each page, but the real problem was that the last control on the first page couldn't translate, so it broke the chain and as a result all subpages fail received in translation.
I was translating a FallbackValue
text block, I used this property to show the default text when the anchor was null.
Anyway, to catch errors like this, you can subscribe to the following event:
LocalizeDictionary.Instance.MissingKeyEvent += (sender, args) =>
{
//Do something
};
source to share