Skip to content

Commit

Permalink
Support interface localisation using GNU gettext
Browse files Browse the repository at this point in the history
The xkeyboard-config library is required as a runtime dependency to
present a localized string of the keyboard layout.

Useful links:
- https://www.gnu.org/software/gettext/manual/gettext.html#Sources
- https://mesonbuild.com/Localisation.html
- https://mesonbuild.com/i18n-module.html
  • Loading branch information
MisterDA committed Nov 29, 2020
1 parent 235b925 commit 1522c86
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Install dependencies:
* wayland
* wayland-protocols \*
* libxkbcommon
* xkeyboard-config
* cairo
* gdk-pixbuf2 \*\*
* pam (optional)
Expand Down
6 changes: 6 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <unistd.h>
#include <wayland-client.h>
#include <wordexp.h>
#include <locale.h>
#include <libintl.h>
#include "background-image.h"
#include "cairo.h"
#include "comm.h"
Expand Down Expand Up @@ -1101,6 +1103,10 @@ int main(int argc, char **argv) {
initialize_pw_backend(argc, argv);
srand(time(NULL));

setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
textdomain(GETTEXT_PACKAGE);

enum line_mode line_mode = LM_LINE;
state.failed_attempts = 0;
state.args = (struct swaylock_args){
Expand Down
5 changes: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(
'c',
version: '1.6',
license: 'MIT',
meson_version: '>=0.48.0',
meson_version: '>=0.50.0',
default_options: [
'c_std=c11',
'warning_level=2',
Expand All @@ -27,6 +27,8 @@ sysconfdir = get_option('sysconfdir')
prefix = get_option('prefix')
is_freebsd = host_machine.system().startswith('freebsd')

subdir('po')

add_project_arguments(
'-DSYSCONFDIR="/@0@"'.format(join_paths(prefix, sysconfdir)),
language : 'c')
Expand All @@ -38,6 +40,7 @@ endif
wayland_client = dependency('wayland-client')
wayland_protos = dependency('wayland-protocols', version: '>=1.14')
xkbcommon = dependency('xkbcommon')
xkeyboard_config = dependency('xkeyboard-config')
cairo = dependency('cairo')
gdk_pixbuf = dependency('gdk-pixbuf-2.0', required: get_option('gdk-pixbuf'))
bash_comp = dependency('bash-completion', required: false)
Expand Down
1 change: 1 addition & 0 deletions po/LINGUAS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de fr
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
render.c
34 changes: 34 additions & 0 deletions po/de.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# German translations for swaylock package.
# Copyright (C) 2020 THE swaylock'S COPYRIGHT HOLDER
# This file is distributed under the same license as the swaylock package.
# Automatically generated, 2020.
#
msgid ""
msgstr ""
"Project-Id-Version: swaylock 1.6\n"
"Report-Msgid-Bugs-To: https://github.com/swaywm/swaylock/issues\n"
"POT-Creation-Date: 2020-08-18 14:46+0200\n"
"PO-Revision-Date: 2020-08-18 15:25+0200\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: render.c:161
msgid "verifying"
msgstr "Überprüfung"

#: render.c:164
msgid "wrong"
msgstr "falsch"

#: render.c:167
msgid "cleared"
msgstr "gelöscht"

#: render.c:174
msgid "Caps Lock"
msgstr "Feststelltaste"
34 changes: 34 additions & 0 deletions po/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# French translations for swaylock package.
# Copyright (C) 2020 THE swaylock'S COPYRIGHT HOLDER
# This file is distributed under the same license as the swaylock package.
# Automatically generated, 2020.
#
msgid ""
msgstr ""
"Project-Id-Version: swaylock 1.6\n"
"Report-Msgid-Bugs-To: https://github.com/swaywm/swaylock/issues\n"
"POT-Creation-Date: 2020-08-18 14:46+0200\n"
"PO-Revision-Date: 2020-08-18 14:50+0200\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: render.c:161
msgid "verifying"
msgstr "vérification"

#: render.c:164
msgid "wrong"
msgstr "incorrect"

#: render.c:167
msgid "cleared"
msgstr "effacé"

#: render.c:174
msgid "Caps Lock"
msgstr "Verr Maj"
17 changes: 17 additions & 0 deletions po/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
i18n = import('i18n')

add_project_arguments(
[
'-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name()),
'-DLOCALEDIR="@0@"'.format(get_option('prefix') / get_option('localedir'))
],
language:'c'
)
i18n.gettext(meson.project_name(),
args: [
'--directory=' + meson.source_root(),
'--package-name=' + meson.project_name(),
'--package-version=' + meson.project_version(),
'--msgid-bugs-address=https://github.com/swaywm/swaylock/issues',
]
)
34 changes: 34 additions & 0 deletions po/swaylock.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the swaylock package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: swaylock 1.6\n"
"Report-Msgid-Bugs-To: https://github.com/swaywm/swaylock/issues\n"
"POT-Creation-Date: 2020-08-18 14:46+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: render.c:161
msgid "verifying"
msgstr ""

#: render.c:164
msgid "wrong"
msgstr ""

#: render.c:167
msgid "cleared"
msgstr ""

#: render.c:174
msgid "Caps Lock"
msgstr ""
12 changes: 7 additions & 5 deletions render.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <math.h>
#include <stdlib.h>
#include <wayland-client.h>
#include <libintl.h>
#include "cairo.h"
#include "background-image.h"
#include "swaylock.h"
Expand Down Expand Up @@ -157,20 +158,20 @@ void render_frame(struct swaylock_surface *surface) {
}
switch (state->auth_state) {
case AUTH_STATE_VALIDATING:
text = "verifying";
text = gettext("verifying");
break;
case AUTH_STATE_INVALID:
text = "wrong";
text = gettext("wrong");
break;
case AUTH_STATE_CLEAR:
text = "cleared";
text = gettext("cleared");
break;
case AUTH_STATE_INPUT:
case AUTH_STATE_INPUT_NOP:
case AUTH_STATE_BACKSPACE:
// Caps Lock has higher priority
if (state->xkb.caps_lock && state->args.show_caps_lock_text) {
text = "Caps Lock";
text = gettext("Caps Lock");
} else if (state->args.show_failed_attempts &&
state->failed_attempts > 0) {
if (state->failed_attempts > 999) {
Expand All @@ -193,7 +194,8 @@ void render_frame(struct swaylock_surface *surface) {
++curr_layout;
}
// will handle invalid index if none are active
layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout);
layout_text = dgettext("xkeyboard-config",
xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout));
}
break;
default:
Expand Down

0 comments on commit 1522c86

Please sign in to comment.