Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend: Add multi-domain support to compile_catalog command #335

Merged
merged 1 commit into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class compile_catalog(Command):
description = 'compile message catalogs to binary MO files'
user_options = [
('domain=', 'D',
"domain of PO file (default 'messages')"),
"domains of PO files (space separated list, default 'messages')"),
('directory=', 'd',
'path to base directory containing the catalogs'),
('input-file=', 'i',
Expand Down Expand Up @@ -118,6 +118,12 @@ def finalize_options(self):
'or the base directory')

def run(self):
domains = self.domain.split()

for domain in domains:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make for a cleaner patch and cleaner code if you just split def run_domain(self, domain) into a separate fn, I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, good point, will do that

self._run_domain(domain)

def _run_domain(self, domain):
po_files = []
mo_files = []

Expand All @@ -126,27 +132,27 @@ def run(self):
po_files.append((self.locale,
os.path.join(self.directory, self.locale,
'LC_MESSAGES',
self.domain + '.po')))
domain + '.po')))
mo_files.append(os.path.join(self.directory, self.locale,
'LC_MESSAGES',
self.domain + '.mo'))
domain + '.mo'))
else:
for locale in os.listdir(self.directory):
po_file = os.path.join(self.directory, locale,
'LC_MESSAGES', self.domain + '.po')
'LC_MESSAGES', domain + '.po')
if os.path.exists(po_file):
po_files.append((locale, po_file))
mo_files.append(os.path.join(self.directory, locale,
'LC_MESSAGES',
self.domain + '.mo'))
domain + '.mo'))
else:
po_files.append((self.locale, self.input_file))
if self.output_file:
mo_files.append(self.output_file)
else:
mo_files.append(os.path.join(self.directory, self.locale,
'LC_MESSAGES',
self.domain + '.mo'))
domain + '.mo'))

if not po_files:
raise DistutilsOptionError('no message catalogs found')
Expand Down
32 changes: 32 additions & 0 deletions tests/messages/data/project/i18n/de_DE/LC_MESSAGES/bar.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# German (Germany) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: 2007-07-30 22:18+0200\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: de_DE <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9dev-r245\n"

#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr "Stange"

#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] "Fuhstange"
msgstr[1] "Fuhstangen"

32 changes: 32 additions & 0 deletions tests/messages/data/project/i18n/de_DE/LC_MESSAGES/foo.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# German (Germany) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: 2007-07-30 22:18+0200\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: de_DE <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9dev-r245\n"

#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr "Stange"

#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] "Fuhstange"
msgstr[1] "Fuhstangen"

23 changes: 23 additions & 0 deletions tests/messages/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,29 @@ def test_compile_catalog_with_more_than_2_plural_forms(self):
if os.path.isfile(mo_file):
os.unlink(mo_file)

def test_compile_catalog_multidomain(self):
po_foo = os.path.join(self._i18n_dir(), 'de_DE', 'LC_MESSAGES',
'foo.po')
po_bar = os.path.join(self._i18n_dir(), 'de_DE', 'LC_MESSAGES',
'bar.po')
mo_foo = po_foo.replace('.po', '.mo')
mo_bar = po_bar.replace('.po', '.mo')
try:
self.cli.run(sys.argv + ['compile',
'--locale', 'de_DE', '--domain', 'foo bar', '--use-fuzzy',
'-d', self._i18n_dir()])
for mo_file in [mo_foo, mo_bar]:
assert os.path.isfile(mo_file)
self.assertEqual("""\
compiling catalog %r to %r
compiling catalog %r to %r
""" % (po_foo, mo_foo, po_bar, mo_bar), sys.stderr.getvalue())

finally:
for mo_file in [mo_foo, mo_bar]:
if os.path.isfile(mo_file):
os.unlink(mo_file)

def test_update(self):
template = Catalog()
template.add("1")
Expand Down