Skip to content

Commit 14fc745

Browse files
authored
Allow to translate site_name and site_description (#46)
* Allow to translate `site_name` and `site_description` * Document new config setting * Fix error setting language for mkdocs-material theme * Add tests * Add description to translated homepages using mkdocs and readthedocs themes
1 parent fdb751e commit 14fc745

22 files changed

+619
-39
lines changed

.vscode/extensions.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"recommendations": ["redhat.vscode-yaml"],
3+
"unwantedRecommendations": []
4+
}

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"yaml.schemas": {
3+
"https://squidfunk.github.io/mkdocs-material/schema.json": "mkdocs.yml"
4+
}
5+
}

docs/locale/es/_compendium.po

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
msgid ""
33
msgstr ""
44

5+
msgid "Mkdocs plugin for translations using PO files"
6+
msgstr "Plugin de Mkdocs para traducciones usando archivos PO"
7+
58
msgid "Permanent link"
69
msgstr "Link permanente"
710

docs/locale/es/config.md.po

+21
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,24 @@ msgstr "Filtrar por porcentaje"
246246

247247
msgid "Filter by number of messages"
248248
msgstr "Filtrar por número de mensajes"
249+
250+
msgid ""
251+
"Configuration settings to translate. Currently only the next settings are "
252+
"supported:"
253+
msgstr ""
254+
"Parámetros de configuración a traducir. Actualmente sólo están soportados "
255+
"los siguientes parámetros:"
256+
257+
msgid ""
258+
"[mkdocs-config-site_name]: https://www.mkdocs.org/user-"
259+
"guide/configuration/#site_name"
260+
msgstr ""
261+
"[mkdocs-config-site_name]: https://www.mkdocs.org/user-"
262+
"guide/configuration/#site_name"
263+
264+
msgid ""
265+
"[mkdocs-config-site_description]: https://www.mkdocs.org/user-"
266+
"guide/configuration/#site_description"
267+
msgstr ""
268+
"[mkdocs-config-site_description]: https://www.mkdocs.org/user-"
269+
"guide/configuration/#site_description"

docs/src/config.md

+13
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ The context for the template includes:
192192

193193
## Content
194194

195+
<!-- mdpo-disable-next-line -->
196+
### **`translate`** (*list[str]*)
197+
198+
Configuration settings to translate. Currently only the next settings are
199+
supported:
200+
201+
<!-- mdpo-disable -->
202+
- [`site_name`][mkdocs-config-site_name]
203+
- [`site_description`][mkdocs-config-site_description]
204+
<!-- mdpo-enable -->
205+
195206
<!-- mdpo-disable-next-line -->
196207
### **`cross_language_search`** (*bool*)
197208

@@ -291,3 +302,5 @@ this list.
291302
[mkdocs-material-site-language-selector]: https://squidfunk.github.io/mkdocs-material/setup/changing-the-language/#site-language-selector
292303
[jinja2-template]: https://jinja2docs.readthedocs.io/en/stable/templates.html
293304
[polib.POFile]: https://polib.readthedocs.io/en/latest/api.html#polib.POFile
305+
[mkdocs-config-site_name]: https://www.mkdocs.org/user-guide/configuration/#site_name
306+
[mkdocs-config-site_description]: https://www.mkdocs.org/user-guide/configuration/#site_description

examples/material-theme/docs/locale/es/_compendium.po

+6
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22
msgid ""
33
msgstr ""
44

5+
msgid "Example using mkdocs-material theme with mkdocs-mdpo-plugin"
6+
msgstr "Ejemplo usando el tema mkdocs-material con mkdocs-mdpo-plugin"
7+
8+
msgid "mkdocs-mdpo-plugin Material theme example"
9+
msgstr "Ejemplo de tema Material con mkdocs-mdpo-plugin"
10+
511
msgid "Some content"
612
msgstr "Algo de contenido"

examples/material-theme/docs/locale/fr/_compendium.po

+6
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22
msgid ""
33
msgstr ""
44

5+
msgid "Example using mkdocs-material theme with mkdocs-mdpo-plugin"
6+
msgstr "Exemple utilisant le thème mkdocs-material avec mkdocs-mdpo-plugin"
7+
8+
msgid "mkdocs-mdpo-plugin Material theme example"
9+
msgstr "mkdocs-mdpo-plugin exemple de Material thème"
10+
511
msgid "Some content"
612
msgstr "Du contenu"

examples/material-theme/mkdocs.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
site_name: mkdocs-mdpo-plugin Material example
1+
site_name: mkdocs-mdpo-plugin Material theme example
2+
site_description: Example using mkdocs-material theme with mkdocs-mdpo-plugin
23
site_url: https://mkdocs-mdpo.ga
34
docs_dir: docs/src
45
edit_uri: tree/master/docs/src
@@ -17,6 +18,9 @@ plugins:
1718
- mdpo:
1819
cross_language_search: false
1920
locale_dir: ../locale
21+
translate:
22+
- site_name
23+
- site_description
2024

