Skip to content

Commit ca16e21

Browse files
committed
Support interface localisation using GNU gettext
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
1 parent 235b925 commit ca16e21

File tree

10 files changed

+141
-6
lines changed

10 files changed

+141
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Install dependencies:
3434
* wayland
3535
* wayland-protocols \*
3636
* libxkbcommon
37+
* xkeyboard-config
3738
* cairo
3839
* gdk-pixbuf2 \*\*
3940
* pam (optional)

main.c

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <unistd.h>
1616
#include <wayland-client.h>
1717
#include <wordexp.h>
18+
#include <locale.h>
19+
#include <libintl.h>
1820
#include "background-image.h"
1921
#include "cairo.h"
2022
#include "comm.h"
@@ -1101,6 +1103,10 @@ int main(int argc, char **argv) {
11011103
initialize_pw_backend(argc, argv);
11021104
srand(time(NULL));
11031105

1106+
setlocale(LC_ALL, "");
1107+
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
1108+
textdomain(GETTEXT_PACKAGE);
1109+
11041110
enum line_mode line_mode = LM_LINE;
11051111
state.failed_attempts = 0;
11061112
state.args = (struct swaylock_args){

meson.build

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(
33
'c',
44
version: '1.6',
55
license: 'MIT',
6-
meson_version: '>=0.48.0',
6+
meson_version: '>=0.50.0',
77
default_options: [
88
'c_std=c11',
99
'warning_level=2',
@@ -27,6 +27,8 @@ sysconfdir = get_option('sysconfdir')
2727
prefix = get_option('prefix')
2828
is_freebsd = host_machine.system().startswith('freebsd')
2929

30+
subdir('po')
31+
3032
add_project_arguments(
3133
'-DSYSCONFDIR="/@0@"'.format(join_paths(prefix, sysconfdir)),
3234
language : 'c')
@@ -38,13 +40,15 @@ endif
3840
wayland_client = dependency('wayland-client')
3941
wayland_protos = dependency('wayland-protocols', version: '>=1.14')
4042
xkbcommon = dependency('xkbcommon')
43+
xkeyboard_config = dependency('xkeyboard-config')
4144
cairo = dependency('cairo')
4245
gdk_pixbuf = dependency('gdk-pixbuf-2.0', required: get_option('gdk-pixbuf'))
4346
bash_comp = dependency('bash-completion', required: false)
4447
fish_comp = dependency('fish', required: false)
4548
libpam = cc.find_library('pam', required: get_option('pam'))
4649
crypt = cc.find_library('crypt', required: not libpam.found())
4750
math = cc.find_library('m')
51+
intl = cc.fing_library('intl', required: false)
4852

4953
git = find_program('git', required: false)
5054
scdoc = find_program('scdoc', required: get_option('man-pages'))
@@ -116,6 +120,7 @@ dependencies = [
116120
cairo,
117121
client_protos,
118122
gdk_pixbuf,
123+
intl,
119124
math,
120125
xkbcommon,
121126
wayland_client,

po/LINGUAS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
de fr

po/POTFILES

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
render.c

po/de.po

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# German translations for swaylock package.
2+
# Copyright (C) 2020 THE swaylock'S COPYRIGHT HOLDER
3+
# This file is distributed under the same license as the swaylock package.
4+
# Automatically generated, 2020.
5+
#
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: swaylock 1.6\n"
9+
"Report-Msgid-Bugs-To: https://github.com/swaywm/swaylock/issues\n"
10+
"POT-Creation-Date: 2020-08-18 14:46+0200\n"
11+
"PO-Revision-Date: 2020-08-18 15:25+0200\n"
12+
"Last-Translator: Automatically generated\n"
13+
"Language-Team: none\n"
14+
"Language: de\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=UTF-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
19+
20+
#: render.c:161
21+
msgid "verifying"
22+
msgstr "Überprüfung"
23+
24+
#: render.c:164
25+
msgid "wrong"
26+
msgstr "falsch"
27+
28+
#: render.c:167
29+
msgid "cleared"
30+
msgstr "gelöscht"
31+
32+
#: render.c:174
33+
msgid "Caps Lock"
34+
msgstr "Feststelltaste"

po/fr.po

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# French translations for swaylock package.
2+
# Copyright (C) 2020 THE swaylock'S COPYRIGHT HOLDER
3+
# This file is distributed under the same license as the swaylock package.
4+
# Automatically generated, 2020.
5+
#
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: swaylock 1.6\n"
9+
"Report-Msgid-Bugs-To: https://github.com/swaywm/swaylock/issues\n"
10+
"POT-Creation-Date: 2020-08-18 14:46+0200\n"
11+
"PO-Revision-Date: 2020-08-18 14:50+0200\n"
12+
"Last-Translator: Automatically generated\n"
13+
"Language-Team: none\n"
14+
"Language: fr\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=UTF-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
19+
20+
#: render.c:161
21+
msgid "verifying"
22+
msgstr "vérification"
23+
24+
#: render.c:164
25+
msgid "wrong"
26+
msgstr "incorrect"
27+
28+
#: render.c:167
29+
msgid "cleared"
30+
msgstr "effacé"
31+
32+
#: render.c:174
33+
msgid "Caps Lock"
34+
msgstr "Verr Maj"

po/meson.build

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
i18n = import('i18n')
2+
3+
add_project_arguments(
4+
[
5+
'-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name()),
6+
'-DLOCALEDIR="@0@"'.format(get_option('prefix') / get_option('localedir'))
7+
],
8+
language:'c'
9+
)
10+
i18n.gettext(meson.project_name(),
11+
args: [
12+
'--directory=' + meson.source_root(),
13+
'--package-name=' + meson.project_name(),
14+
'--package-version=' + meson.project_version(),
15+
'--msgid-bugs-address=https://github.com/swaywm/swaylock/issues',
16+
]
17+
)

po/swaylock.pot

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3+
# This file is distributed under the same license as the swaylock package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5+
#
6+
#, fuzzy
7+
msgid ""
8+
msgstr ""
9+
"Project-Id-Version: swaylock 1.6\n"
10+
"Report-Msgid-Bugs-To: https://github.com/swaywm/swaylock/issues\n"
11+
"POT-Creation-Date: 2020-08-18 14:46+0200\n"
12+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14+
"Language-Team: LANGUAGE <[email protected]>\n"
15+
"Language: \n"
16+
"MIME-Version: 1.0\n"
17+
"Content-Type: text/plain; charset=CHARSET\n"
18+
"Content-Transfer-Encoding: 8bit\n"
19+
20+
#: render.c:161
21+
msgid "verifying"
22+
msgstr ""
23+
24+
#: render.c:164
25+
msgid "wrong"
26+
msgstr ""
27+
28+
#: render.c:167
29+
msgid "cleared"
30+
msgstr ""
31+
32+
#: render.c:174
33+
msgid "Caps Lock"
34+
msgstr ""

render.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <math.h>
22
#include <stdlib.h>
33
#include <wayland-client.h>
4+
#include <libintl.h>
45
#include "cairo.h"
56
#include "background-image.h"
67
#include "swaylock.h"
@@ -157,20 +158,20 @@ void render_frame(struct swaylock_surface *surface) {
157158
}
158159
switch (state->auth_state) {
159160
case AUTH_STATE_VALIDATING:
160-
text = "verifying";
161+
text = gettext("verifying");
161162
break;
162163
case AUTH_STATE_INVALID:
163-
text = "wrong";
164+
text = gettext("wrong");
164165
break;
165166
case AUTH_STATE_CLEAR:
166-
text = "cleared";
167+
text = gettext("cleared");
167168
break;
168169
case AUTH_STATE_INPUT:
169170
case AUTH_STATE_INPUT_NOP:
170171
case AUTH_STATE_BACKSPACE:
171172
// Caps Lock has higher priority
172173
if (state->xkb.caps_lock && state->args.show_caps_lock_text) {
173-
text = "Caps Lock";
174+
text = gettext("Caps Lock");
174175
} else if (state->args.show_failed_attempts &&
175176
state->failed_attempts > 0) {
176177
if (state->failed_attempts > 999) {
@@ -193,7 +194,8 @@ void render_frame(struct swaylock_surface *surface) {
193194
++curr_layout;
194195
}
195196
// will handle invalid index if none are active
196-
layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout);
197+
layout_text = dgettext("xkeyboard-config",
198+
xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout));
197199
}
198200
break;
199201
default:

0 commit comments

Comments
 (0)