1# $Id$
2# Author: David Goodger <goodger@python.org>
3# Copyright: This module has been placed in the public domain.
4
5# Internationalization details are documented in
6# <https://docutils.sourceforge.io/docs/howto/i18n.html>.
7
8"""
9This package contains modules for language-dependent features of
10reStructuredText.
11"""
12
13from __future__ import annotations
14
15__docformat__ = 'reStructuredText'
16
17from typing import TYPE_CHECKING, overload
18
19from docutils.languages import LanguageImporter
20
21if TYPE_CHECKING:
22 import types
23 from typing import NoReturn, Protocol
24
25 class RSTLanguageModule(Protocol):
26 __name__: str
27
28 directives: dict[str, str]
29 roles: dict[str, str]
30
31
32class RstLanguageImporter(LanguageImporter):
33 """Import language modules.
34
35 When called with a BCP 47 language tag, instances return a module
36 with localisations for "directive" and "role" names for from
37 `docutils.parsers.rst.languages` or the PYTHONPATH.
38
39 If there is no matching module, warn (if a `reporter` is passed)
40 and return None.
41 """
42 packages = ('docutils.parsers.rst.languages.', '')
43 warn_msg = 'rST localisation for language "%s" not found.'
44 fallback = None
45
46 @overload
47 def check_content(self, module: RSTLanguageModule) -> None:
48 ...
49
50 @overload
51 def check_content(self, module: types.ModuleType) -> NoReturn:
52 ...
53
54 def check_content(self, module: RSTLanguageModule | types.ModuleType
55 ) -> None:
56 """Check if we got an rST language module."""
57 if not (isinstance(module.directives, dict)
58 and isinstance(module.roles, dict)):
59 raise ImportError
60
61
62get_language: LanguageImporter[RSTLanguageModule] = RstLanguageImporter()