parser.error('you must provide a locale for the new catalog')
else:
try:
- _locale = Locale.parse(options.locale)
+ locale = Locale.parse(options.locale)
except UnknownLocaleError, error:
parser.error(error)
- if _locale.territory.lower() == _locale.language:
+ if locale.language.upper() == locale.territory:
# Remove country part if equal to language
# XXX: This might not be the best behaviour, investigate
- options.locale = _locale.language
+ options.locale = locale.language
if not options.output_file and not options.output_dir:
parser.error('you must specify the output directory')
outfile = open(options.output_file, 'w')
infile = open(options.input_file, 'r')
- if PLURALS.has_key(str(_locale)):
+ if PLURALS.has_key(str(locale)):
# Try <language>_<COUNTRY> if passed by user
- plurals = PLURALS[str(_locale)]
- elif PLURALS.has_key(_locale.language):
+ plurals = PLURALS[str(locale)]
+ elif PLURALS.has_key(locale.language):
# Try <language>
- plurals = PLURALS[_locale.language]
+ plurals = PLURALS[locale.language]
else:
plurals = ('INTEGER', 'EXPRESSION')
print 'creating catalog %r based on %r' % (options.output_file,
options.input_file)
- write_po(outfile, infile, _locale,
+ write_po(outfile, infile, locale,
project=options.project_name,
version=options.project_version,
plurals=plurals,
set
except NameError:
from sets import Set as set
-import textwrap
+from textwrap import wrap
from babel import __version__ as VERSION
from babel.messages.catalog import Catalog
_add_message()
return catalog
-POT_HEADER = """\
+POT_HEADER = u"""\
# Translations template for %(project)s.
# Copyright (C) %(year)s %(copyright_holder)s
-# This file is distributed under the same license as the
-# %(project)s project.
+# This file is distributed under the same license as the %(project)s project.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
"""
if not message.id: # This is the header "message"
if omit_header:
continue
- _write(POT_HEADER % {
+ pot_header = POT_HEADER % {
'year': date.today().strftime('%Y'),
'project': catalog.project,
'copyright_holder': _copyright_holder,
- })
+ }
+ if width and width > 0:
+ lines = []
+ for line in pot_header.splitlines():
+ lines += wrap(line, width=width, subsequent_indent='# ',
+ break_long_words=False)
+ pot_header = u'\n'.join(lines) + u'\n'
+ _write(pot_header)
if message.comments:
for comment in message.comments:
- for line in textwrap.wrap(comment,
- width, break_long_words=False):
+ for line in wrap(comment, width, break_long_words=False):
_write('#. %s\n' % line.strip())
if not no_location:
locs = u' '.join([u'%s:%d' % item for item in message.locations])
if width and width > 0:
- locs = textwrap.wrap(locs, width, break_long_words=False)
+ locs = wrap(locs, width, break_long_words=False)
for line in locs:
_write('#: %s\n' % line.strip())
if message.flags:
_write('msgstr %s\n' % _normalize(message.string or ''))
_write('\n')
+
+# TODO: this should really be a combination of read_po() and write_pot(), the
+# latter then being renamed back to write_po(). The problem at this time
+# is that the Catalog class doesn't know anything about the header
+# comment header
+
def write_po(fileobj, input_fileobj, locale_obj, project='PROJECT',
version='VERSION', first_author=None, first_author_email=None,
plurals=('INTEGER', 'EXPRESSION')):
... first_author_email='user@domain.tld',
... plurals=(2, '(n != 1)'))
>>> print outbuf.getvalue() # doctest: +ELLIPSIS
- # Portuguese (Portugal) Translations for FooBar
+ # Portuguese (Portugal) translations for FooBar
# Copyright (C) 2007 ORGANIZATION
# This file is distributed under the same license as the
# FooBar project.
_date = datetime.now(LOCALTZ)
for index in range(len(inlines)):
if in_header:
- if '# Translations template' in inlines[index]:
- outlines.append('# %s Translations for %s\n' % \
- (locale_obj.english_name, project))
+ if '# Translations template' in inlines[index]:
+ outlines.append('# %s translations for %s\n' % \
+ (locale_obj.english_name, project))
elif '# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.' in inlines[index]:
if _first_author:
outlines.append(
"loop\n"
msgstr ""''', buf.getvalue().strip())
+ def test_wrap_long_lines_in_header(self):
+ """
+ Verify that long lines in the header comment are wrapped correctly.
+ """
+ catalog = Catalog(project='AReallyReallyLongNameForAProject')
+ buf = StringIO()
+ pofile.write_pot(buf, catalog)
+ self.assertEqual('''\
+# Translations template for AReallyReallyLongNameForAProject.
+# Copyright (C) 2007 ORGANIZATION
+# This file is distributed under the same license as the
+# AReallyReallyLongNameForAProject project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy''', '\n'.join(buf.getvalue().splitlines()[:7]))
+
def test_pot_with_translator_comments(self):
catalog = Catalog()
catalog.add(u'foo', locations=[('main.py', 1)],