Skip to content

Commit e8ebb05

Browse files
authored
Merge pull request #196 from timvink/fix_i18n
Fix i18n bug
2 parents 3fce7a0 + 6c51d10 commit e8ebb05

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

docs/howto/specify-locale.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Specify a locale
22

3-
`locale` is aa two letter [ISO639](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code (f.e. `en`) or [5-letter language code with added territory/region/country](https://www.mkdocs.org/user-guide/localizing-your-theme/#supported-locales) (`en_US`) that `git-revision-date-localized` uses to display dates in your preferred language.
3+
`locale` is a two letter [ISO639](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code (f.e. `en`) or [5-letter language code with added territory/region/country](https://www.mkdocs.org/user-guide/localizing-your-theme/#supported-locales) (`en_US`) that `git-revision-date-localized` uses to display dates in your preferred language.
44

55
For example:
66

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ target-version = "py310"
111111
# Always autofix
112112
fix = true
113113

114-
[tool.uv]
115-
dev-dependencies = [
114+
[dependency-groups]
115+
dev = [
116116
"click>=8.1.8",
117117
"codecov>=2.1.13",
118118
"mkdocs-gen-files>=0.5.0",

src/mkdocs_git_revision_date_localized_plugin/dates.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def get_date_formats(
2222
assert time_zone is not None
2323
assert locale is not None
2424

25+
# Normalize locale for Babel: convert hyphens to underscores (e.g., pt-BR -> pt_BR)
26+
# This is needed for compatibility with mkdocs-static-i18n and other plugins
27+
# that may use hyphenated locale codes, while Babel expects underscores
28+
if locale:
29+
locale = str(locale).replace("-", "_")
30+
2531
utc_revision_date = datetime.fromtimestamp(int(unix_timestamp), tz=timezone.utc)
2632
loc_revision_date = utc_revision_date.replace(tzinfo=get_timezone("UTC")).astimezone(get_timezone(time_zone))
2733

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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
320320
testproject_path = setup_clean_mkdocs_folder(mkdocs_yml_path=mkdocs_yml_file, output_path=temp_path)
321321
setup_commit_history(testproject_path)
322322
result = build_docs_setup(testproject_path)
323-
assert result.exit_code == 0, f"'mkdocs build' command failed with output:\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'"
323+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f {mkdocs_yml_file}'"
324324

325325
# validate build with locale retrieved from mkdocs config file
326326
validate_build(testproject_path, plugin_config=get_plugin_config_from_mkdocs(mkdocs_yml_file))
@@ -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"
@@ -637,7 +638,6 @@ def test_low_fetch_depth(tmp_path, caplog):
637638
assert "Running on GitHub Actions might" in caplog.text
638639

639640

640-
@pytest.mark.skip(reason="waiting for PR from mkdocs-genfiles-plugin to be merged first")
641641
def test_mkdocs_genfiles_plugin(tmp_path):
642642
"""
643643
Make sure the mkdocs-gen-files plugin works correctly.
@@ -647,7 +647,7 @@ def test_mkdocs_genfiles_plugin(tmp_path):
647647
)
648648
setup_commit_history(testproject_path)
649649
result = build_docs_setup(testproject_path)
650-
assert result.exit_code == 0, f"'mkdocs build' command failed with {result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'"
650+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f tests/fixtures/mkdocs-gen-files/mkdocs.yml'"
651651

652652
# validate the build
653653
plugin_config = get_plugin_config_from_mkdocs(str(testproject_path / "mkdocs.yml"))
@@ -691,22 +691,22 @@ def test_monorepo_compat(tmp_path):
691691
testproject_path = setup_clean_mkdocs_folder("tests/fixtures/monorepo/mkdocs.yml", tmp_path)
692692
setup_commit_history(testproject_path)
693693
result = build_docs_setup(testproject_path)
694-
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'"
694+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f tests/fixtures/monorepo/mkdocs.yml'"
695695

696696
@pytest.mark.skipif(sys.platform.startswith("win"), reason="monorepo plugin did not work for me on windows (even without this plugin)")
697697
def test_monorepo_compat_reverse_order(tmp_path):
698698
testproject_path = setup_clean_mkdocs_folder("tests/fixtures/monorepo/mkdocs_reverse_order.yml", tmp_path)
699699
setup_commit_history(testproject_path)
700700
result = build_docs_setup(testproject_path)
701-
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'"
701+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f tests/fixtures/monorepo/mkdocs_reverse_order.yml'"
702702

703703

704704
def test_genfiles_plugin(tmp_path):
705705
testproject_path = setup_clean_mkdocs_folder("tests/fixtures/basic_project/mkdocs_plugin_genfiles.yml", tmp_path)
706706
setup_commit_history(testproject_path)
707707

708708
result = build_docs_setup(testproject_path)
709-
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'"
709+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f tests/fixtures/basic_project/mkdocs_plugin_genfiles.yml'"
710710

711711
page_with_tag = testproject_path / "site/foo/index.html"
712712
contents = page_with_tag.read_text(encoding="utf8")

0 commit comments

Comments
 (0)