Skip to content

Commit

Permalink
feat: Root page refers language registered from cookie
Browse files Browse the repository at this point in the history
Refs: #1
  • Loading branch information
attakei committed May 14, 2024
1 parent 688b975 commit 93bf728
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/atsphinx/mini18n/templates/mini18n/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
<body>
<script>
const url = new URL(window.location.href);
const default_language = '{{ config.mini18n_default_language }}';
const targets = [
'{{ config.mini18n_basepath }}',
'{{ config.mini18n_basepath }}/index.html',
];
if (targets.includes(url.pathname)) {
url.pathname = '{{ config.mini18n_basepath }}{{ config.mini18n_default_language }}/';
const fromCookie = document.cookie.split("; ").find(kv => kv.startsWith("lang="));
if (fromCookie) {
url.pathname = `{{ config.mini18n_basepath }}${fromCookie.split("=")[1]}/`;
} else {
url.pathname = '{{ config.mini18n_basepath }}{{ config.mini18n_default_language }}/';
}
location.href = url.href;
}
</script>
Expand Down
3 changes: 3 additions & 0 deletions tests/test_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ def test__select_language(app: SphinxTestApp, page: Page):
cookies = page.context.cookies()
assert cookies[0]["name"] == "lang"
assert cookies[0]["value"] == "ja"
page.goto(url)
time.sleep(0.5)
assert page.url.endswith("/ja/")

0 comments on commit 93bf728

Please sign in to comment.