diff --git a/doc/config.rst b/doc/config.rst index 904d0bdc0..5d188c613 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -406,11 +406,20 @@ documentation on :ref:`intl` for details. add the directory :file:`./locale` to this settting, the message catalogs (compiled from ``.po`` format using :program:`msgfmt`) must be in :file:`./locale/{language}/LC_MESSAGES/sphinx.mo`. The text domain of - individual documents depends on their docname if they are top-level project - files and on their base directory otherwise. + individual documents depends on :confval:`gettext_compact`. The default is ``[]``. +.. confval:: gettext_compact + + .. versionadded:: 1.1 + + If true, a document's text domain is its docname if it is a top-level + project file and its very base directory otherwise. + + By default, the document ``markup/code.rst`` ends up in the ``markup`` text + domain. With this option set to ``False``, it is ``markup/code``. + .. _html-options: diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 41a5acbcc..81535f97f 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -16,7 +16,7 @@ from collections import defaultdict from sphinx.builders import Builder from sphinx.util.nodes import extract_messages -from sphinx.util.osutil import SEP, safe_relpath +from sphinx.util.osutil import SEP, safe_relpath, ensuredir, find_catalog from sphinx.util.console import darkgreen POHEADER = ur""" @@ -76,7 +76,7 @@ class I18nBuilder(Builder): return def write_doc(self, docname, doctree): - catalog = self.catalogs[docname.split(SEP, 1)[0]] + catalog = self.catalogs[find_catalog(docname, self.config)] for node, msg in extract_messages(doctree): catalog.add(msg, node) @@ -97,11 +97,14 @@ class MessageCatalogBuilder(I18nBuilder): # XXX should supply tz ctime = datetime.now().strftime('%Y-%m-%d %H:%M%z'), ) - for section, catalog in self.status_iterator( + for textdomain, catalog in self.status_iterator( self.catalogs.iteritems(), "writing message catalogs... ", - lambda (section, _):darkgreen(section), len(self.catalogs)): + lambda (textdomain, _):darkgreen(textdomain), len(self.catalogs)): - pofn = path.join(self.outdir, section + '.pot') + # noop if config.gettext_compact is set + ensuredir(path.join(self.outdir, path.dirname(textdomain))) + + pofn = path.join(self.outdir, textdomain + '.pot') pofile = open(pofn, 'w', encoding='utf-8') try: pofile.write(POHEADER % data) diff --git a/sphinx/config.py b/sphinx/config.py index 2250c57f7..b05054c60 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -176,6 +176,9 @@ class Config(object): linkcheck_ignore = ([], None), linkcheck_timeout = (None, None), linkcheck_workers = (5, None), + + # gettext options + gettext_compact = (True, 'gettext'), ) def __init__(self, dirname, filename, overrides, tags): diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py index b07fa6ab0..e37d23954 100644 --- a/sphinx/util/osutil.py +++ b/sphinx/util/osutil.py @@ -140,3 +140,6 @@ def safe_relpath(path, start=None): return os.path.relpath(path, start) except ValueError: return path + +def find_catalog(docname, config): + return docname.split(SEP, 1)[0] if config.gettext_compact else docname