Skip to content

Commit

Permalink
Merge f14f135 into 14fc745
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Mar 3, 2022
2 parents 14fc745 + f14f135 commit a4ae6dc
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/src/useful-recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use them directly [as a command line interface][mdpo-cli] or through is
=== "`md2po2md` command line interface"

```bash
md2po2md README.md -l es -l fr -o locale/{lang}
md2po2md README.md -l es fr -o locale/{lang}
```

=== "pre-commit hook configuration"
Expand Down
7 changes: 5 additions & 2 deletions examples/material-theme/docs/locale/es/foo.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
msgid ""
msgstr ""

msgid "Foo"
msgstr "Foo es"
msgid "Some description"
msgstr "Algo de descripción"

msgid "Bar"
msgstr "Bar es"

msgid "Baz"
msgstr "Baz es"

msgid "Foo"
msgstr "Foo es"
6 changes: 3 additions & 3 deletions examples/material-theme/docs/locale/es/index.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
msgid ""
msgstr ""

msgid "Home"
msgstr "Inicio"

msgid "Welcome to MkDocs"
msgstr "Bienvenido a Mkdocs"

msgid "Home"
msgstr "Inicio"
9 changes: 6 additions & 3 deletions examples/material-theme/docs/locale/fr/foo.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
msgid ""
msgstr ""

msgid "Foo"
msgstr "Foo fr"

msgid "Bar"
msgstr "Bar fr"

msgid "Baz"
msgstr "Baz fr"

msgid "Some description"
msgstr "Une description"

msgid "Foo"
msgstr "Foo fr"
6 changes: 3 additions & 3 deletions examples/material-theme/docs/locale/fr/index.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
msgid ""
msgstr ""

msgid "Home"
msgstr "Accueil"

msgid "Welcome to MkDocs"
msgstr "Bienvenue sur mkdocs"

msgid "Home"
msgstr "Accueil"
4 changes: 4 additions & 0 deletions examples/material-theme/docs/src/foo.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
description: Some description
---

# Bar

Baz
Expand Down
112 changes: 95 additions & 17 deletions mkdocs_mdpo_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ def on_page_markdown(self, markdown, page, config, files):
#
# translate title
translated_page_title, _title_in_pofile = (None, False)
# translated custom description
page_meta_description = (
page.meta.get('description')
if config['theme'].name == 'material' else None
)
translated_page_desc, _desc_in_pofile = (None, False)

# translate site_name and site_description
translated_config_settings = {
Expand All @@ -304,15 +310,52 @@ def on_page_markdown(self, markdown, page, config, files):
key: False for key in self.config['translate']
}

for entry in po:
if entry.msgid == page.title:
# matching title found
entry.obsolete = False
translated_page_title = entry.msgstr
_title_in_pofile = True
_translated_entries_msgids.append(page.title)
if entry.msgstr:
_translated_entries_msgstrs.append(page.title)
if page_meta_description:
for entry in po:
if entry.msgid == page.title:
# matching title found
entry.obsolete = False
translated_page_title = entry.msgstr
_title_in_pofile = True
if entry.msgstr:
_translated_entries_msgstrs.append(
entry.msgstr,
)

if entry.msgid == page_meta_description:
# matching description found
entry.obsolete = False
translated_page_desc = entry.msgstr
_desc_in_pofile = True
if entry.msgstr:
_translated_entries_msgstrs.append(
page_meta_description,
)

# add description to PO file if not added
if not _desc_in_pofile:
po.insert(
0,
polib.POEntry(
msgid=page_meta_description,
msgstr='',
),
)

_translated_entries_msgids.append(
page_meta_description,
)
else:
for entry in po:
if entry.msgid == page.title:
# matching title found
entry.obsolete = False
translated_page_title = entry.msgstr
_title_in_pofile = True
if entry.msgstr:
_translated_entries_msgstrs.append(
entry.msgstr,
)

for entry in compendium_pofile:
for setting in translated_config_settings:
Expand Down Expand Up @@ -381,6 +424,19 @@ def on_page_markdown(self, markdown, page, config, files):
events=po2md_events,
wrapwidth=math.inf, # ignore line wrapping
)
if page_meta_description:
po2md.translated_entries.append(
polib.POEntry(
msgid=page_meta_description,
msgstr='',
),
)
po2md.translated_entries.append(
polib.POEntry(
msgid=page.title,
msgstr='',
),
)
content = po2md.translate(markdown)