2125
extra:
2226
alternate:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
msgid ""
3+
msgstr ""
4+
5+
msgid "Example using builtin Mkdocs theme with mkdocs-mdpo-plugin"
6+
msgstr "Ejemplo usando el tema incluido en Mkdocs con mkdocs-mdpo-plugin"
7+
8+
msgid "mkdocs-mdpo-plugin Mkdocs theme example"
9+
msgstr "Ejemplo con tema Mkdocs para mkdocs-mdpo-plugin"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
msgid ""
3+
msgstr ""
4+
5+
msgid "Example using builtin Mkdocs theme with mkdocs-mdpo-plugin"
6+
msgstr "Exemple utilisant le thème Mkdocs intégré avec mkdocs-mdpo-plugin"
7+
8+
msgid "mkdocs-mdpo-plugin Mkdocs theme example"
9+
msgstr "mkdocs-mdpo-plugin exemple de Mkdocs thème"

examples/mkdocs-theme/mkdocs.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
site_name: mkdocs-mdpo-plugin Mkdocs theme example
2+
site_description: Example using builtin Mkdocs theme with mkdocs-mdpo-plugin
23
site_url: https://mkdocs-mdpo.ga
34
docs_dir: docs/src
45

@@ -15,3 +16,6 @@ plugins:
1516
- fr
1617
cross_language_search: false
1718
locale_dir: ../locale
19+
translate:
20+
- site_name
21+
- site_description

mkdocs.yml

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ plugins:
8383
locale_dir: ../locale
8484
cross_language_search: false
8585
min_translated_messages: 95%
86+
translate:
87+
- site_description
8688
- minify:
8789
minify_html: true
8890

mkdocs_mdpo_plugin/config.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
('cross_language_search', Type(bool, default=True)),
2828
('min_translated_messages', Type((str, int), default=None)),
2929
('exclude', Type(list, default=[])),
30+
('translate', Type(list, default=[])),
3031
)
3132

3233

@@ -183,7 +184,7 @@ def _languages_required():
183184
# if cross_language_search is active, add all languages to 'search'
184185
# plugin configuration
185186
lunr_languages = get_lunr_languages()
186-
search_langs = config['plugins']['search'].config['lang'] or []
187+
search_langs = config['plugins']['search'].config.get('lang', [])
187188
for language in plugin.config['languages']:
188189
if language in lunr_languages:
189190
if language not in search_langs:
@@ -220,7 +221,7 @@ def _languages_required():
220221
plugin.config['min_translated_messages'] = min_translated
221222

222223
# check that 'exclude' contains a valid list
223-
exclude = plugin.config.get('exclude') or []
224+
exclude = plugin.config.get('exclude', [])
224225
if not isinstance(exclude, list):
225226
raise ValidationError(
226227
'Expected mdpo\'s "exclude" setting to be a list, but found'
@@ -236,6 +237,28 @@ def _languages_required():
236237
)
237238
plugin.config['exclude'] = exclude
238239

240+
# translation of configuration settings
241+
valid_translate_settings = ['site_name', 'site_description']
242+
for setting in plugin.config.get('translate', []):
243+
if setting not in valid_translate_settings:
244+
valid_translate_settings_readable = ' and '.join([
245+
f"'{opt}'" for opt in valid_translate_settings
246+
])
247+
raise ValidationError(
248+
f"The setting '{setting}' is not supported for"
249+
" 'plugins.mdpo.translate' config setting. Valid settings"
250+
f' are {valid_translate_settings_readable}',
251+
)
252+
elif (
253+
setting == 'site_description'
254+
and not config.get('site_description')
255+
):
256+
logger.warn(
257+
'[mdpo] "site_description" is configured to be translated'
258+
' but was not defined in mkdocs.yml',
259+
)
260+
plugin.config['translate'].remove('site_description')
261+
239262
# store reference in plugin to markdown_extensions for later usage
240263
plugin.extensions.markdown = markdown_extensions
241264

mkdocs_mdpo_plugin/mdpo_utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
STRIP_COMMAND_REGEX = re.compile(r'[^\\]' + COMMAND_SEARCH_REGEX)
9+
MDPO_SETTINGS_TAGS = {'mdpo-site_description', 'mdpo-site_name'}
910

1011

1112
def remove_mdpo_commands_preserving_escaped(text):
@@ -28,3 +29,9 @@ def remove_mdpo_commands_preserving_escaped(text):
2829
'<!-- mdpo',
2930
text,
3031
)
32+
33+
34+
def remove_mdpo_setting_tags_from_po_entry(entry):
35+
for flag in MDPO_SETTINGS_TAGS:
36+
while flag in entry.flags:
37+
entry.flags.remove(flag)

0 commit comments

Comments
 (0)