# HG changeset patch # User cmlenz # Date 1181771201 0 # Node ID abd3a594dab46fe017bd638777e57ff1d9115519 # Parent 57d2f21a1fcca16db780282b59427db1d1017b9d Implement wrapping of header comments in PO(T) output. Related to #14. diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -227,6 +227,13 @@ return num num_plurals = property(num_plurals, doc="""\ The number of plurals used by the locale. + + >>> Catalog(locale='en').num_plurals + 2 + >>> Catalog(locale='cs_CZ').num_plurals + 3 + + :type: `int` """) def plural_forms(self): @@ -240,7 +247,7 @@ plural_forms = property(plural_forms, doc="""\ Return the plural forms declaration for the locale. - >>> Catalog(locale='en_US').plural_forms + >>> Catalog(locale='en').plural_forms 'nplurals=2; plural=(n != 1)' >>> Catalog(locale='pt_BR').plural_forms 'nplurals=2; plural=(n > 1)' diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -550,14 +550,14 @@ 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') @@ -573,19 +573,19 @@ 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 _ 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 - 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, diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -24,7 +24,7 @@ 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 @@ -143,11 +143,10 @@ _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 , YEAR. # """ @@ -296,22 +295,28 @@ 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: @@ -327,6 +332,12 @@ _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')): @@ -370,7 +381,7 @@ ... 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. @@ -414,9 +425,9 @@ _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 , YEAR.' in inlines[index]: if _first_author: outlines.append( diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -68,6 +68,22 @@ "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 , 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)],