Commits

Georg Brandl committed a999d2b Merge

merge with DasIch/sphinx-i18n

Comments (0)

Files changed (6)

doc/Makefile

File contents unchanged.
 .. versionadded:: 1.1
 
 Complementary to translations provided for Sphinx-generated messages such as
-navigation bars Sphinx provides mechanisms facilitating *document* translations
+navigation bars, Sphinx provides mechanisms facilitating *document* translations
 in itself.  It relies on the existing configuration values :confval:`language`
 and :confval:`locale_dirs`.
 
-
 .. XXX write more!

sphinx/builders/intl.py

 
     The MessageCatalogBuilder class.
 
-    :copyright: Copyright 2010 by the Sphinx team, see AUTHORS.
+    :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
 
-import collections
+from os import path
+from codecs import open
 from datetime import datetime
-from os import path
+from collections import defaultdict
 
 from sphinx.builders import Builder
+from sphinx.builders.versioning import VersioningBuilderMixin
 from sphinx.util.nodes import extract_messages
-from sphinx.util.osutil import SEP
+from sphinx.util.osutil import SEP, copyfile
 from sphinx.util.console import darkgreen
 
 POHEADER = ur"""
 
 """[1:]
 
-class MessageCatalogBuilder(Builder):
+
+class I18nBuilder(Builder, VersioningBuilderMixin):
     """
-    Builds gettext-style message catalogs (.pot files).
+    General i18n builder.
     """
-    name = 'gettext'
+    name = 'i18n'
 
     def init(self):
-        self.catalogs = collections.defaultdict(list)
+        Builder.init(self)
+        VersioningBuilderMixin.init(self)
+        self.catalogs = defaultdict(dict)
 
     def get_target_uri(self, docname, typ=None):
         return ''
         return
 
     def write_doc(self, docname, doctree):
-        """
-        Store a document's translatable strings in the message catalog of its
-        section. For this purpose a document's *top-level directory* -- or
-        otherwise its *name* -- is considered its section.
-        """
         catalog = self.catalogs[docname.split(SEP, 1)[0]]
-        for _, msg in extract_messages(doctree):
-            # XXX msgctxt for duplicate messages
-            if msg not in catalog:
-                catalog.append(msg)
+
+        self.handle_versioning(docname, doctree, nodes.TextElement)
+
+        for node, msg in extract_messages(doctree):
+            catalog.setdefault(node.uid, msg)
 
     def finish(self):
+        Builder.finish(self)
+        VersioningBuilderMixin.finish(self)
+
+
+class MessageCatalogBuilder(I18nBuilder):
+    """
+    Builds gettext-style message catalogs (.pot files).
+    """
+    name = 'gettext'
+
+    def finish(self):
+        I18nBuilder.finish(self)
         data = dict(
             version = self.config.version,
             copyright = self.config.copyright,
                 self.catalogs.iteritems(), "writing message catalogs... ",
                 lambda (section, _):darkgreen(section), len(self.catalogs)):
 
-            pofile = open(path.join(self.outdir, '%s.pot' % section), 'w')
+            pofn = path.join(self.outdir, section + '.pot')
+            pofile = open(pofn, 'w', encoding='utf-8')
             try:
                 pofile.write(POHEADER % data)
-                for message in messages:
+                for uid, message in messages.iteritems():
                     # message contains *one* line of text ready for translation
-                    message = message.replace(u'\\', ur'\\').replace(u'"', ur'\"')
-                    pomsg = u'msgid "%s"\nmsgstr ""\n\n' % message
-                    pofile.write(pomsg.encode('utf-8'))
+                    message = message.replace(u'\\', ur'\\'). \
+                                      replace(u'"', ur'\"')
+                    pomsg = u'#%s\nmsgid "%s"\nmsgstr ""\n\n' % (uid, message)
+                    pofile.write(pomsg)
             finally:
                 pofile.close()

sphinx/environment.py

             # XXX ctx not used
             #ctx = node.parent
             patch = new_document(source, settings)
-            msgstr = catalog.ugettext(msg)
+            msgstr = catalog.gettext(msg)
             # XXX add marker to untranslated parts
             if not msgstr or msgstr == msg: # as-of-yet untranslated
                 continue
             if node['level'] < filterlevel:
                 node.parent.remove(node)
 
+
     def process_dependencies(self, docname, doctree):
         """
         Process docutils-generated dependency info.

sphinx/locale/__init__.py

         translator = gettext.NullTranslations()
         has_translation = False
     translators[catalog] = translator
+    if hasattr(translator, 'ugettext'):
+        translator.gettext = translator.ugettext
     return translator, has_translation

tests/test_build_gettext.py

             if p.returncode != 0:
                 print stdout
                 print stderr
-                assert False, 'msginit exited with return code %s' % p.returncode
+                assert False, 'msginit exited with return code %s' % \
+                        p.returncode
         assert (app.outdir / 'en_US.po').isfile(), 'msginit failed'
         try:
             p = Popen(['msgfmt', 'en_US.po', '-o',
             if p.returncode != 0:
                 print stdout
                 print stderr
-                assert False, 'msgfmt exited with return code %s' % p.returncode
-        assert (app.outdir / 'en' / 'LC_MESSAGES' / 'test_root.mo').isfile(), 'msgfmt failed'
+                assert False, 'msgfmt exited with return code %s' % \
+                        p.returncode
+        assert (app.outdir / 'en' / 'LC_MESSAGES' / 'test_root.mo').isfile(), \
+                'msgfmt failed'
     finally:
         os.chdir(cwd)
 
-    _ = gettext.translation('test_root', app.outdir, languages=['en']).ugettext
+    _ = gettext.translation('test_root', app.outdir, languages=['en']).gettext
     assert _("Testing various markup") == u"Testing various markup"
 
 @with_app(buildername='gettext')
             print stdout
             print stderr
             assert False, 'msgfmt exited with return code %s' % p.returncode
-    assert (test_root / 'xx' / 'LC_MESSAGES' / 'bom.mo').isfile(), 'msgfmt failed'
+    assert (test_root / 'xx' / 'LC_MESSAGES' / 'bom.mo').isfile(), \
+            'msgfmt failed'
+
 def teardown_patch():
     (test_root / 'xx').rmtree()
 test_patch.setup = setup_patch