diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py index c5a06d0..0a77d3b 100644 --- a/src/mkdocstrings_handlers/python/handler.py +++ b/src/mkdocstrings_handlers/python/handler.py @@ -180,6 +180,7 @@ def __init__( *args: Any, config_file_path: str | None = None, paths: list[str] | None = None, + locale: str = "en", **kwargs: Any, ) -> None: """Initialize the handler. @@ -188,6 +189,7 @@ def __init__( *args: Handler name, theme and custom templates. config_file_path: The MkDocs configuration file path. paths: A list of paths to use as Griffe search paths. + locale: The locale to use when rendering content. **kwargs: Same thing, but with keyword arguments. """ super().__init__(*args, **kwargs) @@ -208,6 +210,7 @@ def __init__( self._paths = search_paths self._modules_collection: ModulesCollection = ModulesCollection() self._lines_collection: LinesCollection = LinesCollection() + self._locale = locale @classmethod def load_inventory( @@ -321,7 +324,13 @@ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa final_config["signature_crossrefs"] = False return template.render( - **{"config": final_config, data.kind.value: data, "heading_level": heading_level, "root": True}, + **{ + "config": final_config, + data.kind.value: data, + "heading_level": heading_level, + "root": True, + "locale": self._locale, + }, ) def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore missing docstring) @@ -337,6 +346,7 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore self.env.filters["filter_objects"] = rendering.do_filter_objects self.env.filters["stash_crossref"] = lambda ref, length: ref self.env.filters["get_template"] = rendering.do_get_template + self.env.tests["existing_template"] = lambda template_name: template_name in self.env.list_templates() def get_anchors(self, data: CollectorItem) -> set[str]: # noqa: D102 (ignore missing docstring) try: @@ -350,6 +360,7 @@ def get_handler( custom_templates: str | None = None, config_file_path: str | None = None, paths: list[str] | None = None, + locale: str = "en", **config: Any, # noqa: ARG001 ) -> PythonHandler: """Simply return an instance of `PythonHandler`. @@ -359,6 +370,7 @@ def get_handler( custom_templates: Directory containing custom templates. config_file_path: The MkDocs configuration file path. paths: A list of paths to use as Griffe search paths. + locale: The locale to use when rendering content. **config: Configuration passed to the handler. Returns: @@ -370,4 +382,5 @@ def get_handler( custom_templates=custom_templates, config_file_path=config_file_path, paths=paths, + locale=locale, ) diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/attributes.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/attributes.html index 20487f2..296f155 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/attributes.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/attributes.html @@ -1,13 +1,16 @@ {{ log.debug("Rendering attributes section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} -

{{ section.title or "Attributes:" }}

+

{{ section.title or lang.t("Attributes:") }}

- - - + + + @@ -33,7 +36,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Attributes:" }}

+

{{ section.title or lang.t("Attributes:") }}

NameTypeDescription{{ lang.t("Name") }}{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/examples.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/examples.html index 54bbfa5..bbec5e2 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/examples.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/examples.html @@ -1,5 +1,8 @@ {{ log.debug("Rendering examples section") }} -

{{ section.title or "Examples:" }}

+ +{% import "language.html" as lang with context %} + +

{{ section.title or lang.t("Examples:") }}

{% for section_type, sub_section in section.value %} {% if section_type.value == "text" %} {{ sub_section|convert_markdown(heading_level, html_id) }} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/other_parameters.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/other_parameters.html index 82fc493..a8ccbd9 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/other_parameters.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/other_parameters.html @@ -1,13 +1,16 @@ {{ log.debug("Rendering other parameters section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} -

{{ section.title or "Other Parameters:" }}

+

{{ section.title or lang.t("Other Parameters:") }}

{{ (section.title or "ATTRIBUTE").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("ATTRIBUTE")).rstrip(":").upper() }}{{ lang.t("DESCRIPTION") }}
- - - + + + @@ -33,7 +36,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Other Parameters:" }}

+

{{ section.title or lang.t("Other Parameters:") }}

NameTypeDescription{{ lang.t("Name") }}{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + @@ -71,7 +74,7 @@

{% if parameter.annotation %} - TYPE: + {{ lang.t("TYPE:") }} {% with expression = parameter.annotation %} {% include "expression.html" with context %} {% endwith %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html index b2b5750..515be81 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html @@ -1,14 +1,17 @@ {{ log.debug("Rendering parameters section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} -

{{ section.title or "Parameters:" }}

+

{{ section.title or lang.t("Parameters:") }}

{{ (section.title or "PARAMETER").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("PARAMETER")).rstrip(":").upper() }}{{ lang.t("DESCRIPTION") }}
- - - - + + + + @@ -33,7 +36,7 @@ {% include "expression.html" with context %} {% endwith %} {% else %} - required + {{ lang.t("required") }} {% endif %} @@ -43,7 +46,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Parameters:" }}

+

{{ section.title or lang.t("Parameters:") }}

NameTypeDescriptionDefault{{ lang.t("Name") }}{{ lang.t("Type") }}{{ lang.t("Description") }}{{ lang.t("Default") }}
- - + + @@ -81,7 +84,7 @@

{% if parameter.annotation %} - TYPE: + {{ lang.t("TYPE:") }} {% with expression = parameter.annotation %} {% include "expression.html" with context %} {% endwith %} @@ -89,7 +92,7 @@ {% endif %} {% if parameter.default %} - DEFAULT: + {{ lang.t("DEFAULT:") }} {% with expression = parameter.default %} {% include "expression.html" with context %} {% endwith %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html index 45832d1..e4edc66 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html @@ -1,12 +1,15 @@ {{ log.debug("Rendering raises section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} -

{{ section.title or "Raises:" }}

+

{{ section.title or lang.t("Raises:") }}

{{ (section.title or "PARAMETER").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("PARAMETER")).rstrip(":").upper() }} {{ lang.t("DESCRIPTION") }}
- - + + @@ -31,7 +34,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Raises:" }}

+

{{ lang.t(section.title) or lang.t("Raises:") }}

TypeDescription{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/receives.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/receives.html index 03b241c..09b8cae 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/receives.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/receives.html @@ -1,14 +1,17 @@ {{ log.debug("Rendering receives section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} {% set name_column = section.value|selectattr("name")|any %} -

{{ section.title or "Receives:" }}

+

{{ section.title or lang.t("Receives:") }}

{{ (section.title or "RAISES").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("RAISES")).rstrip(":").upper() }}{{ lang.t("DESCRIPTION") }}
- {% if name_column %}{% endif %} - - + {% if name_column %}{% endif %} + + @@ -34,7 +37,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Receives:" }}

+

{{ section.title or lang.t("Receives:") }}

NameTypeDescription{{ lang.t("Name") }}{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + @@ -84,7 +87,7 @@ {% if receives.name and receives.annotation %}

- TYPE: + {{ lang.t("TYPE:") }} {% with expression = receives.annotation %} {% include "expression.html" with context %} {% endwith %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html index ad63db8..374f8de 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html @@ -1,14 +1,17 @@ {{ log.debug("Rendering returns section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} {% set name_column = section.value|selectattr("name")|any %} -

{{ section.title or "Returns:" }}

+

{{ section.title or lang.t("Returns:") }}

{{ (section.title or "RECEIVES").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("RECEIVES")).rstrip(":").upper()) }}{{ lang.t("DESCRIPTION") }}
- {% if name_column %}{% endif %} - - + {% if name_column %}{% endif %} + + @@ -34,7 +37,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Returns:" }}

+

{{ section.title or lang.t("Returns:") }}

NameTypeDescription{{ lang.t("Name") }}{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + @@ -84,7 +87,7 @@ {% if returns.name and returns.annotation %}

- TYPE: + {{ lang.t("TYPE:") }} {% with expression = returns.annotation %} {% include "expression.html" with context %} {% endwith %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html index 49ae438..cf1cc4a 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html @@ -1,12 +1,15 @@ {{ log.debug("Rendering warns section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} -

{{ section.title or "Warns:" }}

+

{{ section.title or lang.t("Warns:") }}

{{ (section.title or "RETURNS").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("RETURNS")).rstrip(":").upper() }}{{ lang.t("DESCRIPTION").upper() }}
- - + + @@ -31,7 +34,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Warns:" }}

+

{{ section.title or lang.t("Warns:") }}

TypeDescription{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/yields.html b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/yields.html index 93b3cfd..9a0db29 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/yields.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/yields.html @@ -1,14 +1,17 @@ {{ log.debug("Rendering yields section") }} + +{% import "language.html" as lang with context %} + {% if config.docstring_section_style == "table" %} {% block table_style %} {% set name_column = section.value|selectattr("name")|any %} -

{{ section.title or "Yields:" }}

+

{{ section.title or lang.t("Yields:") }}

{{ (section.title or "WARNS").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("WARNS")).rstrip(":").upper() }}{{ lang.t("DESCRIPTION") }}
- {% if name_column %}{% endif %} - - + {% if name_column %}{% endif %} + + @@ -34,7 +37,7 @@ {% endblock table_style %} {% elif config.docstring_section_style == "list" %} {% block list_style %} -

{{ section.title or "Yields:" }}

+

{{ section.title or lang.t("Yields:") }}

NameTypeDescription{{ lang.t("Name") }}{{ lang.t("Type") }}{{ lang.t("Description") }}
- - + + @@ -84,7 +87,7 @@ {% if yields.name and yields.annotation %}

- TYPE: + {{ lang.t("TYPE:") }}: {% with expression = yields.annotation %} {% include "expression.html" with context %} {% endwith %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/function.html b/src/mkdocstrings_handlers/python/templates/material/_base/function.html index 70c2689..4ae5333 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/function.html +++ b/src/mkdocstrings_handlers/python/templates/material/_base/function.html @@ -1,5 +1,7 @@ {{ log.debug("Rendering " + function.path) }} +{% import "language.html" as lang with context %} +

{% with html_id = function.path %} @@ -61,7 +63,7 @@ {% if config.show_source and function.source %}
- Source code in {{ function.relative_filepath }} + {{ lang.t("Source code in") }} {{ function.relative_filepath }} {{ function.source|highlight(language="python", linestart=function.lineno, linenums=True) }}
{% endif %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/languages/en.html b/src/mkdocstrings_handlers/python/templates/material/_base/languages/en.html new file mode 100644 index 0000000..5836ccc --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/_base/languages/en.html @@ -0,0 +1,28 @@ + +{% macro t(key) %}{{ { + "ATTRIBUTE": "ATTRIBUTE", + "Attributes:": "Attributes:", + "DEFAULT:": "DEFAULT:", + "Default": "Default", + "DESCRIPTION": "DESCRIPTION", + "Description": "Description", + "Examples:": "Examples:", + "Name": "Name", + "Other Parameters:": "Other Parameters:", + "PARAMETER": "PARAMETER", + "Parameters:": "Parameters:", + "RAISES": "RAISES", + "Raises:" : "Raises:", + "RECEIVES": "RECEIVES", + "Receives:": "Receives:", + "required": "required", + "RETURNS": "RETURNS", + "Returns:": "Returns:", + "Source code in": "Source code in", + "TYPE:": "TYPE:", + "Type": "Type", + "WARNS": "WARNS", + "Warns:": "Warns:", + "YIELDS": "YIELDS", + "Yields:": "Yields:", + }[key] }}{% endmacro %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/languages/ja.html b/src/mkdocstrings_handlers/python/templates/material/_base/languages/ja.html new file mode 100644 index 0000000..6b52ebc --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/_base/languages/ja.html @@ -0,0 +1,28 @@ + +{% macro t(key) %}{{ { + "ATTRIBUTE": "属性", + "Attributes:": "属性:", + "DEFAULT:": "デフォルト:", + "Default": "デフォルト", + "DESCRIPTION": "デスクリプション", + "Description": "デスクリプション", + "Examples:": "例:", + "Name": "名前", + "Other Parameters:": "他の引数:", + "PARAMETER": "引数", + "Parameters:": "引数:", + "RAISES": "発生", + "Raises:" : "発生:", + "RECEIVES": "取得", + "Receives:": "取得:", + "required": "必須", + "RETURNS": "戻り値", + "Returns:": "戻り値:", + "Source code in": "ソースコード位置:", + "TYPE:": "タイプ:", + "Type": "タイプ", + "WARNS": "警告", + "Warns:": "警告:", + "YIELDS": "返す", + "Yields:": "返す:", +}[key] }}{% endmacro %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/languages/zh.html b/src/mkdocstrings_handlers/python/templates/material/_base/languages/zh.html new file mode 100644 index 0000000..a1516f1 --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/_base/languages/zh.html @@ -0,0 +1,28 @@ + +{% macro t(key) %}{{ { + "ATTRIBUTE": "属性", + "Attributes:": "属性:", + "DEFAULT:": "默认:", + "Default": "默认", + "DESCRIPTION": "描述", + "Description": "描述", + "Examples:": "示例:", + "Name": "名称", + "Other Parameters:": "其他参数:", + "PARAMETER": "参数", + "Parameters:": "参数:", + "RAISES": "引发", + "Raises:" : "引发:", + "Receives:": "接收:", + "RECEIVES": "接收", + "required": "必需", + "RETURNS": "返回", + "Returns:": "返回:", + "Source code in": "源代码位于:", + "TYPE:": "类型:", + "Type": "类型", + "Warns:": "警告:", + "WARNS": "警告", + "YIELDS": "产生", + "Yields:": "产生:", + }[key] }}{% endmacro %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/material/language.html b/src/mkdocstrings_handlers/python/templates/material/language.html new file mode 100644 index 0000000..26647ff --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/language.html @@ -0,0 +1,10 @@ + +{% set lang_pth = "languages/" ~ locale ~ ".html" %} +{% if lang_pth is existing_template %} + {% import lang_pth as lang %} + {% import "languages/en.html" as fallback %} + {% macro t(key) %}{{ lang.t(key) or fallback.t(key) }}{% endmacro %} +{% else %} + {% import "languages/en.html" as lang %} + {% macro t(key) %}{{ lang.t(key) }}{% endmacro %} +{% endif %} diff --git a/src/mkdocstrings_handlers/python/templates/material/languages/en.html b/src/mkdocstrings_handlers/python/templates/material/languages/en.html new file mode 100644 index 0000000..eab0a3f --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/languages/en.html @@ -0,0 +1 @@ +{% extends "_base/languages/en.html" %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/material/languages/ja.html b/src/mkdocstrings_handlers/python/templates/material/languages/ja.html new file mode 100644 index 0000000..0463322 --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/languages/ja.html @@ -0,0 +1 @@ +{% extends "_base/languages/ja.html" %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/material/languages/zh.html b/src/mkdocstrings_handlers/python/templates/material/languages/zh.html new file mode 100644 index 0000000..90aeae6 --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/material/languages/zh.html @@ -0,0 +1 @@ +{% extends "_base/languages/zh.html" %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/attributes.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/attributes.html index 3ed566d..6f597cd 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/attributes.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/attributes.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} +
{{ (section.title or "YIELDS").rstrip(":").upper() }}DESCRIPTION{{ (section.title or lang.t("YIELDS")).rstrip(":").upper() }}{{ lang.t("DESCRIPTION") }}
@@ -6,7 +9,7 @@ - +
{{ section.title or "Attributes:" }}{{ section.title or lang.t("Attributes:") }}
    {% for attribute in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/other_parameters.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/other_parameters.html index c5b4b03..d37bc8c 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/other_parameters.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/other_parameters.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
    {{ section.title or "Other parameters:" }}{{ section.title or lang.t("Other parameters:") }}
      {% for parameter in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/parameters.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/parameters.html index b05733a..461fe2a 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/parameters.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/parameters.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
      {{ section.title or "Parameters:" }}{{ section.title or lang.t("Parameters:") }}
        {% for parameter in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/raises.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/raises.html index b744df2..f82437d 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/raises.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/raises.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
        {{ section.title or "Raises:" }}{{ section.title or lang.t("Raises:") }}
          {% for raises in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/receives.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/receives.html index 127bcd3..f112351 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/receives.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/receives.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
          {{ section.title or "Receives:" }}{{ section.title or lang.t("Receives:") }}
            {% for receives in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/returns.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/returns.html index 92f29e3..28b8377 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/returns.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/returns.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
            {{ section.title or "Returns:" }}{{ section.title or lang.t("Returns:") }}
              {% for returns in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/warns.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/warns.html index 1263178..35aff0b 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/warns.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/warns.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
              {{ section.title or "Warns:" }}{{ section.title or lang.t("Warns:") }}
                {% for warns in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/yields.html b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/yields.html index 86e8ff7..7838a66 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/yields.html +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/docstring/yields.html @@ -1,4 +1,7 @@ {{ log.debug() }} + +{% import "language.html" as lang with context %} + @@ -6,7 +9,7 @@ - +
                {{ section.title or "Yields:" }}{{ section.title or lang.t("Yields:") }}
                  {% for yields in section.value %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/language.html b/src/mkdocstrings_handlers/python/templates/readthedocs/language.html new file mode 100644 index 0000000..26647ff --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/language.html @@ -0,0 +1,10 @@ + +{% set lang_pth = "languages/" ~ locale ~ ".html" %} +{% if lang_pth is existing_template %} + {% import lang_pth as lang %} + {% import "languages/en.html" as fallback %} + {% macro t(key) %}{{ lang.t(key) or fallback.t(key) }}{% endmacro %} +{% else %} + {% import "languages/en.html" as lang %} + {% macro t(key) %}{{ lang.t(key) }}{% endmacro %} +{% endif %} diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/languages/en.html b/src/mkdocstrings_handlers/python/templates/readthedocs/languages/en.html new file mode 100644 index 0000000..acc6d5a --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/languages/en.html @@ -0,0 +1,11 @@ + +{% macro t(key) %}{{ { + "Attributes:": "Attributes:", + "Other parameters:": "Other parameters:", + "Parameters:": "Parameters:", + "Raises:" : "Raises:", + "Receives:": "Receives:", + "Returns:": "Returns:", + "Warns:": "Warns:", + "Yields:": "Yields:", + }[key] }}{% endmacro %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/languages/ja.html b/src/mkdocstrings_handlers/python/templates/readthedocs/languages/ja.html new file mode 100644 index 0000000..9ae4a56 --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/languages/ja.html @@ -0,0 +1,11 @@ + +{% macro t(key) %}{{ { + "Attributes:": "属性:", + "Other Parameters:": "他の引数:", + "Parameters:": "引数:", + "Raises:" : "発生:", + "Receives:": "取得:", + "Returns:": "戻り値:", + "Warns:": "警告:", + "Yields:": "返す:", + }[key] }}{% endmacro %} \ No newline at end of file diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/languages/zh.html b/src/mkdocstrings_handlers/python/templates/readthedocs/languages/zh.html new file mode 100644 index 0000000..42184f9 --- /dev/null +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/languages/zh.html @@ -0,0 +1,11 @@ + +{% macro t(key) %}{{ { + "Attributes:": "属性:", + "Other Parameters:": "其他参数:", + "Parameters:": "参数:", + "Raises:" : "引发:", + "Receives:": "接收:", + "Returns:": "返回:", + "Warns:": "警告:", + "Yields:": "产生:", + }[key] }}{% endmacro %} \ No newline at end of file diff --git a/tests/test_handler.py b/tests/test_handler.py index fc31942..4971e13 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -63,7 +63,8 @@ def test_render_docstring_examples_section(handler: PythonHandler) -> None: ], ) template = handler.env.get_template("docstring/examples.html") - rendered = template.render(section=section) + rendered = template.render(section=section, locale="en") + template.render(section=section, locale="not_existing") assert "

                  This is an example.

                  " in rendered assert "print" in rendered assert "Hello" in rendered