WPF 本地化扩展:在运行时翻译窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14668640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Localize Extension : Translate window at run-time
提问by Olemis Lang
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.resxfile . At design time translations work . So I guess I'm on the right track .
我正在翻译 WPF 窗口。我正在使用WPF 本地化扩展。到目前为止,我在<Resource>.es.resx文件中只有一个用于测试目的的西班牙语翻译。在设计时翻译工作。所以我想我是在正确的轨道上。
I have included menu items to translate the GUI dynamically at run time . Initially I tried this (na?ve) command ...
我已经包含了在运行时动态转换 GUI 的菜单项。最初我尝试了这个(天真的)命令......
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 .
...并且每种语言的菜单项都以这种方式在 XAML 中绑定到它。
<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.Cultureits DictionaryEventis triggered, so I thought this would update the GUI automatically . Obviously I'm wrong .
事实是这种方法行不通。文化信息仍然设置为"en-US"无论如何。我在设置LocalizeDictionary.Instance.Culture它时读到它DictionaryEvent被触发,所以我认为这会自动更新 GUI。显然我错了。
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 ?
- 在运行时使用WPF Localize Extension翻译窗口的推荐方法是什么?
- 我如何列出可用的翻译?
Thanks in advance .
提前致谢 。
回答by Olemis Lang
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.
上次编译库时,我似乎不小心引入了一个错字(或者我的 HDD/CPU 中有鬼影)。设置后语言切换现在工作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.
...至少这是一个简单的方法,只要资源文件 l10n 提供程序处于活动状态,它就会起作用。
回答by aKzenT
The LocalizeExtension's Cultureproperty 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.
该LocalizeExtension的Culture财产独立线程的UI文化。有时这可能是需要的,因为线程文化确实会影响很多事情。我们在我们自己的项目中使用扩展并手动设置线程文化以匹配LocalizeDictionary的文化。
Normally this should update the UI automatically. Make sure you are using the LocTextmarkup extension in your XAML, e.g:
通常这应该自动更新 UI。确保您LocText在 XAML中使用标记扩展,例如:
<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.
但是,我建议您只在代码中提供语言的修复列表,或者如果您使用的是 IoC 容器,请在那里注册可用语言。
回答by Shahin Dohan
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.
这并没有回答 OP 的问题,但我想无论如何我都会将它发布在这里,因为问题的标题是相关的。
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 FallbackValueof a textblock, I was using this property to show a default text when the binding was null.
我正在翻译FallbackValue文本块的 ,当绑定为空时,我使用此属性显示默认文本。
Anyway, to catch errors like this, you can subscribe to the following event:
无论如何,要捕获这样的错误,您可以订阅以下事件:
LocalizeDictionary.Instance.MissingKeyEvent += (sender, args) =>
{
//Do something
};

