From e03fdccc4e191e0e7cd60b4fef9e6f603ccc112b Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Sat, 26 Jun 2021 15:32:40 +0200 Subject: Prepare for internationalization weltschmerz currently does not allow for translation and exists in English only. This is a serious defect for people who do not speak that language. The following series of commits aims to remedy this by adding the necessary infrastructure and build steps to enable easy translation. To do this we use GNU gettext[1]; its tooling is widely supported and a de-facto standard. Since we need to tell gettext where to find the compiled .mo translation files, have both the Makefile and meson pass the locale directory to weltschmerz in form of a LOCALEDIR macro. The translation domain will be "weltschmerz". Not only will any strings in the Vala code have to be translated, we need to make sure to translate terminal.ui and weltschmerz.desktop as well. The former will be taken care of through POTFILES only, but for the latter we have to define an intermediary build step. To ensure compatibility with the Makefile, we suffix the desktop file with '.in'. For now, LINGUAS and POTFILES remain empty. A future commit will document all files needing translation. LINGUAS will be updated as translations are completed. [1] https://www.gnu.org/software/gettext --- .gitignore | 2 ++ Makefile | 22 +++++++++++++++++++--- meson.build | 12 +++++++++++- po/LINGUAS | 0 po/POTFILES | 0 po/meson.build | 7 +++++++ weltschmerz.desktop | 9 --------- weltschmerz.desktop.in | 9 +++++++++ weltschmerz.vala | 5 +++++ 9 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 po/LINGUAS create mode 100644 po/POTFILES create mode 100644 po/meson.build delete mode 100644 weltschmerz.desktop create mode 100644 weltschmerz.desktop.in diff --git a/.gitignore b/.gitignore index 4aad62a..931223b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ build/* *.c +*.mo weltschmerz +weltschmerz.desktop diff --git a/Makefile b/Makefile index 6e84f4f..4be1213 100644 --- a/Makefile +++ b/Makefile @@ -2,22 +2,38 @@ PREFIX ?= /usr/local EXEC_PREFIX ?= ${PREFIX} BINDIR ?= ${EXEC_PREFIX}/bin DATAROOTDIR ?= ${PREFIX}/share +LOCALEDIR ?= ${DATAROOTDIR}/locale MANDIR ?= ${DATAROOTDIR}/man VALAC ?= valac +POFILES := $(wildcard po/*.po) +MOFILES := $(POFILES:.po=.mo) + weltschmerz: weltschmerz.vala terminal.vala config.vala configreader.vala utils.vala resources.c - ${VALAC} -X -lm --pkg posix --pkg gtk+-3.0 --pkg vte-2.91 --gresources resources.xml \ + ${VALAC} -X -DGETTEXT_PACKAGE=\"weltschmerz\" -X -DLOCALEDIR="\"${LOCALEDIR}\"" \ + -X -lm --pkg posix --pkg gtk+-3.0 --pkg vte-2.91 --gresources resources.xml \ weltschmerz.vala terminal.vala config.vala configreader.vala utils.vala resources.c resources.c: resources.xml terminal.ui terminal.css glib-compile-resources $< --target=$@ --generate-source -install: weltschmerz weltschmerz.1 weltschmerz.desktop +%.mo: %.po + msgfmt $< -o $@ + +weltschmerz.desktop: weltschmerz.desktop.in + msgfmt --desktop -d po --template $< -o $@ + +install: weltschmerz weltschmerz.1 weltschmerz.desktop $(MOFILES) install -D -m 755 -t '${DESTDIR}${BINDIR}' weltschmerz install -D -m 644 -t '${DESTDIR}${MANDIR}/man1' weltschmerz.1 install -D -m 644 -t '${DESTDIR}${DATAROOTDIR}/applications' weltschmerz.desktop + for mo in $(MOFILES); do \ + lang=$$(basename "$$mo" .mo); \ + install -D -m 644 "$$mo" "${DESTDIR}${LOCALEDIR}/$$lang/LC_MESSAGES/weltschmerz.mo"; \ + done clean: - rm -f weltschmerz resources.c + rm -f weltschmerz weltschmerz.desktop resources.c + rm -f po/*.mo .PHONY: install clean diff --git a/meson.build b/meson.build index dc83f80..4bae2de 100644 --- a/meson.build +++ b/meson.build @@ -11,10 +11,20 @@ dependencies = [ dependency('vte-2.91'), ] +subdir('po') + sources = files('weltschmerz.vala', 'terminal.vala', 'config.vala', 'configreader.vala', 'utils.vala') sources += gnome.compile_resources('resources', 'resources.xml') executable('weltschmerz', sources, dependencies: dependencies, install: true) install_man('weltschmerz.1') -install_data('weltschmerz.desktop', install_dir: join_paths(get_option('datadir'), 'applications')) + +i18n.merge_file( + input: 'weltschmerz.desktop.in', + output: 'weltschmerz.desktop', + po_dir: 'po', + type: 'desktop', + install: true, + install_dir: get_option('datadir') / 'applications', +) diff --git a/po/LINGUAS b/po/LINGUAS new file mode 100644 index 0000000..e69de29 diff --git a/po/POTFILES b/po/POTFILES new file mode 100644 index 0000000..e69de29 diff --git a/po/meson.build b/po/meson.build new file mode 100644 index 0000000..6870f58 --- /dev/null +++ b/po/meson.build @@ -0,0 +1,7 @@ +i18n = import('i18n') + +localedir = join_paths(get_option('prefix'), get_option('localedir')) +add_project_arguments('-DGETTEXT_PACKAGE="weltschmerz"', language: 'c') +add_project_arguments('-DLOCALEDIR="@0@"'.format(localedir), language: 'c') + +i18n.gettext(meson.project_name(), args: '--directory=' + meson.source_root()) diff --git a/weltschmerz.desktop b/weltschmerz.desktop deleted file mode 100644 index 2b61982..0000000 --- a/weltschmerz.desktop +++ /dev/null @@ -1,9 +0,0 @@ -[Desktop Entry] -Name=weltschmerz -Comment=A terminal emulator in Vala -Type=Application -Exec=weltschmerz -TryExec=weltschmerz -Icon=utilities-terminal -Keywords=shell;prompt;command;commandline;cmd; -Categories=GTK;System;TerminalEmulator; diff --git a/weltschmerz.desktop.in b/weltschmerz.desktop.in new file mode 100644 index 0000000..2b61982 --- /dev/null +++ b/weltschmerz.desktop.in @@ -0,0 +1,9 @@ +[Desktop Entry] +Name=weltschmerz +Comment=A terminal emulator in Vala +Type=Application +Exec=weltschmerz +TryExec=weltschmerz +Icon=utilities-terminal +Keywords=shell;prompt;command;commandline;cmd; +Categories=GTK;System;TerminalEmulator; diff --git a/weltschmerz.vala b/weltschmerz.vala index dac2291..42349d9 100644 --- a/weltschmerz.vala +++ b/weltschmerz.vala @@ -1,3 +1,4 @@ +[CCode(cname="LOCALEDIR")] extern const string LOCALEDIR; const string PROGRAM_NAME = "weltschmerz"; void warning(string message) { @@ -5,6 +6,10 @@ void warning(string message) { } static int main(string[] args) { + Intl.bindtextdomain(PROGRAM_NAME, LOCALEDIR); + Intl.bind_textdomain_codeset(PROGRAM_NAME, "UTF-8"); + Intl.textdomain(PROGRAM_NAME); + unowned string[]? nullargs = null; Gtk.init(ref nullargs); -- cgit v1.2.3-2-gb3c3