From 99e7053558437a393c78241f5c57bea4d03e9352 Mon Sep 17 00:00:00 2001 From: GeoJulien Date: Wed, 6 Dec 2023 18:46:11 +0100 Subject: [PATCH] fix: handle locale or without territory #176 --- mkdocs_rss_plugin/util.py | 6 ++++- .../mkdocs_lang_without_territory.yml | 12 +++++++++ tests/test_build.py | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/mkdocs_lang_without_territory.yml diff --git a/mkdocs_rss_plugin/util.py b/mkdocs_rss_plugin/util.py index 1e4c6255..707e99c3 100644 --- a/mkdocs_rss_plugin/util.py +++ b/mkdocs_rss_plugin/util.py @@ -629,7 +629,11 @@ def guess_locale(mkdocs_config: Config) -> str or None: "[rss plugin] Locale detected in theme " f"('{mkdocs_config.theme.name}') settings: {locale=}" ) - return f"{locale}" + return ( + f"{locale.language}-{locale.territory}" + if locale.territory + else f"{locale.language}" + ) elif "language" in mkdocs_config.theme: logger.debug( "[rss plugin] Language detected in theme " diff --git a/tests/fixtures/mkdocs_lang_without_territory.yml b/tests/fixtures/mkdocs_lang_without_territory.yml new file mode 100644 index 00000000..102ac9f0 --- /dev/null +++ b/tests/fixtures/mkdocs_lang_without_territory.yml @@ -0,0 +1,12 @@ +site_name: Test RSS Plugin +site_description: Test a language code with territory +site_url: https://guts.github.io/mkdocs-rss-plugin + +use_directory_urls: true + +plugins: + - rss + +theme: + name: mkdocs + locale: fr diff --git a/tests/test_build.py b/tests/test_build.py index 239c412d..342fd1a8 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -356,6 +356,32 @@ def test_simple_build_lang_with_territory(self): feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml") self.assertEqual(feed_parsed.feed.get("language"), "en-US") + def test_simple_build_lang_without_territory(self): + with tempfile.TemporaryDirectory() as tmpdirname: + cli_result = self.build_docs_setup( + testproject_path="docs", + mkdocs_yml_filepath=Path( + "tests/fixtures/mkdocs_lang_without_territory.yml" + ), + output_path=tmpdirname, + strict=True, + ) + + if cli_result.exception is not None: + e = cli_result.exception + logger.debug(format_exception(type(e), e, e.__traceback__)) + + self.assertEqual(cli_result.exit_code, 0) + self.assertIsNone(cli_result.exception) + + # created items + feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml") + self.assertEqual(feed_parsed.feed.get("language"), "fr") + + # updated items + feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml") + self.assertEqual(feed_parsed.feed.get("language"), "fr") + def test_simple_build_pretty_print_enabled(self): with tempfile.TemporaryDirectory() as tmpdirname: cli_result = self.build_docs_setup(