Skip to content

Commit

Permalink
feat: Save selected language into cookie
Browse files Browse the repository at this point in the history
Refs: #1
  • Loading branch information
attakei committed May 14, 2024
1 parent 73c8d23 commit 688b975
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
</style>
<div>
<label for="atsphinx-mini18n-select-lang">{{ mini18n.select_lang_label }}</label>
<select name="lang" onchange="location.href=value;" id="atsphinx-mini18n-select-lang">
<select name="lang" onchange="{
const values = value.split('@@');
document.cookie = `lang=${values[0]}; path=/; samesite=Strict; max-age=${60*60*24*400};`;
location.href=values[1];
}" id="atsphinx-mini18n-select-lang">
{%- for code in mini18n.support_languages %}
<option value="{{ pathto('../' + code + '/', 1) }}{{ pathto(pagename, 0, '.') }}" {%- if code==language %}
<option value="{{ code }}@@{{ pathto('../' + code + '/', 1) }}{{ pathto(pagename, 0, '.') }}" {%- if code==language %}
selected {%- endif %}>{{ code }}</option>
{%- endfor %}
</select>
Expand Down
3 changes: 3 additions & 0 deletions tests/test-e2e/conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# noqa: D100
from atsphinx.mini18n import get_template_dir

extensions = [
"atsphinx.mini18n",
]

html_sidebars = {"**": [f"{get_template_dir()}/mini18n/snippets/select-lang.html"]}

mini18n_default_language = "en"
mini18n_support_languages = ["en", "ja"]
21 changes: 21 additions & 0 deletions tests/test_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ def test__root(app: SphinxTestApp, page: Page):
page.goto(url)
time.sleep(0.5)
assert page.url.endswith("/en/")


@pytest.mark.sphinx("mini18n-html", testroot="e2e")
def test__select_language(app: SphinxTestApp, page: Page):
app.build()
with ServerOnPytest(app.outdir) as server:
url = f"http://localhost:{server.port}/"
page.goto(url)
time.sleep(0.5)
assert page.url.endswith("/en/")
assert not page.context.cookies()
element = page.get_by_label("Language:")
element.select_option(label="ja")
element.evaluate(
"elm => elm.dispatchEvent(new Event('change', {bubbles: true}))"
)
time.sleep(0.5)
assert page.url.endswith("/ja/index.html")
cookies = page.context.cookies()
assert cookies[0]["name"] == "lang"
assert cookies[0]["value"] == "ja"

0 comments on commit 688b975

Please sign in to comment.