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

发布于 2024-10-17 22:04:42 字数 153 浏览 2 评论 0原文

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

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

I'm playing with the .NET 4.0 WPF ribbon and am looking for a way to localize the Ribbon context menu strings. By default, right clicking the ribbon shows items like "Minimize the Ribbon". I'd like to localize these strings to for example display "Multifunktionsleiste verbergen".

How would I do that? Is this possible at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

行雁书 2024-10-24 22:04:42

(希望)对 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");

A (hopefully) complete extension to userxyz's solution:

        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");
花伊自在美 2024-10-24 22:04:42

我不确定,但如果不从源重新编译库,这是不可能的(如果我不正确,那么从某人这里来会很棒),因为功能区库定义了 ResourcesManager

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

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

字符串的标识符为:

  • RibbonContextMenu_ShowQATAbove
  • RibbonContextMenu_ShowQATBelow
  • RibbonContextMenu_MaximizeTheRibbon
  • RibbonContextMenu_MinimizeTheRibbon

库源可以从 此处。在 RibbonControlsLibrary\Resources\ 中,您可以找到 Ribbon 资源的实现。

I am not sure but it cannot be possible without recompling library from sources (If I am not correct it would be great to here from someone) as the ribbon library defines ResourcesManager

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

If you are changing source those texts are stored in resources file called ExceptionStringTable.txt.

The identifiers for strings are:

  • RibbonContextMenu_ShowQATAbove
  • RibbonContextMenu_ShowQATBelow
  • RibbonContextMenu_MaximizeTheRibbon
  • RibbonContextMenu_MinimizeTheRibbon

Sources for library can be downloaded from here. In RibbonControlsLibrary\Resources\ you can found the implementation of resources for Ribbon.

无人接听 2024-10-24 22:04:42

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

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

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

Another approach would be to replace the values by reflection:

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

This code has to be executed before the first usage of the ribbon control (typically InitializeComponent of the main window)

诗笺 2024-10-24 22:04:42

我想建议基于之前讨论的另一种解决方案:

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 是基本 ResourceManager 的继承者,具有重写的 GetString 方法。并且 RibbonResourceManager 应返回下面列出的所有字符串:

  • "RibbonQuickAccessToolBar_OverflowButtonToolTip"
  • "QATKeyTipCharacters"
  • "RibbonContextMenu_AddToQAT
  • " "RibbonContextMenu_AddGalleryToQAT"
  • "RibbonContextMenu_RemoveFromQAT"
  • "RibbonContextMenu_ShowQATAbove" "
  • RibbonContextMenu_ShowQATBelow" "
  • RibbonContext Menu_MaximizeTheRibbon"
  • "RibbonContextMenu_MinimizeTheRibbon"

I would like to suggest another one solution based on previously discussed:

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

where RibbonReourceManager is inheritor of the basic ResourceManager with overridden GetString methods. And RibbonResourceManager should return all strings listed below:

  • "RibbonQuickAccessToolBar_OverflowButtonToolTip"
  • "QATKeyTipCharacters"
  • "RibbonContextMenu_AddToQAT"
  • "RibbonContextMenu_AddGalleryToQAT"
  • "RibbonContextMenu_RemoveFromQAT"
  • "RibbonContextMenu_ShowQATAbove"
  • "RibbonContextMenu_ShowQATBelow"
  • "RibbonContextMenu_MaximizeTheRibbon"
  • "RibbonContextMenu_MinimizeTheRibbon"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文