Supports i18n console output

This commit is contained in:
Takeshi KOMIYA 2018-02-24 22:30:30 +09:00
parent 1c4a38c700
commit ed403ad8ff
8 changed files with 34 additions and 12 deletions

View File

@ -40,6 +40,7 @@ Features added
* :confval:`source_suffix` allows a mapping fileext to file types * :confval:`source_suffix` allows a mapping fileext to file types
* Add :confval:`author` as a configuration value * Add :confval:`author` as a configuration value
* #2852: imgconverter: Support to convert GIF to PNG * #2852: imgconverter: Support to convert GIF to PNG
* ``sphinx-build`` command supports i18n console output
Bugs fixed Bugs fixed
---------- ----------

View File

@ -1124,6 +1124,7 @@ class Sphinx(object):
.. versionadded:: 1.8 .. versionadded:: 1.8
""" """
locale.init([locale_dir], self.config.language, catalog) locale.init([locale_dir], self.config.language, catalog)
locale.init_console(locale_dir, catalog)
# ---- other methods ------------------------------------------------- # ---- other methods -------------------------------------------------
def is_parallel_allowed(self, typ): def is_parallel_allowed(self, typ):

View File

@ -11,6 +11,7 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import locale
import multiprocessing import multiprocessing
import os import os
import sys import sys
@ -20,7 +21,8 @@ from typing import TYPE_CHECKING
from docutils.utils import SystemMessage from docutils.utils import SystemMessage
from six import text_type, binary_type from six import text_type, binary_type
from sphinx import __display_version__ import sphinx.locale
from sphinx import __display_version__, package_dir
from sphinx.application import Sphinx from sphinx.application import Sphinx
from sphinx.errors import SphinxError from sphinx.errors import SphinxError
from sphinx.util import Tee, format_exception_cut_frames, save_traceback from sphinx.util import Tee, format_exception_cut_frames, save_traceback
@ -304,6 +306,9 @@ def build_main(argv=sys.argv[1:]): # type: ignore
def main(argv=sys.argv[1:]): # type: ignore def main(argv=sys.argv[1:]): # type: ignore
# type: (List[unicode]) -> int # type: (List[unicode]) -> int
locale.setlocale(locale.LC_ALL, '')
sphinx.locale.init_console(os.path.join(package_dir, 'locale'), 'sphinx')
if sys.argv[1:2] == ['-M']: if sys.argv[1:2] == ['-M']:
return make_main(argv) return make_main(argv)
else: else:

View File

