Skip to content

Commit

Permalink
Merge pull request #8026 from tk0miya/7768_docpath_for_figure_languag…
Browse files Browse the repository at this point in the history
…e_filename

Close #7768: i18n: figure_language_filename supports {docpath} token
  • Loading branch information
tk0miya committed Aug 2, 2020
2 parents 4f56fad + af15593 commit 0cfb433
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -33,6 +33,7 @@ Features added
* #7902: html theme: Add a new option :confval:`globaltoc_maxdepth` to control
the behavior of globaltoc in sidebar
* #7840: i18n: Optimize the dependencies check on bootstrap
* #7768: i18n: :confval:`figure_language_filename` supports ``docpath`` token
* #5208: linkcheck: Support checks for local links
* #5090: setuptools: Link verbosity to distutils' -v and -q option
* #7052: add ``:noindexentry:`` to the Python, C, C++, and Javascript domains.
Expand Down
5 changes: 5 additions & 0 deletions doc/usage/configuration.rst
Expand Up @@ -821,6 +821,8 @@ documentation on :ref:`intl` for details.
extension, e.g. ``dirname/filename``
* ``{path}`` - the directory path component of the filename, with a trailing
slash if non-empty, e.g. ``dirname/``
* ``{docpath}`` - the directory path component for the current document, with
a trailing slash if non-empty.
* ``{basename}`` - the filename without the directory path or file extension
components, e.g. ``filename``
* ``{ext}`` - the file extension, e.g. ``.png``
Expand All @@ -834,6 +836,9 @@ documentation on :ref:`intl` for details.
.. versionchanged:: 1.5
Added ``{path}`` and ``{basename}`` tokens.

.. versionchanged:: 3.2
Added ``{docpath}`` token.


.. _math-options:

Expand Down
4 changes: 4 additions & 0 deletions sphinx/util/i18n.py
Expand Up @@ -306,8 +306,12 @@ def get_image_filename_for_language(filename: str, env: "BuildEnvironment") -> s
dirname = path.dirname(d['root'])
if dirname and not dirname.endswith(path.sep):
dirname += path.sep
docpath = path.dirname(env.docname)
if docpath and not docpath.endswith(path.sep):
docpath += path.sep
d['path'] = dirname
d['basename'] = path.basename(d['root'])
d['docpath'] = docpath
d['language'] = env.config.language
try:
return filename_format.format(**d)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_util_i18n.py
Expand Up @@ -90,6 +90,8 @@ def test_format_date():

@pytest.mark.xfail(os.name != 'posix', reason="Path separators don't match on windows")
def test_get_filename_for_language(app):
app.env.temp_data['docname'] = 'index'

# language is None
app.env.config.language = None
assert app.env.config.language is None
Expand Down Expand Up @@ -145,6 +147,17 @@ def test_get_filename_for_language(app):
with pytest.raises(SphinxError):
i18n.get_image_filename_for_language('foo.png', app.env)

# docpath (for a document in the top of source directory)
app.env.config.language = 'en'
app.env.config.figure_language_filename = '/{docpath}{language}/{basename}{ext}'
assert (i18n.get_image_filename_for_language('foo.png', app.env) ==
'/en/foo.png')

# docpath (for a document in the sub directory)
app.env.temp_data['docname'] = 'subdir/index'
assert (i18n.get_image_filename_for_language('foo.png', app.env) ==
'/subdir/en/foo.png')


def test_CatalogRepository(tempdir):
(tempdir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs()
Expand Down

0 comments on commit 0cfb433

Please sign in to comment.