diff --git a/sphinx/builders/latex/__init__.py b/sphinx/builders/latex/__init__.py index a3bc89161..35382b173 100644 --- a/sphinx/builders/latex/__init__.py +++ b/sphinx/builders/latex/__init__.py @@ -21,6 +21,7 @@ from sphinx.builders.latex.transforms import ( FootnoteDocnameUpdater, LaTeXFootnoteTransform, LiteralBlockTransform, ShowUrlsTransform, DocumentTargetTransform, ) +from sphinx.builders.latex.util import ExtBabel from sphinx.config import ENUM from sphinx.environment import NoUri from sphinx.environment.adapters.asset import ImageAdapter @@ -129,6 +130,7 @@ class LaTeXBuilder(Builder): def init(self): # type: () -> None + self.babel = None # type: ExtBabel self.context = {} # type: Dict[str, Any] self.docnames = [] # type: Iterable[str] self.document_data = [] # type: List[Tuple[str, str, str, str, str, bool]] @@ -136,6 +138,7 @@ class LaTeXBuilder(Builder): texescape.init() self.init_context() + self.init_babel() def get_outdated_docs(self): # type: () -> Union[str, List[str]] @@ -210,6 +213,15 @@ class LaTeXBuilder(Builder): # Show the release label only if release value exists self.context['releasename'] = _('Release') + def init_babel(self): + # type: () -> None + self.babel = ExtBabel(self.config.language, not self.context['babel']) + if self.config.language and not self.babel.is_supported_language(): + # emit warning if specified language is invalid + # (only emitting, nothing changed to processing) + logger.warning(__('no Babel option known for language %r'), + self.config.language) + def write_stylesheet(self): # type: () -> None highlighter = highlighting.PygmentsBridge('latex', self.config.pygments_style) @@ -407,8 +419,16 @@ class LaTeXBuilder(Builder): def write_message_catalog(self): # type: () -> None + context = { + 'addtocaptions': '', + 'figurename': self.config.numfig_format.get('figure', '').split('%s', 1), + } + + if self.context['babel'] or self.context['polyglossia']: + context['addtocaptions'] = r'\addto\captions%s' % self.babel.get_language() + filename = path.join(package_dir, 'templates', 'latex', 'sphinxmessages.sty_t') - copy_asset_file(filename, self.outdir, context={}, renderer=LaTeXRenderer()) + copy_asset_file(filename, self.outdir, context=context, renderer=LaTeXRenderer()) def validate_config_values(app, config): diff --git a/sphinx/templates/latex/sphinxmessages.sty_t b/sphinx/templates/latex/sphinxmessages.sty_t index c5c661bb4..382583e6f 100644 --- a/sphinx/templates/latex/sphinxmessages.sty_t +++ b/sphinx/templates/latex/sphinxmessages.sty_t @@ -11,3 +11,8 @@ \renewcommand{\sphinxsymbolsname}{<%= _('Symbols') | e %>} \renewcommand{\sphinxnumbersname}{<%= _('Numbers') | e %>} \def\pageautorefname{<%= _('page') | e %>} + +<%= addtocaptions %>{\renewcommand{\figurename}{<%= figurename[0].strip() | e %>}} +<%- if figurename[1] %> +\def\fnum@figure{\figurename\thefigure\relax{}<%= figurename[1].strip() | e %>} +<%- endif %> diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index d7d6326c7..2ae6bcbb1 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -203,7 +203,6 @@ def test_numref(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.}}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table}}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Listing}}' in result assert ('\\hyperref[\\detokenize{index:fig1}]' @@ -227,6 +226,11 @@ def test_numref(app, status, warning): assert ('\\hyperref[\\detokenize{foo:foo}]{Sect.\\ref{\\detokenize{foo:foo}} ' '\\nameref{\\detokenize{foo:foo}}}') in result + # sphinxmessages.sty + result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') + print(result) + assert r'\addto\captionsenglish{\renewcommand{\figurename}{Fig.}}' in result + @pytest.mark.sphinx( 'latex', testroot='numfig', @@ -241,7 +245,6 @@ def test_numref_with_prefix1(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Figure:}}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Tab\\_}}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Code-}}' in result assert '\\ref{\\detokenize{index:fig1}}' in result @@ -271,6 +274,11 @@ def test_numref_with_prefix1(app, status, warning): assert ('\\hyperref[\\detokenize{foo:foo}]{Sect.\\ref{\\detokenize{foo:foo}} ' '\\nameref{\\detokenize{foo:foo}}}') in result + # sphinxmessages.sty + result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') + print(result) + assert r'\addto\captionsenglish{\renewcommand{\figurename}{Figure:}}' in result + @pytest.mark.sphinx( 'latex', testroot='numfig', @@ -285,8 +293,6 @@ def test_numref_with_prefix2(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Figure:}}' in result - assert '\\def\\fnum@figure{\\figurename\\thefigure.}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Tab\\_}}' in result assert '\\def\\fnum@table{\\tablename\\thetable:}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Code-}}' in result @@ -311,6 +317,12 @@ def test_numref_with_prefix2(app, status, warning): assert ('\\hyperref[\\detokenize{foo:foo}]{Sect.\\ref{\\detokenize{foo:foo}} ' '\\nameref{\\detokenize{foo:foo}}}') in result + # sphinxmessages.sty + result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') + print(result) + assert r'\addto\captionsenglish{\renewcommand{\figurename}{Figure:}}' in result + assert r'\def\fnum@figure{\figurename\thefigure\relax{}.}' in result + @pytest.mark.sphinx( 'latex', testroot='numfig', @@ -321,7 +333,6 @@ def test_numref_with_language_ja(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert '\\renewcommand{\\figurename}{\u56f3}' in result # 図 assert '\\renewcommand{\\tablename}{\u8868}' in result # 表 assert '\\renewcommand{\\literalblockname}{\u30ea\u30b9\u30c8}' in result # リスト assert ('\\hyperref[\\detokenize{index:fig1}]' @@ -345,6 +356,11 @@ def test_numref_with_language_ja(app, status, warning): assert ('\\hyperref[\\detokenize{foo:foo}]{Sect.\\ref{\\detokenize{foo:foo}} ' '\\nameref{\\detokenize{foo:foo}}}') in result + # sphinxmessages.sty + result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') + print(result) + assert '\n{\\renewcommand{\\figurename}{図}}' in result + @pytest.mark.sphinx('latex', testroot='latex-numfig') def test_latex_obey_numfig_is_false(app, status, warning): @@ -419,7 +435,6 @@ def test_babel_with_no_language_settings(app, status, warning): assert '\\usepackage[Bjarne]{fncychap}' in result assert ('\\addto\\captionsenglish{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff' not in result @@ -427,6 +442,7 @@ def test_babel_with_no_language_settings(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{page}' in result + assert r'\addto\captionsenglish{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -444,7 +460,6 @@ def test_babel_with_language_de(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsngerman{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsngerman{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsngerman{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff{"}' in result @@ -452,6 +467,7 @@ def test_babel_with_language_de(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{Seite}' in result + assert r'\addto\captionsngerman{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -469,7 +485,6 @@ def test_babel_with_language_ru(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsrussian{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsrussian{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsrussian{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff{"}' in result @@ -477,6 +492,7 @@ def test_babel_with_language_ru(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{страница}' in result + assert r'\addto\captionsrussian{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -494,7 +510,6 @@ def test_babel_with_language_tr(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsturkish{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsturkish{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsturkish{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff{=}' in result @@ -502,6 +517,7 @@ def test_babel_with_language_tr(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{sayfa}' in result + assert r'\addto\captionsturkish{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -518,7 +534,6 @@ def test_babel_with_language_ja(app, status, warning): assert '\\usepackage{times}' in result assert '\\usepackage[Sonny]{fncychap}' not in result assert '\\renewcommand{\\contentsname}{Table of content}\n' in result - assert '\\renewcommand{\\figurename}{Fig.}\n' in result assert '\\renewcommand{\\tablename}{Table.}\n' in result assert '\\shorthandoff' not in result @@ -526,6 +541,7 @@ def test_babel_with_language_ja(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{ページ}' in result + assert '\n{\\renewcommand{\\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -543,7 +559,6 @@ def test_babel_with_unknown_language(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsenglish{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff' in result @@ -553,6 +568,7 @@ def test_babel_with_unknown_language(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{page}' in result + assert r'\addto\captionsenglish{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -571,7 +587,6 @@ def test_polyglossia_with_language_de(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsgerman{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsgerman{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsgerman{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff' not in result @@ -579,6 +594,7 @@ def test_polyglossia_with_language_de(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{Seite}' in result + assert r'\addto\captionsgerman{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx( @@ -597,7 +613,6 @@ def test_polyglossia_with_language_de_1901(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsgerman{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsgerman{\\renewcommand{\\figurename}{Fig.}}\n' in result assert '\\addto\\captionsgerman{\\renewcommand{\\tablename}{Table.}}\n' in result assert '\\shorthandoff' not in result @@ -605,6 +620,7 @@ def test_polyglossia_with_language_de_1901(app, status, warning): result = (app.outdir / 'sphinxmessages.sty').text(encoding='utf8') print(result) assert r'\def\pageautorefname{page}' in result + assert r'\addto\captionsgerman{\renewcommand{\figurename}{Fig.}}' in result @pytest.mark.sphinx('latex') diff --git a/tests/test_markup.py b/tests/test_markup.py index 1dce09558..218dc0c83 100644 --- a/tests/test_markup.py +++ b/tests/test_markup.py @@ -91,6 +91,7 @@ def verify_re_latex(app, parse): app.builder = LaTeXBuilder(app) app.builder.set_environment(app.env) app.builder.init_context() + app.builder.init_babel() latex_translator = ForgivingLaTeXTranslator(document, app.builder) latex_translator.first_document = -1 # don't write \begin{document} document.walkabout(latex_translator)