* Added 'N_' (gettext noop) to the extractor's default keywords.
* Made locale string parsing more robust, and also take the script part into
account (ticket #27).
+ * Added a function to list all locales for which locale data is available.
+ * Added a command-line option to the `babel` command which prints out all
+ available locales (ticket #24).
Version 0.8
return self.__data
_data = property(_data)
- def display_name(self):
- retval = self.languages.get(self.language)
- if self.territory:
- variant = ''
+ def get_display_name(self, locale=None):
+ """Return the display name of the locale using the given locale.
+
+ The display name will include the language, territory, script, and
+ variant, if those are specified.
+
+ >>> Locale('zh', 'CN', script='Hans').get_display_name('en')
+ u'Chinese (Simplified Han, China)'
+
+ :param locale: the locale to use
+ :return: the display name
+ """
+ if locale is None:
+ locale = self
+ locale = Locale.parse(locale)
+ retval = locale.languages.get(self.language)
+ if self.territory or self.script or self.variant:
+ details = []
+ if self.script:
+ details.append(locale.scripts.get(self.script))
+ if self.territory:
+ details.append(locale.territories.get(self.territory))
if self.variant:
- variant = ', %s' % self.variants.get(self.variant)
- retval += ' (%s%s)' % (self.territories.get(self.territory),
- variant)
+ details.append(locale.variants.get(self.variant))
+ details = filter(None, details)
+ if details:
+ retval += ' (%s)' % u', '.join(details)
return retval
- display_name = property(display_name, doc="""\
+
+ display_name = property(get_display_name, doc="""\
The localized display name of the locale.
>>> Locale('en').display_name
""")
def english_name(self):
- en = Locale('en')
- retval = en.languages.get(self.language)
- if self.territory:
- variant = ''
- if self.variant:
- variant = ', %s' % en.variants.get(self.variant)
- retval += ' (%s%s)' % (en.territories.get(self.territory),
- variant)
- return retval
+ return self.get_display_name(Locale('en'))
english_name = property(english_name, doc="""\
The english display name of the locale.
return True
return os.path.exists(os.path.join(_dirname, '%s.dat' % name))
+def list():
+ """Return a list of all locale identifiers for which locale data is
+ available.
+
+ :return: a list of locale identifiers (strings)
+ :rtype: `list`
+ :since: version 0.8.1
+ """
+ return [stem for stem, extension in [
+ os.path.splitext(filename) for filename in os.listdir(_dirname)
+ ] if extension == '.dat' and stem != 'root']
+
def load(name):
"""Load the locale data for the given locale.
import sys
from babel import __version__ as VERSION
-from babel import Locale
+from babel import Locale, localedata
from babel.core import UnknownLocaleError
from babel.messages.catalog import Catalog
from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \
:param argv: list of arguments passed on the command-line
"""
self.parser = OptionParser(usage=self.usage % ('command', '[args]'),
- version=self.version)
+ version=self.version)
self.parser.disable_interspersed_args()
self.parser.print_help = self._help
+ self.parser.add_option('--list-locales', dest='list_locales',
+ action='store_true',
+ help="print all known locales and exit")
+ self.parser.set_defaults(list_locales=False)
+
options, args = self.parser.parse_args(argv[1:])
+
+ if options.list_locales:
+ identifiers = localedata.list()
+ longest = max([len(identifier) for identifier in identifiers])
+ format = '%%-%ds %%s' % (longest + 1)
+ for identifier in localedata.list():
+ locale = Locale.parse(identifier)
+ print format % (identifier, locale.english_name)
+ return 0
+
if not args:
self.parser.error('incorrect number of arguments')
if cmdname not in self.commands:
self.parser.error('unknown command "%s"' % cmdname)
- getattr(self, cmdname)(args[1:])
+ return getattr(self, cmdname)(args[1:])
def _help(self):
print self.parser.format_help()
print "commands:"
longest = max([len(command) for command in self.commands])
- format = " %%-%ds %%s" % max(11, longest)
+ format = " %%-%ds %%s" % max(8, longest + 1)
commands = self.commands.items()
commands.sort()
for name, description in commands:
def main():
- CommandLineInterface().run(sys.argv)
+ return CommandLineInterface().run(sys.argv)
def parse_mapping(fileobj, filename=None):
"""Parse an extraction method mapping from a file-like object.
usage: babel command [options] [args]
options:
- --version show program's version number and exit
- -h, --help show this help message and exit
+ --version show program's version number and exit
+ -h, --help show this help message and exit
+ --list-locales print all known locales and exit
commands:
- compile compile message catalogs to mo files
- extract extract messages from source files and generate a pot file
- init create new message catalogs from a pot file
- update update existing message catalogs from a pot file
+ compile compile message catalogs to mo files
+ extract extract messages from source files and generate a pot file
+ init create new message catalogs from a pot file
+ update update existing message catalogs from a pot file
""", sys.stdout.getvalue().lower())
def test_extract_with_default_mapping(self):