Commit 9ea21dd9 by Ned Batchelder

i18n: extract and generate properly handle segments

parent 05856903
......@@ -59,3 +59,15 @@ segment:
mako.po:
mako-studio.po:
- cms/*
# How should the generate step merge files?
generate_merge:
django.po:
- django-partial.po
- django-studio.po
- mako.po
- mako-studio.po
- messages.po
djangojs.po:
- djangojs.po
- djangojs-studio.po
......@@ -14,8 +14,7 @@ LOCALE_DIR = BASE_DIR.joinpath('conf', 'locale')
class Configuration(object):
"""
# Reads localization configuration in json format
Reads localization configuration in json format.
"""
DEFAULTS = {
'generate_merge': {},
......
......@@ -11,7 +11,7 @@ def execute(command, working_directory=BASE_DIR):
Output is ignored.
"""
LOG.info(command)
subprocess.check_call(command, cwd=working_directory, stderr=sys.STDOUT, shell=True)
subprocess.check_call(command, cwd=working_directory, stderr=subprocess.STDOUT, shell=True)
def call(command, working_directory=BASE_DIR):
......
......@@ -21,6 +21,7 @@ from polib import pofile
from i18n.config import BASE_DIR, LOCALE_DIR, CONFIGURATION
from i18n.execute import execute, create_dir_if_necessary, remove_file
from i18n.segment import segment_pofiles
# BABEL_CONFIG contains declarations for Babel to extract strings from mako template files
......@@ -44,7 +45,7 @@ def main():
# Prepare makemessages command.
makemessages = "django-admin.py makemessages -l en"
ignores = " ".join("--ignore={}/*".format(d) for d in CONFIGURATION.ignore_dirs)
ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs)
if ignores:
makemessages += " " + ignores
......@@ -67,6 +68,11 @@ def main():
source_msgs_dir.joinpath('django-partial.po')
)
# Segment the generated files.
segmented_files = segment_pofiles("en")
generated_files.extend(segmented_files)
# Finish each file.
for filename in generated_files:
LOG.info('Cleaning %s' % filename)
po = pofile(source_msgs_dir.joinpath(filename))
......
......@@ -22,10 +22,10 @@ from i18n.execute import execute
LOG = logging.getLogger(__name__)
def merge(locale, target='django.po', fail_if_missing=True):
def merge(locale, target='django.po', sources=('django-partial.po',), fail_if_missing=True):
"""
For the given locale, merge django-partial.po, messages.po, mako.po -> django.po
target is the resulting filename
For the given locale, merge the `sources` files to become the `target`
file. Note that the target file might also be one of the sources.
If fail_if_missing is true, and the files to be merged are missing,
throw an Exception, otherwise return silently.
......@@ -34,18 +34,17 @@ def merge(locale, target='django.po', fail_if_missing=True):
just return silently.
"""
LOG.info('Merging locale={0}'.format(locale))
LOG.info('Merging {target} for locale {locale}'.format(target=target, locale=locale))
locale_directory = CONFIGURATION.get_messages_dir(locale)
files_to_merge = ('django-partial.po', 'messages.po', 'mako.po')
try:
validate_files(locale_directory, files_to_merge)
validate_files(locale_directory, sources)
except Exception, e:
if not fail_if_missing:
return
raise e
# merged file is merged.po
merge_cmd = 'msgcat -o merged.po ' + ' '.join(files_to_merge)
merge_cmd = 'msgcat -o merged.po ' + ' '.join(sources)
execute(merge_cmd, working_directory=locale_directory)
# clean up redunancies in the metadata
......@@ -53,8 +52,16 @@ def merge(locale, target='django.po', fail_if_missing=True):
clean_metadata(merged_filename)
# rename merged.po -> django.po (default)
django_filename = locale_directory.joinpath(target)
os.rename(merged_filename, django_filename) # can't overwrite file on Windows
target_filename = locale_directory.joinpath(target)
os.rename(merged_filename, target_filename)
def merge_files(locale, fail_if_missing=True):
"""
Merge all the files in `locale`, as specified in config.yaml.
"""
for target, sources in CONFIGURATION.generate_merge.items():
merge(locale, target, sources, fail_if_missing)
def clean_metadata(file):
......@@ -85,9 +92,10 @@ def main():
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
for locale in CONFIGURATION.locales:
merge(locale)
merge_files(locale)
# Dummy text is not required. Don't raise exception if files are missing.
merge(CONFIGURATION.dummy_locale, fail_if_missing=False)
merge_files(CONFIGURATION.dummy_locale, fail_if_missing=False)
compile_cmd = 'django-admin.py compilemessages'
execute(compile_cmd, working_directory=BASE_DIR)
......
......@@ -49,7 +49,10 @@ class TestGenerate(TestCase):
self.assertTrue(exists, msg='Missing file in locale %s: %s' % (locale, mofile))
self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path), UTC) >= self.start_time,
msg='File not recently modified: %s' % path)
self.assert_merge_headers(locale)
# Segmenting means that the merge headers don't work they way they
# used to, so don't make this check for now. I'm not sure if we'll
# get the merge header back eventually, or delete this code eventually.
# self.assert_merge_headers(locale)
def assert_merge_headers(self, locale):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment