Skip to content

Commit 2667a7a

Browse files
authored
Merge pull request #362 from OpenDataServices/translations
Translate strings that appear in the cove web interface
2 parents 0927d6d + 12a137d commit 2667a7a

File tree

14 files changed

+737
-109
lines changed

14 files changed

+737
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.egg-info
22
*.pyc
33
*.swp
4+
*.mo
45
.vscode
56
.cache
67
.coverage

.tx/config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[main]
2+
host = https://www.transifex.com
3+
4+
[cove-common.flatten-tool]
5+
file_filter = flattentool/locale/<lang>/LC_MESSAGES/flatten-tool.po
6+
source_file = flattentool/locale/en/LC_MESSAGES/flatten-tool.po
7+
source_lang = en
8+
type = PO

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.15.0] - 2020-10-19
10+
11+
### Added
12+
13+
- Add Spanish translation for all strings that could appear in the CoVE web UI https://github.com/OpenDataServices/flatten-tool/pull/362
14+
915
## [0.14.0] - 2020-09-29
1016

1117
### Fixed

babel.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Extraction from Python source files
2+
[python: **.py]

flattentool/i18n.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import gettext
2+
from pathlib import Path
3+
4+
domain = "flatten-tool"
5+
locale_dir = str(Path(__file__).parent / "locale")
6+
7+
try:
8+
# If we can get the language from Django use that
9+
from django.core.exceptions import ImproperlyConfigured
10+
from django.utils.translation import get_language
11+
12+
try:
13+
get_language()
14+
except ImproperlyConfigured:
15+
raise ImportError
16+
17+
# We set up the translations ourselves, instead of using Django's gettext
18+
# function, so that we can have a custom domain and locale directory.
19+
translations = {}
20+
translations["en"] = gettext.translation(domain, locale_dir, languages=["en"])
21+
translations["es"] = gettext.translation(domain, locale_dir, languages=["es"])
22+
23+
def _(text):
24+
lang = get_language()
25+
if lang not in translations:
26+
lang = "en"
27+
return translations[lang].gettext(text)
28+
29+
30+
except ImportError:
31+
# If there's no Django, call gettext.translation without a languages array,
32+
# and it will pick one based on the environment variables.
33+
t = gettext.translation(domain, locale_dir)
34+
_ = t.gettext

flattentool/input.py

Lines changed: 70 additions & 63 deletions
Large diffs are not rendered by default.

flattentool/json_input.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import xmltodict
1717

18+
from flattentool.i18n import _
1819
from flattentool.input import path_search
1920
from flattentool.schema import make_sub_sheet_name
2021
from flattentool.sheet import Sheet
@@ -146,7 +147,7 @@ def __init__(
146147
if isinstance(rollup, (list,)) and (
147148
len(rollup) > 1 or (len(rollup) == 1 and rollup[0] is not True)
148149
):
149-
warn("Using rollUp values from schema, ignoring direct input.")
150+
warn(_("Using rollUp values from schema, ignoring direct input."))
150151
elif isinstance(rollup, (list,)):
151152
if len(rollup) == 1 and os.path.isfile(rollup[0]):
152153
# Parse file, one json path per line.
@@ -159,13 +160,17 @@ def __init__(
159160
# Rollup args passed directly at the commandline
160161
elif len(rollup) == 1 and rollup[0] is True:
161162
warn(
162-
"No fields to rollup found (pass json path directly, as a list in a file, or via a schema)"
163+
_(
164+
"No fields to rollup found (pass json path directly, as a list in a file, or via a schema)"
165+
)
163166
)
164167
else:
165168
self.rollup = set(rollup)
166169
else:
167170
warn(
168-
"Invalid value passed for rollup (pass json path directly, as a list in a file, or via a schema)"
171+
_(
172+
"Invalid value passed for rollup (pass json path directly, as a list in a file, or via a schema)"
173+
)
169174
)
170175

171176
if self.xml:
@@ -180,11 +185,13 @@ def __init__(
180185
json_filename = None
181186

182187
if json_filename is None and root_json_dict is None:
183-
raise ValueError("Etiher json_filename or root_json_dict must be supplied")
188+
raise ValueError(
189+
_("Etiher json_filename or root_json_dict must be supplied")
190+
)
184191

185192
if json_filename is not None and root_json_dict is not None:
186193
raise ValueError(
187-
"Only one of json_file or root_json_dict should be supplied"
194+
_("Only one of json_file or root_json_dict should be supplied")
188195
)
189196

190197
if json_filename:
@@ -222,9 +229,9 @@ def __init__(
222229
if field not in self.schema_parser.flattened.keys():
223230
input_not_in_schema.add(field)
224231
warn(
225-
"You wanted to preserve the following fields which are not present in the supplied schema: {}".format(
226-
list(input_not_in_schema)
227-
)
232+
_(
233+
"You wanted to preserve the following fields which are not present in the supplied schema: {}"
234+
).format(list(input_not_in_schema))
228235
)
229236
except AttributeError:
230237
# no schema
@@ -260,9 +267,9 @@ def parse(self):
260267
nonexistent_input_paths.append(field)
261268
if len(nonexistent_input_paths) > 0:
262269
warn(
263-
"You wanted to preserve the following fields which are not present in the input data: {}".format(
264-
nonexistent_input_paths
265-
)
270+
_(
271+
"You wanted to preserve the following fields which are not present in the input data: {}"
272+
).format(nonexistent_input_paths)
266273
)
267274

268275
def parse_json_dict(
@@ -366,7 +373,9 @@ def parse_json_dict(
366373

367374
if self.use_titles and not self.schema_parser:
368375
warn(
369-
"Warning: No schema was provided so column headings are JSON keys, not titles."
376+
_(
377+
"Warning: No schema was provided so column headings are JSON keys, not titles."
378+
)
370379
)
371380

372381
if len(value) == 1:
@@ -381,7 +390,7 @@ def parse_json_dict(
381390

382391
if type(v) not in BASIC_TYPES:
383392
raise ValueError(
384-
"Rolled up values must be basic types"
393+
_("Rolled up values must be basic types")
385394
)
386395
else:
387396
if self.schema_parser:
@@ -458,22 +467,26 @@ def parse_json_dict(
458467
in self.schema_parser.main_sheet
459468
):
460469
warn(
461-
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'.format(
462-
parent_name + key
463-
)
470+
_(
471+
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'
472+
).format(parent_name + key)
464473
)
465474
flattened_dict[
466475
sheet_key(sheet, parent_name + key + "/0/" + k)
467-
] = "WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
476+
] = _(
477+
"WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
478+
)
468479
elif parent_name + key in self.rollup:
469480
warn(
470-
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'.format(
471-
parent_name + key
472-
)
481+
_(
482+
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'
483+
).format(parent_name + key)
473484
)
474485
flattened_dict[
475486
sheet_key(sheet, parent_name + key + "/0/" + k)
476-
] = "WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
487+
] = _(
488+
"WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
489+
)
477490

478491
if (
479492
self.use_titles
@@ -502,7 +515,7 @@ def parse_json_dict(
502515
top_level_of_sub_sheet=True,
503516
)
504517
else:
505-
raise ValueError("Unsupported type {}".format(type(value)))
518+
raise ValueError(_("Unsupported type {}").format(type(value)))
506519

507520
if top:
508521
sheet.lines.append(flattened_dict)

0 commit comments

Comments
 (0)