_disabled_msgids = [
Expand All @@ -395,6 +451,7 @@ def on_page_markdown(self, markdown, page, config, files):
# mock variables if the file is excluded from being translated
content = markdown
translated_page_title = None
translated_page_desc = None
_disabled_msgids = []
_translated_entries_msgstrs = []
_translated_entries_msgids = []
Expand Down Expand Up @@ -427,6 +484,8 @@ def on_page_markdown(self, markdown, page, config, files):
new_file,
config,
)
if translated_page_desc:
new_page.meta['description'] = translated_page_desc

# overwrite the edit uri for the translated page targetting
# the PO file located in the repository
Expand Down Expand Up @@ -459,6 +518,15 @@ def on_page_markdown(self, markdown, page, config, files):
self.translations.config_settings[language] = (
translated_config_settings
)
if language not in self.translations.page_metas:
self.translations.page_metas[language] = {}
if (
new_file.src_path
not in self.translations.page_metas[language]
):
self.translations.page_metas[
language
][new_file.src_path] = new_page.meta

# change file url
url = removesuffix(new_page.file.url, '.md') + '.html'
Expand Down Expand Up @@ -601,6 +669,10 @@ def on_post_page(self, output, page, config):
f'{config["site_name"]}</title>',
f'{tr_settings["site_name"]}</title>',
)

meta_description = self.translations.page_metas[
language
][page.file.src_path].get('description')
if tr_settings.get('site_description'):
# insert site_description into description meta tag
# if the file is a translated index, only for
Expand All @@ -617,13 +689,21 @@ def on_post_page(self, output, page, config):
' />'
),
)
else:
elif not meta_description:
# mkdocs-material theme includes the description
# in all pages
#
# is defined using a 'description' metadata with
# 'site_description' config setting as fallback
output = output.replace(
f'content="{config["site_description"]}"',
f'content="{tr_settings["site_description"]}"',
)
if meta_description:
output = output.replace(
f'content="{config["site_description"]}"',
f'content="{meta_description}"',
)

# write translated HTML file to 'site' directory
os.makedirs(
Expand Down Expand Up @@ -709,9 +789,8 @@ def on_post_build(self, config):

_msgids_appended_to_compendium = []
for translation in translations:
po = polib.pofile(translation.po_filepath)
_entry_found = None
for entry in po:
for entry in translation.po:
if entry.msgid == repeated_msgid:
if (
repeated_msgid not in
Expand All @@ -725,8 +804,8 @@ def on_post_build(self, config):
_entry_found = entry
break
if _entry_found:
po.remove(_entry_found)
po.save(translation.po_filepath)
translation.po.remove(_entry_found)
translation.po.save(translation.po_filepath)

for entry in compendium_pofile:
if entry.msgid not in repeated_msgids:
Expand All @@ -752,11 +831,10 @@ def on_post_build(self, config):
# po_filepath is None if the file has been excluded from
# translations using 'exclude' config setting
if translation.po_filepath is not None:
po = polib.pofile(translation.po_filepath)
for entry in po:
for entry in translation.po:
if entry.msgid not in translation.translated_msgids:
entry.obsolete = True
po.save(translation.po_filepath)
translation.po.save(translation.po_filepath)

# reset mkdocs build instance
MkdocsBuild._instance = None
Expand Down
8 changes: 7 additions & 1 deletion mkdocs_mdpo_plugin/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Translations:
'locations',
'stats',
'config_settings',
'page_metas',
}

def __init__(self):
Expand Down Expand Up @@ -100,6 +101,10 @@ def __init__(self):
# {lang: {site_name: str, site_description: str}}
self.config_settings = {}

# translated page metadatas by language
# {lang: {page.file.src_path: {...meta...}}}
self.page_metas = {}

def __str__(self): # pragma: no cover
current = 'None' if self.current is None else 'Translation(...)'
return (
Expand All @@ -111,6 +116,7 @@ def __str__(self): # pragma: no cover
f'{str(self.compendium_msgstrs_tr)},'
f' current={current},'
f' locations={str(self.locations)},'
f' config_settings={str(self.config_settings)}'
f' config_settings={str(self.config_settings)},'
f' page_metas={str(self.page_metas)}'
')'
)

0 comments on commit a4ae6dc

Please sign in to comment.