aboutsummaryrefslogtreecommitdiffstats
path: root/community/portfolio/0002-fall-back-to-gettext-module-on-systems-that.patch
blob: 4fec99783dccabf56cbbc2529bcf4e20cf4aa54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
From aad5dc56192584301bcdbd8e67d7f1157959ad06 Mon Sep 17 00:00:00 2001
From: Clayton Craft <clayton@craftyguy.net>
Date: Wed, 3 Mar 2021 14:45:18 -0800
Subject: [PATCH 1/2] translation: encapsulate gettext to handle systems
 without glibc

Some systems include python without glibc support, and will fail to find
gettext, bindtextdomain, etc in the locale module. One such system/OS
without glibc is Alpine Linux / postmarketOS.

This change moves gettext loading to a new wrapper that will fall back
to using the python gettext implementation [1] if python is built
without glibc support.

1. https://github.com/hannosch/python-gettext
---
 src/dev.tchx84.Portfolio.in |  6 +++---
 src/meson.build             |  1 +
 src/places.py               |  3 +--
 src/translation.py          | 21 +++++++++++++++++++++
 src/window.py               |  3 ++-
 src/worker.py               |  2 +-
 6 files changed, 29 insertions(+), 7 deletions(-)
 create mode 100644 src/translation.py

diff --git a/src/dev.tchx84.Portfolio.in b/src/dev.tchx84.Portfolio.in
index 9a9068a..b47d863 100755
--- a/src/dev.tchx84.Portfolio.in
+++ b/src/dev.tchx84.Portfolio.in
@@ -29,13 +29,13 @@ localedir = '@localedir@'
 sys.path.insert(1, pkgdatadir)
 signal.signal(signal.SIGINT, signal.SIG_DFL)
 
-locale.bindtextdomain('portfolio', localedir)
-locale.textdomain('portfolio')
-
 if __name__ == '__main__':
     from gi.repository import Gio
     resource = Gio.Resource.load(os.path.join(pkgdatadir, 'portfolio.gresource'))
     resource._register()
 
+    from portfolio.translation import translation_init
+    translation_init(localedir)
+
     from portfolio import main
     sys.exit(main.main(VERSION))
diff --git a/src/meson.build b/src/meson.build
index 801a8ee..a9d9d77 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -37,6 +37,7 @@ portfolio_sources = [
   'utils.py',
   'service.py',
   'place.py',
+  'translation.py',
 ]
 
 install_data(portfolio_sources, install_dir: moduledir)
diff --git a/src/places.py b/src/places.py
index 1cbbbe6..2bed48c 100644
--- a/src/places.py
+++ b/src/places.py
@@ -17,12 +17,11 @@
 
 import os
 
-from locale import gettext as _
-
 from gi.repository import GLib, Gio, Gtk, GObject, Handy
 
 from . import logger
 from .place import PortfolioPlace
+from .translation import gettext as _
 
 
 class PortfolioPlaces(Gtk.Stack):
diff --git a/src/translation.py b/src/translation.py
new file mode 100644
index 0000000..51f648b
--- /dev/null
+++ b/src/translation.py
@@ -0,0 +1,21 @@
+import locale
+
+try:
+    from locale import gettext as _
+except ImportError:
+    from gettext import gettext as _
+
+
+def translation_init(localedir):
+    try:
+        locale.bindtextdomain("portfolio", localedir)
+        locale.textdomain("portfolio")
+    except AttributeError:
+        import gettext
+
+        gettext.bindtextdomain("portfolio", localedir)
+        gettext.textdomain("portfolio")
+
+
+def gettext(*args, **kwargs):
+    return _(*args, **kwargs)
diff --git a/src/window.py b/src/window.py
index 5f3ec80..df7ddb7 100644
--- a/src/window.py
+++ b/src/window.py
@@ -18,7 +18,8 @@
 import os
 
 from pathlib import Path
-from locale import gettext as _
+
+from .translation import gettext as _
 
 from gi.repository import Gtk, GLib, Gio, Handy, GObject
 
diff --git a/src/worker.py b/src/worker.py
index 253ec96..3c8e649 100644
--- a/src/worker.py
+++ b/src/worker.py
@@ -21,11 +21,11 @@
 import datetime
 import threading
 
-from locale import gettext as _
 from gi.repository import Gio, GObject, GLib
 
 from . import utils
 from . import logger
+from .translation import gettext as _
 
 
 class WorkerStoppedException(Exception):

From e66cd9125fcccfcf700189684964019e631fe585 Mon Sep 17 00:00:00 2001
From: Martin Abente Lahaye <martin.abente.lahaye@gmail.com>
Date: Fri, 5 Mar 2021 07:23:28 -0300
Subject: [PATCH 2/2] translation: Refactor into a single init function

This will make it easier to trace the different
scenarios, e.g. with logging, etc.
---
 src/dev.tchx84.Portfolio.in |  4 ++--
 src/translation.py          | 44 ++++++++++++++++++++++++++-----------
 2 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/src/dev.tchx84.Portfolio.in b/src/dev.tchx84.Portfolio.in
index b47d863..6719bca 100755
--- a/src/dev.tchx84.Portfolio.in
+++ b/src/dev.tchx84.Portfolio.in
@@ -34,8 +34,8 @@ if __name__ == '__main__':
     resource = Gio.Resource.load(os.path.join(pkgdatadir, 'portfolio.gresource'))
     resource._register()
 
-    from portfolio.translation import translation_init
-    translation_init(localedir)
+    from portfolio import translation
+    translation.init(localedir)
 
     from portfolio import main
     sys.exit(main.main(VERSION))
diff --git a/src/translation.py b/src/translation.py
index 51f648b..10df47c 100644
--- a/src/translation.py
+++ b/src/translation.py
@@ -1,21 +1,39 @@
-import locale
+# translation.py
+#
+# Copyright 2021 Clayton Craft
+# Copyright 2021 Martin Abente Lahaye
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-try:
-    from locale import gettext as _
-except ImportError:
-    from gettext import gettext as _
+from . import logger
 
+gettext = lambda msg: msg
+
+
+def init(localedir):
+    global gettext
 
-def translation_init(localedir):
     try:
+        import locale
+
+        gettext = locale.gettext
         locale.bindtextdomain("portfolio", localedir)
         locale.textdomain("portfolio")
     except AttributeError:
-        import gettext
-
-        gettext.bindtextdomain("portfolio", localedir)
-        gettext.textdomain("portfolio")
-
+        logger.debug("Using fallback gettext module")
+        import gettext as _gettext
 
-def gettext(*args, **kwargs):
-    return _(*args, **kwargs)
+        gettext = _gettext.gettext
+        _gettext.bindtextdomain("portfolio", localedir)
+        _gettext.textdomain("portfolio")