feat: improve language and project handling

- Default values for `lang` and `project` added in `render_template` to ensure consistent rendering parameters.
- Included `lang` and `project` parameters in error and article templates for better context in error messages.
- Enhanced search form in base template to persist selected `lang` and `project` improving user experience.

These changes provide a more consistent and localized user experience by maintaining language and project context across different parts of the application.

Fixes #17.
This commit is contained in:
Kumi 2024-08-20 07:25:07 +02:00
parent f91ccba437
commit 1e31998f50
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 11 additions and 3 deletions

View file

@ -92,6 +92,9 @@ def render_template(*args, **kwargs) -> Text:
Returns: Returns:
Text: The rendered template. Text: The rendered template.
""" """
kwargs.setdefault("lang", "en")
kwargs.setdefault("project", "wiki")
return flask_render_template( return flask_render_template(
*args, *args,
**kwargs, **kwargs,
@ -237,6 +240,8 @@ def wiki_article(
"article.html", "article.html",
title="Article not found", title="Article not found",
content=f"Sorry, the article {title} was not found in the {project} project in the {lang} language.", content=f"Sorry, the article {title} was not found in the {project} project in the {lang} language.",
lang=lang,
project=project,
), ),
404, 404,
) )
@ -247,6 +252,8 @@ def wiki_article(
"article.html", "article.html",
title="Error", title="Error",
content=f"An error occurred while fetching the article {title} from the {project} project in the {lang} language.", content=f"An error occurred while fetching the article {title} from the {project} project in the {lang} language.",
lang=lang,
project=project,
), ),
500, 500,
) )
@ -274,7 +281,6 @@ def wiki_article(
a["href"] = f"/{project}/{lang}{href}" a["href"] = f"/{project}/{lang}{href}"
elif href.startswith("//") or href.startswith("https://"): elif href.startswith("//") or href.startswith("https://"):
print(f"Checking {href}")
parts = urlparse(href) parts = urlparse(href)
target_domain = f"https://{parts.netloc}" target_domain = f"https://{parts.netloc}"
@ -337,6 +343,8 @@ def wiki_article(
"article.html", "article.html",
title=title.replace("_", " "), title=title.replace("_", " "),
content=processed_html, content=processed_html,
lang=lang,
project=project,
rtl=rtl, rtl=rtl,
) )

View file

@ -12,12 +12,12 @@
<form id="search-form" action="{{ url_for('search') }}" method="post"> <form id="search-form" action="{{ url_for('search') }}" method="post">
<select name="project" id="project"> <select name="project" id="project">
{% for key, value in wikimedia_projects.items() %} {% for key, value in wikimedia_projects.items() %}
<option value="{{ key }}">{{ value }}</option> <option {% if key == project %}selected="selected"{% endif %} value="{{ key }}">{{ value }}</option>
{% endfor %} {% endfor %}
</select> </select>
<select name="lang" id="lang"> <select name="lang" id="lang">
{% for key, value in languages.items() %} {% for key, value in languages.items() %}
<option {% if key == "en" %}selected="selected"{% endif %} value="{{ key }}">{{ value["name"] }}</option> <option {% if key == lang %}selected="selected"{% endif %} value="{{ key }}">{{ value["name"] }}</option>
{% endfor %} {% endfor %}
</select> </select>
<input type="text" name="query" id="query" placeholder="Search Wikipedia"> <input type="text" name="query" id="query" placeholder="Search Wikipedia">