# HG changeset patch # User palgarvio # Date 1181299067 0 # Node ID a258e4015eb6359ab65b97da5ac5fdf5e56b8dc0 # Parent 7dbcbc3f07e02c67fcb31283956f2c12a6d6c38d `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `//.po`, the correct form. Removed the language and coutry arguments of `write_po`, which instead accepts a `babel.core.Locale` object to retrieve the needed object. diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -197,13 +197,15 @@ description = 'create new catalogs based on a catalog template' user_options = [ + ('domain=', 'D', + "domain of PO file (defaults to lower-cased project name"), ('input-file=', 'i', 'name of the input file'), ('output-dir=', 'd', 'path to output directory'), ('output-file=', 'o', "name of the output file (default " - "'/.po')"), + "'//.po')"), ('locale=', 'l', 'locale for the new localized catalog'), ('first-author=', None, @@ -217,69 +219,64 @@ self.output_file = None self.input_file = None self.locale = None + self.domain = None self.first_author = None self.first_author_email = None def finalize_options(self): if not self.input_file: raise DistutilsOptionError('you must specify the input file') + + if not self.domain: + self.domain = self.distribution.get_name().lower() if not self.locale: raise DistutilsOptionError('you must provide a locale for the ' - 'new catalog') + 'new catalog') else: try: - locale = Locale.parse(self.locale) + self._locale = Locale.parse(self.locale) except UnknownLocaleError, error: log.error(error) sys.exit(1) - self._locale_parts = self.locale.split('_') - self._language = None - self._country = None - _locale = Locale('en') - if len(self._locale_parts) == 2: - if self._locale_parts[0] == self._locale_parts[1].lower(): - # Remove country part if equal to language - locale = self._locale_parts[0] - else: - locale = self.locale - self._language = _locale.languages[self._locale_parts[0]] - self._country = _locale.territories[self._locale_parts[1]] - else: - locale = self._locale_parts[0] - self._language = _locale.languages[locale] + if self._locale.territory.lower() == self._locale.language: + # Remove country part if equal to language + # XXX: This might not be the best behaviour, investigate + self.locale = self._locale.language if not self.output_file and not self.output_dir: raise DistutilsOptionError('you must specify the output directory') if not self.output_file and self.output_dir: - self.output_file = os.path.join(self.output_dir, locale + '.po') + self.output_file = os.path.join(self.output_dir, + self.locale, + self.domain + '.po') + if not os.path.exists(os.path.dirname(self.output_file)): + os.makedirs(os.path.dirname(self.output_file)) def run(self): outfile = open(self.output_file, 'w') infile = open(self.input_file, 'r') - if PLURALS.has_key(self.locale): - # Try _ - plurals = PLURALS[self.locale] - elif PLURALS.has_key(self._locale_parts[0]): + if PLURALS.has_key(str(self._locale)): + # Try _ if passed by user + plurals = PLURALS[str(self._locale)] + elif PLURALS.has_key(self._locale.language): # Try - plurals = PLURALS[self._locale_parts[0]] + plurals = PLURALS[self._locale.language] else: plurals = ('INTEGER', 'EXPRESSION') - if self._country: - logline = 'Creating %%s (%s) %%r PO from %%r' % self._country + \ - ' PO template' - else: - logline = 'Creating %s %r PO from %r PO template' - log.info(logline, self._language, self.output_file, self.input_file) + log.info('Creating %s %r PO from %r PO template', + self._locale.english_name, + self.output_file, + self.input_file) - write_po(outfile, infile, self._language, country=self._country, + write_po(outfile, infile, self._locale, project=self.distribution.get_name(), version=self.distribution.get_version(), - charset=self.charset, plurals=plurals, + plurals=plurals, first_author=self.first_author, first_author_email=self.first_author_email) infile.close() @@ -487,4 +484,4 @@ if __name__ == '__main__': - extract_cmdline() + main() diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -344,13 +344,15 @@ _write('msgstr ""\n') _write('\n') -def write_po(fileobj, input_fileobj, language, country=None, project='PROJECT', +def write_po(fileobj, input_fileobj, locale_obj, project='PROJECT', version='VERSION', first_author=None, first_author_email=None, plurals=('INTEGER', 'EXPRESSION')): r"""Write a ``gettext`` PO (portable object) file to the given file-like object, from the given input PO template file. >>> from StringIO import StringIO + >>> from babel import Locale + >>> locale_obj = Locale.parse('pt_PT') >>> inbuf = StringIO(r'''# Translations Template for FooBar. ... # Copyright (C) 2007 ORGANIZATION ... # This file is distributed under the same license as the @@ -380,12 +382,12 @@ ... msgstr "" ... ''') >>> outbuf = StringIO() - >>> write_po(outbuf, inbuf, 'English', project='FooBar', + >>> write_po(outbuf, inbuf, locale_obj, project='FooBar', ... version='0.1', first_author='A Name', ... first_author_email='user@domain.tld', ... plurals=(2, '(n != 1)')) >>> print outbuf.getvalue() # doctest: +ELLIPSIS - # English Translations for FooBar + # Portuguese (Portugal) Translations for FooBar # Copyright (C) 2007 ORGANIZATION # This file is distributed under the same license as the # FooBar project. @@ -428,13 +430,9 @@ in_header = True for index in range(len(inlines)): if in_header: - if '# Translations Template' in inlines[index]: - if country: - line = '# %s (%s) Translations for %%s\n' % \ - (language, country) - else: - line = '# %s Translations for %%s\n' % language - outlines.append(line % 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(