Skip to content

Commit 66287b2

Browse files
committed
Fix locale with hyphens
1 parent 3fce7a0 commit 66287b2

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
291291
if not locale:
292292
locale = self.config.get("locale")
293293

294+
# Normalize locale for Babel: convert hyphens to underscores (e.g., pt-BR -> pt_BR)
295+
# This is needed for compatibility with mkdocs-static-i18n and other plugins
296+
# that may use hyphenated locale codes, while Babel expects underscores
297+
if locale:
298+
locale = str(locale).replace("-", "_")
299+
294300
# Retrieve git commit timestamp
295301
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
296302
if getattr(page.file, "generated_by", None):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
theme:
5+
name: 'material'
6+
language: zh
7+
8+
plugins:
9+
- search
10+
- git-revision-date-localized:
11+
locale: "pt-BR"

tests/test_builds.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
341341
"basic_project/mkdocs.yml",
342342
"basic_project/mkdocs_theme_timeago_locale.yml",
343343
"basic_project/mkdocs_theme_language.yml",
344+
"basic_project/mkdocs_theme_locale_and_language_5char.yml",
344345
"basic_project/mkdocs_theme_locale_and_language.yml",
345346
"basic_project/mkdocs_theme_locale_disabled.yml",
346347
"basic_project/mkdocs_theme_timeago.yml",
@@ -376,7 +377,7 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
376377
testproject_path = setup_clean_mkdocs_folder(mkdocs_yml_path=f"tests/fixtures/{mkdocs_file}", output_path=tmp_path)
377378
setup_commit_history(testproject_path)
378379
result = build_docs_setup(testproject_path)
379-
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'"
380+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f tests/fixtures/{mkdocs_file}'"
380381

381382
plugin_config = get_plugin_config_from_mkdocs(str(testproject_path / "mkdocs.yml"))
382383
tags_file = testproject_path / "site/page_with_tag/index.html"

0 commit comments

Comments
 (0)