Skip to content

Commit bc0ef3f

Browse files
authored
Merge pull request #7 from level12/babel-2.8-ci-helpers
Babel 2.8 and CI helpers
2 parents e3c4507 + d4a7d2d commit bc0ef3f

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2
1+
version: 2.1
22

33
jobs:
44
build:

morphi/messages/frontend.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def _run_domain(self, domain): # noqa: C901
6262
if not po_files:
6363
raise DistutilsOptionError('no message catalogs found')
6464

65+
catalogs_and_errors = {}
66+
6567
for idx, (locale, po_file) in enumerate(po_files):
6668
json_file = json_files[idx]
6769
with open(po_file, 'rb') as infile:
@@ -84,7 +86,8 @@ def _run_domain(self, domain): # noqa: C901
8486
self.log.info('catalog %s is marked as fuzzy, skipping', po_file)
8587
continue
8688

87-
for message, errors in catalog.check():
89+
catalogs_and_errors[catalog] = catalog_errors = list(catalog.check())
90+
for message, errors in catalog_errors:
8891
for error in errors:
8992
self.log.error(
9093
'error: %s:%d: %s', po_file, message.lineno, error
@@ -98,6 +101,8 @@ def _run_domain(self, domain): # noqa: C901
98101
with open(json_file, 'w') as outfile:
99102
self._write_json(outfile, catalog, use_fuzzy=self.use_fuzzy)
100103

104+
return catalogs_and_errors
105+
101106
@staticmethod
102107
def _write_json(outfile, catalog, use_fuzzy=False):
103108
"""Write `catalog` as JSON text to the specified `outfile`"""

morphi/messages/validation.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import subprocess
2+
import sys
3+
4+
5+
def check_translations(
6+
root_path,
7+
package_name,
8+
locales=frozenset(['es']),
9+
ignored_strings=frozenset(),
10+
):
11+
# extract messages through to catalogs
12+
setup_py = str(root_path / 'setup.py')
13+
subprocess.run(['python', setup_py, 'extract_messages'])
14+
subprocess.run(['python', setup_py, 'update_catalog', '--no-fuzzy-matching'])
15+
subprocess.run(['python', setup_py, 'compile_catalog'])
16+
subprocess.run(['python', setup_py, 'compile_json'])
17+
18+
found_fuzzy = False
19+
untranslated_strings = []
20+
21+
# check for fuzzy matches
22+
for locale in locales:
23+
po_path = (
24+
root_path / package_name / 'i18n' / locale / 'LC_MESSAGES'
25+
/ '{}.po'.format(package_name)
26+
)
27+
with open(po_path, mode='rb') as fp:
28+
contents = fp.read()
29+
found_fuzzy = found_fuzzy or b'#, fuzzy' in contents
30+
31+
fp.seek(0)
32+
from babel.messages.pofile import read_po
33+
load_catalog = read_po(fp)
34+
for message in load_catalog:
35+
if message.id in ignored_strings:
36+
continue
37+
if message.id and not message.string:
38+
untranslated_strings.append('{}: {}'.format(locale, message.id))
39+
40+
# note: the strings below are intentionally left untranslated
41+
if found_fuzzy:
42+
print('Detected fuzzy translations.')
43+
44+
if untranslated_strings:
45+
print('Did not find translations for the following strings:')
46+
for item in untranslated_strings:
47+
print(' ', item)
48+
49+
if found_fuzzy or untranslated_strings:
50+
print('Edit the PO file and compile the catalogs.')
51+
sys.exit(1)
52+
53+
print('No detected translation issues.')

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,34,35,36,37},flake8
2+
envlist = py{36,37,38},flake8
33

44
[testenv]
55
usedevelop = false

0 commit comments

Comments
 (0)