]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Add a command-line option that prints out all available locales. Closes #24.
authorChristopher Lenz <cmlenz@gmail.com>
Thu, 28 Jun 2007 16:34:40 +0000 (16:34 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Thu, 28 Jun 2007 16:34:40 +0000 (16:34 +0000)
ChangeLog
babel/core.py
babel/localedata.py
babel/messages/frontend.py
babel/messages/tests/frontend.py

index a65ba412b9bf8bb0958f8270b9723d9bd512de4c..afa09be0e6b450c167e332ea72f5a3f1a77a4c17 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -28,6 +28,9 @@ http://svn.edgewall.org/repos/babel/tags/0.8.1/
  * 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
index a74f7542d33db5dec399dc5de0dda4388b4912a2..f0cc0d1b748e9df6d71fa0888a667ff1c3187eee 100644 (file)
@@ -181,16 +181,36 @@ class Locale(object):
         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
@@ -204,15 +224,7 @@ class Locale(object):
         """)
 
     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.
         
index 1e28da00b803a157ea15b194b6ce6adeae7d330e..b32690c92769e3c252a311cc18a2754f9e3b3b85 100644 (file)
@@ -42,6 +42,18 @@ def exists(name):
         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.
     
index 498909f6292cca5379369de29ba6fd87acab5d67..732ec014f0de8f3cb7f20e8cb82670ff7af1bf01 100755 (executable)
@@ -26,7 +26,7 @@ from StringIO import StringIO
 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, \
@@ -531,10 +531,25 @@ class CommandLineInterface(object):
         :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')
 
@@ -542,13 +557,13 @@ class CommandLineInterface(object):
         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:
@@ -889,7 +904,7 @@ class CommandLineInterface(object):
 
 
 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.
index 01c834ac580860d6c1b3fbe91b35303f3a62bf2e..c63a5a1edb98722e1ec6ce899d11698a3db87065 100644 (file)
@@ -462,14 +462,15 @@ babel: error: incorrect number of arguments
 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):