diff --git a/docs/howto/specify-locale.md b/docs/howto/specify-locale.md index 1123898..a23c039 100644 --- a/docs/howto/specify-locale.md +++ b/docs/howto/specify-locale.md @@ -1,6 +1,6 @@ # Specify a locale -`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. +`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. For example: diff --git a/pyproject.toml b/pyproject.toml index d8bcaf8..760b3af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -111,8 +111,8 @@ target-version = "py310" # Always autofix fix = true -[tool.uv] -dev-dependencies = [ +[dependency-groups] +dev = [ "click>=8.1.8", "codecov>=2.1.13", "mkdocs-gen-files>=0.5.0", diff --git a/src/mkdocs_git_revision_date_localized_plugin/dates.py b/src/mkdocs_git_revision_date_localized_plugin/dates.py index f27e6fe..fa2c108 100644 --- a/src/mkdocs_git_revision_date_localized_plugin/dates.py +++ b/src/mkdocs_git_revision_date_localized_plugin/dates.py @@ -22,6 +22,12 @@ def get_date_formats( assert time_zone is not None assert locale is not None + # Normalize locale for Babel: convert hyphens to underscores (e.g., pt-BR -> pt_BR) + # This is needed for compatibility with mkdocs-static-i18n and other plugins + # that may use hyphenated locale codes, while Babel expects underscores + if locale: + locale = str(locale).replace("-", "_") + utc_revision_date = datetime.fromtimestamp(int(unix_timestamp), tz=timezone.utc) loc_revision_date = utc_revision_date.replace(tzinfo=get_timezone("UTC")).astimezone(get_timezone(time_zone)) diff --git a/tests/fixtures/basic_project/mkdocs_theme_locale_and_language_5char.yml b/tests/fixtures/basic_project/mkdocs_theme_locale_and_language_5char.yml new file mode 100644 index 0000000..9fd0d8f --- /dev/null +++ b/tests/fixtures/basic_project/mkdocs_theme_locale_and_language_5char.yml @@ -0,0 +1,11 @@ +site_name: test gitrevisiondatelocalized_plugin +use_directory_urls: true + +theme: + name: 'material' + language: zh + +plugins: + - search + - git-revision-date-localized: + locale: "pt-BR" diff --git a/tests/test_builds.py b/tests/test_builds.py index 310727c..f7848ab 100644 --- a/tests/test_builds.py +++ b/tests/test_builds.py @@ -320,7 +320,7 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str): testproject_path = setup_clean_mkdocs_folder(mkdocs_yml_path=mkdocs_yml_file, output_path=temp_path) setup_commit_history(testproject_path) result = build_docs_setup(testproject_path) - assert result.exit_code == 0, f"'mkdocs build' command failed with output:\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'" + assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'uv run mkdocs serve -f {mkdocs_yml_file}'" # validate build with locale retrieved from mkdocs config file 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): "basic_project/mkdocs.yml", "basic_project/mkdocs_theme_timeago_locale.yml", "basic_project/mkdocs_theme_language.yml", + "basic_project/mkdocs_theme_locale_and_language_5char.yml", "basic_project/mkdocs_theme_locale_and_language.yml", "basic_project/mkdocs_theme_locale_disabled.yml", "basic_project/mkdocs_theme_timeago.yml", @@ -376,7 +377,7 @@ def test_tags_are_replaced(tmp_path, mkdocs_file): testproject_path = setup_clean_mkdocs_folder(mkdocs_yml_path=f"tests/fixtures/{mkdocs_file}", output_path=tmp_path) setup_commit_history(testproject_path) result = build_docs_setup(testproject_path) - assert result.exit_code == 0, f"'mkdocs build' command failed with:\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'" + 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}'" plugin_config = get_plugin_config_from_mkdocs(str(testproject_path / "mkdocs.yml")) tags_file = testproject_path / "site/page_with_tag/index.html" @@ -637,7 +638,6 @@ def test_low_fetch_depth(tmp_path, caplog): assert "Running on GitHub Actions might" in caplog.text -@pytest.mark.skip(reason="waiting for PR from mkdocs-genfiles-plugin to be merged first") def test_mkdocs_genfiles_plugin(tmp_path): """ Make sure the mkdocs-gen-files plugin works correctly. @@ -647,7 +647,7 @@ def test_mkdocs_genfiles_plugin(tmp_path): ) setup_commit_history(testproject_path) result = build_docs_setup(testproject_path) - assert result.exit_code == 0, f"'mkdocs build' command failed with {result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'" + 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'" # validate the build plugin_config = get_plugin_config_from_mkdocs(str(testproject_path / "mkdocs.yml")) @@ -691,14 +691,14 @@ def test_monorepo_compat(tmp_path): testproject_path = setup_clean_mkdocs_folder("tests/fixtures/monorepo/mkdocs.yml", tmp_path) setup_commit_history(testproject_path) result = build_docs_setup(testproject_path) - assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'" + 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'" @pytest.mark.skipif(sys.platform.startswith("win"), reason="monorepo plugin did not work for me on windows (even without this plugin)") def test_monorepo_compat_reverse_order(tmp_path): testproject_path = setup_clean_mkdocs_folder("tests/fixtures/monorepo/mkdocs_reverse_order.yml", tmp_path) setup_commit_history(testproject_path) result = build_docs_setup(testproject_path) - assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'" + 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'" def test_genfiles_plugin(tmp_path): @@ -706,7 +706,7 @@ def test_genfiles_plugin(tmp_path): setup_commit_history(testproject_path) result = build_docs_setup(testproject_path) - assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}\nReplicate with 'cd {testproject_path} && uv run mkdocs build'" + 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'" page_with_tag = testproject_path / "site/foo/index.html" contents = page_with_tag.read_text(encoding="utf8")