WPF:本地化WPF功能区QAT上下文菜单文本

时间:2011-02-16 08:54:49

标签: wpf visual-studio-2010 .net-4.0 ribbon

我正在使用.NET 4.0 WPF功能区,我正在寻找一种本地化功能区上下文菜单字符串的方法。默认情况下,右键单击功能区会显示“最小化功能区”等项目。我想将这些字符串本地化,例如显示“Multifunktionsleiste verbergen”。

我该怎么做?这有可能吗?

4 个答案:

答案 0 :(得分:1)

我不确定但是如果不从源代码重新编译库就不可能(如果我不对,那么对于某人来说这很好)因为功能区库定义了ResourcesManager

new ResourceManager("ExceptionStringTable", typeof(SR).Assembly);

如果要更改源,则这些文本存储在名为ExceptionStringTable.txt的资源文件中。

字符串的标识符为:

  • RibbonContextMenu_ShowQATAbove
  • RibbonContextMenu_ShowQATBelow
  • RibbonContextMenu_MaximizeTheRibbon
  • RibbonContextMenu_MinimizeTheRibbon

图书馆的资源可以从here下载。在RibbonControlsLibrary \ Resources \中,您可以找到Ribbon的资源实现。

答案 1 :(得分:1)

另一种方法是通过反射替换值:

  FieldInfo pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("AddToQATText", (BindingFlags.NonPublic | BindingFlags.Static));
  pi.SetValue(null, "Localized text");

此代码必须在第一次使用功能区控件之前执行(通常是主窗口的InitializeComponent)

答案 2 :(得分:1)

(希望)完全扩展到userxyz的解决方案:

        FieldInfo pi;

        pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("AddToQATText", (BindingFlags.NonPublic | BindingFlags.Static));
        pi.SetValue(null, "RibbonContext_AddToQATText");
        pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("RemoveFromQATText", (BindingFlags.NonPublic | BindingFlags.Static));
        pi.SetValue(null, "RibbonContext_RemoveFromQATText");
        pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("ShowQATAboveText", (BindingFlags.NonPublic | BindingFlags.Static));
        pi.SetValue(null, "RibbonContext_ShowQATAboveText");
        pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("ShowQATBelowText", (BindingFlags.NonPublic | BindingFlags.Static));
        pi.SetValue(null, "RibbonContext_ShowQATBelowText");
        pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("MaximizeTheRibbonText", (BindingFlags.NonPublic | BindingFlags.Static));
        pi.SetValue(null, "RibbonContext_MaximizeTheRibbonText");
        pi = typeof(Microsoft.Windows.Controls.Ribbon.RibbonContextMenu).GetField("MinimizeTheRibbonText", (BindingFlags.NonPublic | BindingFlags.Static));
        pi.SetValue(null, "RibbonContext_MinimizeTheRibbonText");

答案 3 :(得分:0)

我想根据之前的讨论提出另一个解决方案:

var ribbonResourceManager = new RibbonResourceManager();

var translatorType = typeof(RibbonContextMenu)
    .Assembly.GetType("Microsoft.Windows.Controls.SR");
var resourceManagerField = translatorType.GetField("_resourceManager", 
    BindingFlags.NonPublic | BindingFlags.Static);
resourceManagerField.SetValue(null, ribbonResourceManager);

其中RibbonReourceManager是具有重写的GetString方法的基本ResourceManager的继承者。并且RibbonResourceManager应该返回下面列出的所有字符串:

  • “RibbonQuickAccessToolBar_OverflowButtonToolTip”
  • “QATKeyTipCharacters”
  • “RibbonContextMenu_AddToQAT”
  • “RibbonContextMenu_AddGalleryToQAT”
  • “RibbonContextMenu_RemoveFromQAT”
  • “RibbonContextMenu_ShowQATAbove”
  • “RibbonContextMenu_ShowQATBelow”
  • “RibbonContextMenu_MaximizeTheRibbon”
  • “RibbonContextMenu_MinimizeTheRibbon”