Gtk.stock 已弃用,有什么替代方案?

Gtk.stock is deprecated, what's the alternative?

我一直在学习向 Gtk 开发,大多数在线示例都建议使用 Gtk.stock 图标。但是,它的使用会产生警告,表明它已被弃用,我找不到这些图标的替代品。

代码示例是:

    open_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.OPEN)
    open_button.clicked.connect (openfile)

    new_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.NEW)
    new_button.clicked.connect (createNew)

    save_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.SAVE)
    save_button.clicked.connect (saveFile)

生成错误为:

   /tmp/text_editor-exercise_7_1.vala.c:258:2: warning: 'GtkStock' is deprecated [-Wdeprecated-declarations]
     _tmp1_ = (GtkToolButton*) gtk_tool_button_new_from_stock (GTK_STOCK_OPEN);

哪个是替代方案,它在上面的代码中看起来如何?

GTK+3 已经移到 freedesktop.org Icon Naming Specification and internationalised labels. Taking Gtk.Stock.OPEN as an example. The GNOME Developer documentation for GTK_STOCK_OPEN 给出了两个替换:

GTK_STOCK_OPEN has been deprecated since version 3.10 and should not be used in newly-written code. Use named icon "document-open" or the label "_Open".

命名图标方法

命名图标方法类似于:

var open_icon = new Gtk.Image.from_icon_name( "document-open",
                                              IconSize.SMALL_TOOLBAR
                                              )
var open_button = new Gtk.ToolButton( open_icon, null )

标签方法

标签方法利用gettext将标签翻译成程序当前的运行时间语言。这由标签前的下划线表示。您程序中的行将是:

var open_button = new Gtk.ToolButton( null, dgettext( "gtk30", "_Open") )

gettext 使用域,即包含翻译的文件。 Gtk+3 域是 gtk30。您还需要在程序开头添加一行,以将 C 语言的默认语言环境(美国英语 ASCII)更改为 运行 时间环境的语言环境:

init
    Intl.setlocale()

要编译 Genie 程序,您需要为 gettext 设置默认域。这通常设置为空:

valac -X -DGETTEXT_PACKAGE --pkg gtk+-3.0 my_program.gs

当您 运行 您的程序时,您将获得翻译成您的语言环境的“_Open”。您还可以更改语言环境。如果您安装了法语语言环境,那么 运行 将程序设置为:

LC_ALL=fr ./my_program

将以法语显示“_Open”标签。

您可能会在示例中看到 _( "_OPEN" )_() 是一个类似于 dgettext 的函数,但使用默认域。您可能希望将默认域保留为您自己程序的翻译文件。使用 _( "_translate me" )dgettext( "mydomain", "_translate me" ) 少输入一些。要在 Genie 中设置默认域,请在 init:

之前添加一行
const GETTEXT_PACKAGE:string = "mydomain"

init
    Intl.setlocale()