diff --git a/CHANGES b/CHANGES index effaa10a4..01fd01e32 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,10 @@ Release 1.2 (in development) ============================ +* Add i18n capabilities for custom templates. + For example: The Sphinx reference documentation in doc directory provides + sphinx.pot file from ``doc/_templates/*.html`` by ``make gettext``. + * PR#123, #1106: Add epub_use_index configuration value. If provided, it will be used instead of html_use_index for epub builder. diff --git a/doc/_templates/index.html b/doc/_templates/index.html index cf2761545..d1955cac1 100644 --- a/doc/_templates/index.html +++ b/doc/_templates/index.html @@ -1,87 +1,87 @@ {% extends "layout.html" %} -{% set title = 'Overview' %} +{% set title = _('Overview') %} {% block body %} -
+
{%trans%} Sphinx is a tool that makes it easy to create intelligent and beautiful - documentation, written by Georg Brandl and licensed under the BSD license.
-It was originally created for the + documentation, written by Georg Brandl and licensed under the BSD license.{%endtrans%}
+{%trans%}It was originally created for the new Python documentation, and it has excellent facilities for the documentation of Python projects, but C/C++ is already supported as well, and it is planned to add special support for other languages as well. Of course, this site is also created from reStructuredText sources using - Sphinx! The following features should be highlighted: + Sphinx! The following features should be highlighted:{%endtrans%}
+
{%trans%} Sphinx uses reStructuredText as its markup language, and many of its strengths come from the power and straightforwardness of reStructuredText and its parsing and translating - suite, the Docutils. + suite, the Docutils.{%endtrans%}
-
- First steps with Sphinx Contents {%trans%}First steps with Sphinx{%endtrans%} {%trans%}Contents{%endtrans%} |
- Search page General Index {%trans%}Search page{%endtrans%} {%trans%}General Index{%endtrans%} |
+
{%trans%} You can also download PDF versions of the Sphinx documentation: a version generated from the LaTeX Sphinx produces, and a version generated - by rst2pdf. + by rst2pdf.{%endtrans%}
-Links to documentation generated with Sphinx can be found on the - Projects using Sphinx page. +
{%trans path=pathto("examples")%}Links to documentation generated with Sphinx can be found on the + Projects using Sphinx page.{%endtrans%}
-+
{%trans%} For examples of how Sphinx source files look, use the “Show source” links on all pages of the documentation apart from this - welcome page. + welcome page.{%endtrans%}
-You may also be interested in the very nice +
{%trans%}You may also be interested in the very nice tutorial on how to create a customized documentation using Sphinx written by the matplotlib - developers.
+ developers.{%endtrans%} -There is a Japanese translation - of this documentation, thanks to Yoshiki Shibukawa.
+{%trans%}There is a Japanese translation + of this documentation, thanks to Yoshiki Shibukawa.{%endtrans%}
{% endblock %} diff --git a/doc/_templates/indexsidebar.html b/doc/_templates/indexsidebar.html index f9aa2abfe..7805b9422 100644 --- a/doc/_templates/indexsidebar.html +++ b/doc/_templates/indexsidebar.html @@ -1,32 +1,32 @@A
- project
This documentation is for version {{ version }}, which is - not released yet.
-You can use it from the +
{%trans%}This documentation is for version {{ version }}, which is + not released yet.{%endtrans%}
+{%trans%}You can use it from the Mercurial repo or look for released versions in the Python - Package Index.
+ Package Index.{%endtrans%} {% else %} -Current version: {{ version }}
-Get Sphinx from the Python Package -Index, or install it with:
+{%trans%}Current version: {{ version }}{%endtrans%}
+{%trans%}Get Sphinx from the Python Package +Index, or install it with:{%endtrans%}
easy_install -U Sphinx-
Latest development version docs -are also available.
+{%trans%}Latest development version docs +are also available.{%endtrans%}
{% endif %} -Join the Google group:
+{%trans%}Join the Google group:{%endtrans%}
-or come to the #pocoo channel on FreeNode.
-You can also open an issue at the - tracker.
+{%trans%}or come to the #pocoo channel on FreeNode.{%endtrans%}
+{%trans%}You can also open an issue at the + tracker.{%endtrans%}
diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 7a6e1abe6..1cede981f 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -9,16 +9,17 @@ :license: BSD, see LICENSE for details. """ -from os import path +from os import path, walk from codecs import open from datetime import datetime from collections import defaultdict +from uuid import uuid4 from sphinx.builders import Builder from sphinx.util import split_index_msg from sphinx.util.nodes import extract_messages, traverse_translatable_index -from sphinx.util.osutil import safe_relpath, ensuredir, find_catalog -from sphinx.util.console import darkgreen +from sphinx.util.osutil import safe_relpath, ensuredir, find_catalog, SEP +from sphinx.util.console import darkgreen, purple, bold from sphinx.locale import pairindextypes POHEADER = ur""" @@ -57,6 +58,17 @@ class Catalog(object): self.metadata[msg].append((origin.source, origin.line, origin.uid)) +class MsgOrigin(object): + """ + Origin holder for Catalog message origin. + """ + + def __init__(self, source, line): + self.source = source + self.line = line + self.uid = uuid4().hex + + class I18nBuilder(Builder): """ General i18n builder. @@ -101,6 +113,43 @@ class MessageCatalogBuilder(I18nBuilder): """ name = 'gettext' + def init(self): + I18nBuilder.init(self) + self.create_template_bridge() + self.templates.init(self) + + def _collect_templates(self): + template_files = set() + for template_path in self.config.templates_path: + tmpl_abs_path = path.join(self.app.srcdir, template_path) + for dirpath, dirs, files in walk(tmpl_abs_path): + for fn in files: + if fn.endswith('.html'): + filename = path.join(dirpath, fn) + filename = filename.replace(path.sep, SEP) + template_files.add(filename) + return template_files + + def _extract_from_template(self): + files = self._collect_templates() + self.info(bold('building [%s]: ' % self.name), nonl=1) + self.info('targets for %d template files' % len(files)) + + extract_translations = self.templates.environment.extract_translations + + for template in self.status_iterator(files, + 'reading templates... ', purple, len(files)): + #catalog = self.catalogs[template] + catalog = self.catalogs['sphinx'] + context = open(template, 'rt').read() #TODO: encoding + for line, meth, msg in extract_translations(context): + origin = MsgOrigin(template, line) + catalog.add(msg, origin) + + def build(self, docnames, summary=None, method='update'): + self._extract_from_template() + I18nBuilder.build(self, docnames, summary, method) + def finish(self): I18nBuilder.finish(self) data = dict( @@ -136,7 +185,8 @@ class MessageCatalogBuilder(I18nBuilder): # message contains *one* line of text ready for translation message = message.replace(u'\\', ur'\\'). \ - replace(u'"', ur'\"') + replace(u'"', ur'\"'). \ + replace(u'\n', u'\\n"\n"') pofile.write(u'msgid "%s"\nmsgstr ""\n\n' % message) finally: diff --git a/tests/roots/test-intl/_templates/index.html b/tests/roots/test-intl/_templates/index.html new file mode 100644 index 000000000..22bc0e832 --- /dev/null +++ b/tests/roots/test-intl/_templates/index.html @@ -0,0 +1,5 @@ +{% extends "layout.html" %} +{% block body %} +{%trans%}Sphinx {{ version }}{%endtrans%}
+{% endblock %} diff --git a/tests/roots/test-intl/conf.py b/tests/roots/test-intl/conf.py index 457c5056f..59ce714d9 100644 --- a/tests/roots/test-intl/conf.py +++ b/tests/roots/test-intl/conf.py @@ -5,3 +5,6 @@ import sys, os project = 'Sphinx intl