@ -12,6 +12,7 @@ from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import locale
import os import os
import re import re
import sys import sys
@ -36,6 +37,7 @@ from six import PY2, PY3, text_type, binary_type
from six.moves import input from six.moves import input
from six.moves.urllib.parse import quote as urlquote from six.moves.urllib.parse import quote as urlquote
import sphinx.locale
from sphinx import __display_version__, package_dir from sphinx import __display_version__, package_dir
from sphinx.util import texescape from sphinx.util import texescape
from sphinx.util.console import ( # type: ignore from sphinx.util.console import ( # type: ignore
@ -598,6 +600,9 @@ Makefile to be used with sphinx-build.
def main(argv=sys.argv[1:]): def main(argv=sys.argv[1:]):
# type: (List[str]) -> int # type: (List[str]) -> int
locale.setlocale(locale.LC_ALL, '')
sphinx.locale.init_console(os.path.join(package_dir, 'locale'), 'sphinx')
if not color_terminal(): if not color_terminal():
nocolor() nocolor()

View File

@ -8,6 +8,7 @@
:copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS. :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
import sys import sys

View File

@ -19,6 +19,7 @@ from __future__ import print_function
import argparse import argparse
import glob import glob
import locale
import os import os
import sys import sys
from fnmatch import fnmatch from fnmatch import fnmatch
@ -27,7 +28,8 @@ from typing import TYPE_CHECKING
from six import binary_type from six import binary_type
from sphinx import __display_version__ import sphinx.locale
from sphinx import __display_version__, package_dir
from sphinx.cmd.quickstart import EXTENSIONS from sphinx.cmd.quickstart import EXTENSIONS
from sphinx.util import rst from sphinx.util import rst
from sphinx.util.osutil import FileAvoidWrite, ensuredir, walk from sphinx.util.osutil import FileAvoidWrite, ensuredir, walk
@ -382,6 +384,9 @@ Note: By default this script will not overwrite already created files.""")
def main(argv=sys.argv[1:]): def main(argv=sys.argv[1:]):
# type: (List[str]) -> int # type: (List[str]) -> int
"""Parse and check the command line arguments.""" """Parse and check the command line arguments."""
locale.setlocale(locale.LC_ALL, '')
sphinx.locale.init_console(os.path.join(package_dir, 'locale'), 'sphinx')
parser = get_parser() parser = get_parser()
args = parser.parse_args(argv) args = parser.parse_args(argv)

View File

@ -21,6 +21,7 @@ from __future__ import print_function
import argparse import argparse
import codecs import codecs
import locale
import os import os
import pydoc import pydoc
import re import re
@ -30,6 +31,7 @@ from typing import TYPE_CHECKING
from jinja2 import FileSystemLoader, TemplateNotFound from jinja2 import FileSystemLoader, TemplateNotFound
from jinja2.sandbox import SandboxedEnvironment from jinja2.sandbox import SandboxedEnvironment
import sphinx.locale
from sphinx import __display_version__ from sphinx import __display_version__
from sphinx import package_dir from sphinx import package_dir
from sphinx.ext.autosummary import import_by_name, get_documenter from sphinx.ext.autosummary import import_by_name, get_documenter
@ -402,6 +404,9 @@ The format of the autosummary directive is documented in the
def main(argv=sys.argv[1:]): def main(argv=sys.argv[1:]):
# type: (List[str]) -> None # type: (List[str]) -> None
locale.setlocale(locale.LC_ALL, '')
sphinx.locale.init_console(os.path.join(package_dir, 'locale'), 'sphinx')
app = DummyApplication() app = DummyApplication()
setup_documenters(app) setup_documenters(app)
args = get_parser().parse_args(argv) args = get_parser().parse_args(argv)

View File

@ -10,6 +10,7 @@
""" """
import gettext import gettext
import locale
import warnings import warnings
from collections import defaultdict from collections import defaultdict
from gettext import NullTranslations from gettext import NullTranslations
@ -203,16 +204,6 @@ def lazy_gettext(string):
translators = defaultdict(NullTranslations) # type: Dict[Tuple[unicode, unicode], NullTranslations] # NOQA translators = defaultdict(NullTranslations) # type: Dict[Tuple[unicode, unicode], NullTranslations] # NOQA
def __(message, *args):
# type: (unicode, *Any) -> unicode
"""A dummy wrapper to i18n'ize exceptions and command line messages.
In future, the messages are translated using LC_MESSAGES or any other
locale settings.
"""
return message if len(args) <= 1 else args[0]
def init(locale_dirs, language, catalog='sphinx', namespace='general'): def init(locale_dirs, language, catalog='sphinx', namespace='general'):
# type: (List, unicode, unicode) -> Tuple[Any, bool] # type: (List, unicode, unicode) -> Tuple[Any, bool]
"""Look for message catalogs in `locale_dirs` and *ensure* that there is at """Look for message catalogs in `locale_dirs` and *ensure* that there is at
@ -256,6 +247,13 @@ def init(locale_dirs, language, catalog='sphinx', namespace='general'):
return translator, has_translation return translator, has_translation
def init_console(locale_dir, catalog):
# type: (unicode, unicode) -> None
"""Initialize locale for console."""
language, _ = locale.getlocale(locale.LC_MESSAGES) # encoding is ignored
return init([locale_dir], language, catalog, 'console')
def get_translator(catalog='sphinx', namespace='general'): def get_translator(catalog='sphinx', namespace='general'):
# type: (unicode, unicode) -> NullTranslations # type: (unicode, unicode) -> NullTranslations
return translators[(namespace, catalog)] return translators[(namespace, catalog)]
@ -313,6 +311,7 @@ def get_translation(catalog, namespace='general'):
# A shortcut for sphinx-core # A shortcut for sphinx-core
_ = get_translation('sphinx') _ = get_translation('sphinx')
__ = get_translation('sphinx', 'console')
def l_(*args): def l_(*args):