diff --git a/conf/locale/config.yaml b/conf/locale/config.yaml index aeb281103f..d031a5cb91 100644 --- a/conf/locale/config.yaml +++ b/conf/locale/config.yaml @@ -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 diff --git a/i18n/config.py b/i18n/config.py index 79076d35b3..3828578b5b 100644 --- a/i18n/config.py +++ b/i18n/config.py @@ -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': {}, diff --git a/i18n/execute.py b/i18n/execute.py index 43bdec2deb..e386ea5c87 100644 --- a/i18n/execute.py +++ b/i18n/execute.py @@ -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): diff --git a/i18n/extract.py b/i18n/extract.py index 2bada77aab..209ee3413d 100755 --- a/i18n/extract.py +++ b/i18n/extract.py @@ -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)) diff --git a/i18n/generate.py b/i18n/generate.py index a851d9217e..97293c0d35 100755 --- a/i18n/generate.py +++ b/i18n/generate.py @@ -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) diff --git a/i18n/tests/test_generate.py b/i18n/tests/test_generate.py index 8dcc5edcb8..7e81ee2073 100644 --- a/i18n/tests/test_generate.py +++ b/i18n/tests/test_generate.py @@ -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): """