Skip to content

Commit

Permalink
feat: prefer language, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGlassBlock committed Jan 18, 2024
1 parent 03e53e3 commit 4494d4e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
12 changes: 12 additions & 0 deletions docs/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ with the `handler` option:
handler: python
```

## Internationalization (i18n)

The Python handler of *mkdocstrings* supports changing the output language.

The following languages can be used by enabling in [mkdocstrings configuration](https://mkdocstrings.github.io/usage/#internationalization-i18n):

Currently supported languages:

- :flag_us: `en`: English (default)
- :flag_cn: `zh`: Chinese (Simplified)
- :flag_jp: `ja`: Japanese

## Configuration

When installed, the Python handler becomes the default *mkdocstrings* handler.
Expand Down
14 changes: 7 additions & 7 deletions src/mkdocstrings_handlers/python/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def __init__(
*args: Any,
config_file_path: str | None = None,
paths: list[str] | None = None,
locale: str = "en",
language: str = "en",
load_external_modules: bool = False,
**kwargs: Any,
) -> None:
Expand All @@ -199,7 +199,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.
language: The language to use when rendering content.
load_external_modules: Load external modules when resolving aliases.
**kwargs: Same thing, but with keyword arguments.
"""
Expand All @@ -222,7 +222,7 @@ def __init__(
self._paths = search_paths
self._modules_collection: ModulesCollection = ModulesCollection()
self._lines_collection: LinesCollection = LinesCollection()
self._locale = locale
self._language = language

@classmethod
def load_inventory(
Expand Down Expand Up @@ -357,8 +357,8 @@ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa
"config": final_config,
data.kind.value: data,
"heading_level": heading_level,
"language": self._language,
"root": True,
"locale": self._locale,
},
)

Expand Down Expand Up @@ -431,7 +431,7 @@ def get_handler(
custom_templates: str | None = None,
config_file_path: str | None = None,
paths: list[str] | None = None,
locale: str = "en",
language: str = "en",
load_external_modules: bool = False,
**config: Any, # noqa: ARG001
) -> PythonHandler:
Expand All @@ -442,7 +442,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.
language: The language to use when rendering content.
load_external_modules: Load external modules when resolving aliases.
**config: Configuration passed to the handler.
Expand All @@ -455,6 +455,6 @@ def get_handler(
custom_templates=custom_templates,
config_file_path=config_file_path,
paths=paths,
locale=locale,
language=language,
load_external_modules=load_external_modules,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Import translations for given language and fallback -->
{% set lang_pth = "languages/" ~ locale ~ ".html" %}
{% set lang_pth = "languages/" ~ language ~ ".html" %}
{% if lang_pth is existing_template %}
{% import lang_pth as lang %}
{% import "languages/en.html" as fallback %}
Expand Down

0 comments on commit 4494d4e

Please sign in to comment.