sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.57k stars 2.12k forks source link

gettext_additional_targets not identifying custom directive #11956

Open sachin-suresh-rapyuta opened 9 months ago

sachin-suresh-rapyuta commented 9 months ago

Describe the bug

As per the documentation, gettext_additional_targets should allow you to specify additional targets for translation. But, it is not identifying directives like if-builder and panels.

How to Reproduce

I am using sphinx-panels extension: https://pypi.org/project/sphinx-panels/

index.rst:

Documentation
============

.. if-builder:: html

   .. panels::
      :container: container-lg pb-3
      :column: col-lg-4 col-md-4 col-sm-6 col-xs-12 p-2

      This is a test HTML line

conf.py

# -- Localization configuration
locale_dirs = ['locale/']   # path is example but recommended.
gettext_compact = False     # optional
gettext_additional_targets = ['literal-block', 'panels', 'if-builder']

On running the commands below:

make gettext
sphinx-intl update -p build/gettext -l ja

I don't see an entry in index.po for the line "This is a test HTML line"

index.po

#: ../../index.rst:2
msgid "Documentation"
msgstr ""

Expected output:

index.po

#: ../../index.rst:2
msgid "Documentation"
msgstr ""

#: ../../index.rst:5
msgid "This is a test HTML line"
msgstr ""

Environment Information

Platform:              linux; (Linux-5.15.0-92-generic-x86_64-with-glibc2.29)
Python version:        3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0])
Python implementation: CPython
Sphinx version:        6.2.1
Docutils version:      0.18.1
Jinja2 version:        3.1.2
Pygments version:      2.15.1

Sphinx extensions

extensions = [
    'sphinxcontrib.httpdomain',
    'sphinxcontrib.httpexample',
    'sphinx.ext.doctest',
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    'sphinx.ext.autosectionlabel',
    'linuxdoc.rstFlatTable',
    'sphinx_design',
    'sphinx_panels'
]

Additional context

No response

picnixz commented 9 months ago

Actually, I'm not sure that it's meant to identify your custom targets. AFAICT, it says:

To specify names to enable gettext extracting and translation 
applying for i18n additionally. You can specify below names:

Index:

    index terms
Literal-block:

    literal blocks (:: annotation and code-block directive)
Doctest-block:

    doctest block
Raw:

    raw content
Image:

    image/figure uri

So I think we only support those names. If you want to add your custom blocks, you can make a PR out of it.

sachin-suresh-rapyuta commented 9 months ago

Thanks for the reply. Yes, I am trying to get translation strings for texts under the following directives:

.. if-builder:: html

.. panels::

@picnixz Can you help me with the core file that implements this? So I can try to override by adding my custom direcitves?

picnixz commented 9 months ago

Technically, it is TRANSLATABLE_NODES in sphinx/transforms/__init__.py which could be extended but then you need to check the whole logic of i18n. I'm not entirely sure that you can just extend this mapping with your custom nodes but maybe you can try. However, you need the if-builder and panels directives to produce a node. The additional targets are not only roles/directives but they are nodes. So just adding 'panels' might not work. On the other hand, if those directives wrap their content in a docutils node, then you can add that specific node (this is the case for the 'image' node which is created by the .. image directive).