Skip to content

Commit

Permalink
gtk: extract translations from Zig source code
Browse files Browse the repository at this point in the history
  • Loading branch information
pluiedev committed Feb 27, 2025
1 parent 697e04b commit 85dbf8f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
12 changes: 11 additions & 1 deletion po/com.mitchellh.ghostty.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: com.mitchellh.ghostty\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2025-02-26 14:34+0100\n"
"POT-Creation-Date: 2025-02-27 12:03+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -132,6 +132,7 @@ msgid "Terminal Inspector"
msgstr ""

#: src/apprt/gtk/ui/1.0/menu-window-titlebar_menu.blp:102
#: src/apprt/gtk/Window.zig:923
msgid "About Ghostty"
msgstr ""

Expand Down Expand Up @@ -175,3 +176,12 @@ msgid ""
"Pasting this text into the terminal may be dangerous as it looks like some "
"commands may be executed."
msgstr ""

#: src/apprt/gtk/Window.zig:257
msgid ""
"⚠️ You're running a debug build of Ghostty! Performance will be degraded."
msgstr ""

#: src/apprt/gtk/Window.zig:904
msgid "Ghostty Developers"
msgstr ""
7 changes: 4 additions & 3 deletions src/apprt/gtk/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const TabView = @import("TabView.zig");
const HeaderBar = @import("headerbar.zig");
const version = @import("version.zig");
const winproto = @import("winproto.zig");
const i18n = @import("i18n.zig");

const log = std.log.scoped(.gtk);

Expand Down Expand Up @@ -253,7 +254,7 @@ pub fn init(self: *Window, app: *App) !void {
// This is a really common issue where people build from source in debug and performance is really bad.
if (comptime std.debug.runtime_safety) {
const warning_box = c.gtk_box_new(c.GTK_ORIENTATION_VERTICAL, 0);
const warning_text = "⚠️ You're running a debug build of Ghostty! Performance will be degraded.";
const warning_text = i18n._("⚠️ You're running a debug build of Ghostty! Performance will be degraded.");
if (adwaita.versionAtLeast(1, 3, 0)) {
const banner = c.adw_banner_new(warning_text);
c.adw_banner_set_revealed(@ptrCast(banner), 1);
Expand Down Expand Up @@ -900,7 +901,7 @@ fn gtkActionAbout(
"application-name",
name,
"developer-name",
"Ghostty Developers",
i18n._("Ghostty Developers"),
"application-icon",
icon,
"version",
Expand All @@ -919,7 +920,7 @@ fn gtkActionAbout(
"logo-icon-name",
icon,
"title",
"About Ghostty",
i18n._("About Ghostty"),
"version",
build_config.version_string.ptr,
"website",
Expand Down
2 changes: 2 additions & 0 deletions src/apprt/gtk/i18n.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ pub fn init(alloc: std.mem.Allocator) !void {
// as libintl.h isn't always easily available (e.g. in musl)
extern fn bindtextdomain(domainname: [*:0]const u8, dirname: [*:0]const u8) ?[*:0]const u8;
extern fn textdomain(domainname: [*:0]const u8) ?[*:0]const u8;
pub extern fn gettext(msgid: [*:0]const u8) [*:0]const u8;
pub const _ = gettext;
28 changes: 21 additions & 7 deletions src/build/GhosttyI18n.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyI18n {
var steps = std.ArrayList(*std.Build.Step).init(b.allocator);
errdefer steps.deinit();

addUpdateStep(b);
try addUpdateStep(b);

if (cfg.app_runtime == .gtk) {
// Output the .mo files used by the GTK apprt
Expand All @@ -41,10 +41,10 @@ pub fn install(self: *const GhosttyI18n) void {
for (self.steps) |step| self.owner.getInstallStep().dependOn(step);
}

fn addUpdateStep(b: *std.Build) void {
fn addUpdateStep(b: *std.Build) !void {
const pot_step = b.step("update-translations", "Update translation files");

const gettext = b.addSystemCommand(&.{
const xgettext = b.addSystemCommand(&.{
"xgettext",
"--language=C", // Silence the "unknown extension" errors
"--from-code=UTF-8",
Expand All @@ -63,21 +63,35 @@ fn addUpdateStep(b: *std.Build) void {
// would be added to the file as its location, which differs for
// everyone's checkout of the repository.
// This comes at a cost of losing per-file caching, of course.
gettext.addArg(std.fmt.comptimePrint("src/apprt/gtk/ui/{[major]}.{[minor]}/{[name]s}.blp", blp));
xgettext.addArg(std.fmt.comptimePrint("src/apprt/gtk/ui/{[major]}.{[minor]}/{[name]s}.blp", blp));
}

var gtk_files = try b.build_root.handle.openDir("src/apprt/gtk", .{ .iterate = true });
defer gtk_files.close();

var walk = try gtk_files.walk(b.allocator);
defer walk.deinit();

while (try walk.next()) |src| {
switch (src.kind) {
.file => if (!std.mem.endsWith(u8, src.basename, ".zig")) continue,
else => continue,
}
xgettext.addArg((b.pathJoin(&.{ "src/apprt/gtk", src.path })));
}

// Don't make Zig cache it
gettext.has_side_effects = true;
xgettext.has_side_effects = true;

const new_pot = gettext.captureStdOut();
const new_pot = xgettext.captureStdOut();

const wf = b.addWriteFiles();
wf.addCopyFileToSource(new_pot, "po/" ++ domain ++ ".pot");

inline for (locales) |locale| {
const msgmerge = b.addSystemCommand(&.{ "msgmerge", "-q" });
msgmerge.addFileArg(b.path("po/" ++ locale ++ ".po"));
msgmerge.addFileArg(gettext.captureStdOut());
msgmerge.addFileArg(xgettext.captureStdOut());

wf.addCopyFileToSource(msgmerge.captureStdOut(), "po/" ++ locale ++ ".po");
}
Expand Down

0 comments on commit 85dbf8f

Please sign in to comment.