Skip to content

Commit

Permalink
Merge pull request #362 from OpenDataServices/translations
Browse files Browse the repository at this point in the history
Translate strings that appear in the cove web interface
  • Loading branch information
Bjwebb authored Oct 19, 2020
2 parents 0927d6d + 12a137d commit 2667a7a
Show file tree
Hide file tree
Showing 14 changed files with 737 additions and 109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.egg-info
*.pyc
*.swp
*.mo
.vscode
.cache
.coverage
Expand Down
8 changes: 8 additions & 0 deletions .tx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[main]
host = https://www.transifex.com

[cove-common.flatten-tool]
file_filter = flattentool/locale/<lang>/LC_MESSAGES/flatten-tool.po
source_file = flattentool/locale/en/LC_MESSAGES/flatten-tool.po
source_lang = en
type = PO
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.15.0] - 2020-10-19

### Added

- Add Spanish translation for all strings that could appear in the CoVE web UI https://github.com/OpenDataServices/flatten-tool/pull/362

## [0.14.0] - 2020-09-29

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Extraction from Python source files
[python: **.py]
34 changes: 34 additions & 0 deletions flattentool/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import gettext
from pathlib import Path

domain = "flatten-tool"
locale_dir = str(Path(__file__).parent / "locale")

try:
# If we can get the language from Django use that
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import get_language

try:
get_language()
except ImproperlyConfigured:
raise ImportError

# We set up the translations ourselves, instead of using Django's gettext
# function, so that we can have a custom domain and locale directory.
translations = {}
translations["en"] = gettext.translation(domain, locale_dir, languages=["en"])
translations["es"] = gettext.translation(domain, locale_dir, languages=["es"])

def _(text):
lang = get_language()
if lang not in translations:
lang = "en"
return translations[lang].gettext(text)


except ImportError:
# If there's no Django, call gettext.translation without a languages array,
# and it will pick one based on the environment variables.
t = gettext.translation(domain, locale_dir)
_ = t.gettext
133 changes: 70 additions & 63 deletions flattentool/input.py

Large diffs are not rendered by default.

57 changes: 35 additions & 22 deletions flattentool/json_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import xmltodict

from flattentool.i18n import _
from flattentool.input import path_search
from flattentool.schema import make_sub_sheet_name
from flattentool.sheet import Sheet
Expand Down Expand Up @@ -146,7 +147,7 @@ def __init__(
if isinstance(rollup, (list,)) and (
len(rollup) > 1 or (len(rollup) == 1 and rollup[0] is not True)
):
warn("Using rollUp values from schema, ignoring direct input.")
warn(_("Using rollUp values from schema, ignoring direct input."))
elif isinstance(rollup, (list,)):
if len(rollup) == 1 and os.path.isfile(rollup[0]):
# Parse file, one json path per line.
Expand All @@ -159,13 +160,17 @@ def __init__(
# Rollup args passed directly at the commandline
elif len(rollup) == 1 and rollup[0] is True:
warn(
"No fields to rollup found (pass json path directly, as a list in a file, or via a schema)"
_(
"No fields to rollup found (pass json path directly, as a list in a file, or via a schema)"
)
)
else:
self.rollup = set(rollup)
else:
warn(
"Invalid value passed for rollup (pass json path directly, as a list in a file, or via a schema)"
_(
"Invalid value passed for rollup (pass json path directly, as a list in a file, or via a schema)"
)
)

if self.xml:
Expand All @@ -180,11 +185,13 @@ def __init__(
json_filename = None

if json_filename is None and root_json_dict is None:
raise ValueError("Etiher json_filename or root_json_dict must be supplied")
raise ValueError(
_("Etiher json_filename or root_json_dict must be supplied")
)

if json_filename is not None and root_json_dict is not None:
raise ValueError(
"Only one of json_file or root_json_dict should be supplied"
_("Only one of json_file or root_json_dict should be supplied")
)

if json_filename:
Expand Down Expand Up @@ -222,9 +229,9 @@ def __init__(
if field not in self.schema_parser.flattened.keys():
input_not_in_schema.add(field)
warn(
"You wanted to preserve the following fields which are not present in the supplied schema: {}".format(
list(input_not_in_schema)
)
_(
"You wanted to preserve the following fields which are not present in the supplied schema: {}"
).format(list(input_not_in_schema))
)
except AttributeError:
# no schema
Expand Down Expand Up @@ -260,9 +267,9 @@ def parse(self):
nonexistent_input_paths.append(field)
if len(nonexistent_input_paths) > 0:
warn(
"You wanted to preserve the following fields which are not present in the input data: {}".format(
nonexistent_input_paths
)
_(
"You wanted to preserve the following fields which are not present in the input data: {}"
).format(nonexistent_input_paths)
)

def parse_json_dict(
Expand Down Expand Up @@ -366,7 +373,9 @@ def parse_json_dict(

if self.use_titles and not self.schema_parser:
warn(
"Warning: No schema was provided so column headings are JSON keys, not titles."
_(
"Warning: No schema was provided so column headings are JSON keys, not titles."
)
)

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

if type(v) not in BASIC_TYPES:
raise ValueError(
"Rolled up values must be basic types"
_("Rolled up values must be basic types")
)
else:
if self.schema_parser:
Expand Down Expand Up @@ -458,22 +467,26 @@ def parse_json_dict(
in self.schema_parser.main_sheet
):
warn(
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'.format(
parent_name + key
)
_(
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'
).format(parent_name + key)
)
flattened_dict[
sheet_key(sheet, parent_name + key + "/0/" + k)
] = "WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
] = _(
"WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
)
elif parent_name + key in self.rollup:
warn(
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'.format(
parent_name + key
)
_(
'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'
).format(parent_name + key)
)
flattened_dict[
sheet_key(sheet, parent_name + key + "/0/" + k)
] = "WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
] = _(
"WARNING: More than one value supplied, consult the relevant sub-sheet for the data."
)

if (
self.use_titles
Expand Down Expand Up @@ -502,7 +515,7 @@ def parse_json_dict(
top_level_of_sub_sheet=True,
)
else:
raise ValueError("Unsupported type {}".format(type(value)))
raise ValueError(_("Unsupported type {}").format(type(value)))

if top:
sheet.lines.append(flattened_dict)
Loading

0 comments on commit 2667a7a

Please sign in to comment.