Skip to content

Commit

Permalink
multi language support wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Extravi committed Oct 11, 2023
1 parent cf5eb74 commit 621d166
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 54 deletions.
30 changes: 29 additions & 1 deletion __init__.py
Expand Up @@ -29,9 +29,18 @@ def settings():
new_tab = request.cookies.get('new_tab')
domain = request.cookies.get('domain')
javascript = request.cookies.get('javascript', 'enabled')

# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

return render_template('settings.html',
theme=theme,
lang=lang,
ux_lang=ux_lang,
lang_data=lang_data,
safe=safe,
new_tab=new_tab,
domain=domain,
Expand All @@ -47,9 +56,18 @@ def discover():
# default theme if none is set
theme = request.cookies.get('theme', DEFAULT_THEME)
javascript = request.cookies.get('javascript', 'enabled')

# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

return render_template('discover.html',
theme=theme,
javascript=javascript,
ux_lang=ux_lang,
lang_data=lang_data,
commit=COMMIT,
repo_url=REPO,
current_url=request.url,
Expand All @@ -62,6 +80,7 @@ def save_settings():
# get the selected theme from the form
theme = request.form.get('theme')
lang = request.form.get('lang')
ux_lang = request.form.get('ux_lang')
safe = request.form.get('safe')
new_tab = request.form.get('new_tab')
domain = request.form.get('domain')
Expand All @@ -80,6 +99,8 @@ def save_settings():
response.set_cookie('theme', theme, max_age=COOKIE_AGE, httponly=True, secure=app.config.get("HTTPS"))
if lang is not None and javascript == "":
response.set_cookie('lang', lang, max_age=COOKIE_AGE, httponly=True, secure=app.config.get("HTTPS"))
if ux_lang is not None and javascript == "":
response.set_cookie('ux_lang', ux_lang, max_age=COOKIE_AGE, httponly=True, secure=app.config.get("HTTPS"))
if new_tab is not None and javascript == "":
response.set_cookie('new_tab', new_tab, max_age=COOKIE_AGE, httponly=True, secure=app.config.get("HTTPS"))
response.headers["Location"] = past_location
Expand Down Expand Up @@ -147,6 +168,13 @@ def img_proxy():
def search():
lang = request.cookies.get('lang')
domain = request.cookies.get('domain')

# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

if request.method == "GET":
# get the `q` query parameter from the URL
query = request.args.get("q", "").strip()
Expand All @@ -158,7 +186,7 @@ def search():
return render_template("search.html", theme = request.cookies.get('theme', DEFAULT_THEME),
javascript=request.cookies.get('javascript', 'enabled'), DEFAULT_THEME=DEFAULT_THEME,
css_style=css_style, repo_url=REPO, commit=COMMIT, API_ENABLED=API_ENABLED,
lang=lang, domain=domain)
lang=lang, domain=domain, lang_data=lang_data, ux_lang=ux_lang)

# Check if the query has a bang.
if query.startswith(BANG):
Expand Down
12 changes: 10 additions & 2 deletions src/images.py
Expand Up @@ -3,11 +3,18 @@
from _config import *
from flask import request, render_template, jsonify, Response, redirect
import time
import json
from urllib.parse import quote
import base64


def imageResults(query) -> Response:
# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

# remember time we started
start_time = time.time()

Expand Down Expand Up @@ -59,8 +66,9 @@ def imageResults(query) -> Response:
return jsonify(results)
else:
return render_template("images.html", results=results, title=f"{query} - Araa",
q=f"{query}", fetched=f"Fetched the results in {elapsed_time:.2f} seconds",
q=f"{query}", fetched=f"{elapsed_time:.2f}",
theme=request.cookies.get('theme', DEFAULT_THEME), DEFAULT_THEME=DEFAULT_THEME,
javascript=request.cookies.get('javascript', 'enabled'), type="image",
new_tab=request.cookies.get("new_tab"), repo_url=REPO, API_ENABLED=API_ENABLED,
TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, commit=latest_commit())
TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, ux_lang=ux_lang, lang_data=lang_data,
commit=latest_commit())
10 changes: 8 additions & 2 deletions src/textResults.py
Expand Up @@ -9,6 +9,12 @@


def textResults(query) -> Response:
# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

# remember time we started
start_time = time.time()

Expand Down Expand Up @@ -205,13 +211,13 @@ def textResults(query) -> Response:
type = "text"
return render_template("results.html",
results=results, sublink=sublink, p=p, title=f"{query} - Araa",
q=f"{query}", fetched=f"Fetched the results in {elapsed_time:.2f} seconds",
q=f"{query}", fetched=f"{elapsed_time:.2f}",
snip=f"{snip}", kno_rdesc=f"{kno}", rdesc_link=f"{unquote(kno_link)}",
kno_wiki=f"{kno_image}", rkno_title=f"{rkno_title}", kno_title=f"{kno_title}",
user_info=f"{info}", calc=f"{calc}", check=check, current_url=current_url,
theme=request.cookies.get('theme', DEFAULT_THEME), new_tab=request.cookies.get("new_tab"),
javascript=request.cookies.get('javascript', 'enabled'), DEFAULT_THEME=DEFAULT_THEME,
type=type, search_type=search_type, repo_url=REPO, lang=lang, safe=safe, commit=latest_commit(),
exported_math_expression=exported_math_expression, API_ENABLED=API_ENABLED,
TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, domain=domain
TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, domain=domain, ux_lang=ux_lang, lang_data=lang_data
)
Binary file not shown.
Binary file not shown.
12 changes: 10 additions & 2 deletions src/torrents.py
@@ -1,4 +1,5 @@
import time
import json
from src.helpers import latest_commit
from _config import *
from flask import request, render_template, jsonify, Response
Expand All @@ -8,6 +9,12 @@ def torrentResults(query) -> Response:
if not TORRENTSEARCH_ENABLED:
return jsonify({"error": "Torrent search disabled by instance operator"}), 503

# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

# remember time we started
start_time = time.time()

Expand Down Expand Up @@ -37,9 +44,10 @@ def torrentResults(query) -> Response:

return render_template("torrents.html",
results=results, title=f"{query} - Araa",
q=f"{query}", fetched=f"Fetched the results in {elapsed_time:.2f} seconds",
q=f"{query}", fetched=f"{elapsed_time:.2f}",
theme=request.cookies.get('theme', DEFAULT_THEME), DEFAULT_THEME=DEFAULT_THEME,
javascript=request.cookies.get('javascript', 'enabled'), type="torrent",
repo_url=REPO, API_ENABLED=API_ENABLED, TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED,
repo_url=REPO, API_ENABLED=API_ENABLED, TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED,
ux_lang=ux_lang, lang_data=lang_data,
commit=latest_commit()
)
12 changes: 9 additions & 3 deletions src/video.py
Expand Up @@ -8,6 +8,12 @@


def videoResults(query) -> Response:
# get user language settings
ux_lang = request.cookies.get('ux_lang', 'english')
json_path = f'static/lang/{ux_lang}.json'
with open(json_path, 'r') as file:
lang_data = json.load(file)

# remember time we started
start_time = time.time()

Expand Down Expand Up @@ -63,9 +69,9 @@ def videoResults(query) -> Response:
else:
return render_template("videos.html",
results=results, title=f"{query} - Araa",
q=f"{query}", fetched=f"Fetched the results in {elapsed_time:.2f} seconds",
q=f"{query}", fetched=f"{elapsed_time:.2f}",
theme=request.cookies.get('theme', DEFAULT_THEME), DEFAULT_THEME=DEFAULT_THEME,
javascript=request.cookies.get('javascript', 'enabled'), new_tab=request.cookies.get("new_tab"),
type="video", repo_url=REPO, API_ENABLED=API_ENABLED, TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED,
commit=latest_commit()
type="video", repo_url=REPO, API_ENABLED=API_ENABLED, TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, ux_lang=ux_lang,
lang_data=lang_data, commit=latest_commit()
)
4 changes: 2 additions & 2 deletions templates/discover.html
@@ -1,12 +1,12 @@
{% extends "preresults_layout.html" %}

{% block body %}
<div class="settings-nav"><h1 class="logomobile"><a class="no-decoration" href="/settings">Return to settings</a></h1></div>
<div class="settings-nav"><h1 class="logomobile"><a class="no-decoration" href="/settings">{{ lang_data.settings.return_to_settings }}</a></h1></div>
<form action="/save-settings" method="post">
<input type="hidden" name="past" value="{{ current_url }}"></input>
<div class="settings">
<div class="settings-row">
<span class="highlight"><p>Discover Themes</p></span>
<span class="highlight"><p>{{ lang_data.settings.discover_themes_button }}</p></span>
</div>
<div>
<div class="theme-settings">
Expand Down
2 changes: 1 addition & 1 deletion templates/images.html
@@ -1,7 +1,7 @@
{% extends "results_layout.html" %}

{% block body %}
<p class="fetched fetched_dif fetched_img">{{ fetched }}</p>
<p class="fetched fetched_dif fetched_img">{{ lang_data.results.results }} {{ fetched }} {{ lang_data.results.seconds }}</p>
{% if results %}
<div class="images images_viewer_hidden">
<div class="image_view image_hide">
Expand Down
10 changes: 5 additions & 5 deletions templates/preresults_layout.html
Expand Up @@ -28,14 +28,14 @@
{% block body %}{% endblock %}
<div id="search_footer" class="footer">
<a href="/">Araa</a>
<a href="/settings">Settings</a>
<a target="_blank" href="{{ repo_url }}">Source code <span class="emoji-code">❤️</span></a>
<a href="/settings">{{ lang_data.footer.settings }}</a>
<a target="_blank" href="{{ repo_url }}">{{ lang_data.footer.source_code }} <span class="emoji-code">❤️</span></a>
{% if API_ENABLED == True %}
<a target="_blank" class="mobile-none" href="/api?q=firefox&t=text&api=true">API</a>
<a target="_blank" class="mobile-none" href="/suggestions?q=firefox">Suggestions API</a>
<a target="_blank" class="mobile-none" href="/api?q=firefox&t=text&api=true">{{ lang_data.api_links.api_link }}</a>
<a target="_blank" class="mobile-none" href="/suggestions?q=firefox">{{ lang_data.api_links.suggestions_api_link }}</a>
{% else %}
{% endif %}
<a target="_blank" class="mobile-none" href="{{ repo_url }}/commit/{{ commit }}">Commit: {{ commit }}</a>
<a target="_blank" class="mobile-none" href="{{ repo_url }}/commit/{{ commit }}">{{ lang_data.footer.commit }}: {{ commit }}</a>
</div>
</body>

Expand Down
2 changes: 1 addition & 1 deletion templates/results.html
Expand Up @@ -56,7 +56,7 @@
<option value="lang_vi" {% if lang == 'lang_vi' %}selected{% endif %}>Tiếng Việt (Vietnamese)</option>
</select>
</div>
<p class="fetched">{{ fetched }}</p>
<p class="fetched">{{ lang_data.results.results }} {{ fetched }} {{ lang_data.results.seconds }}</p>
{% if check == "" %}
{% else %}
<div class="check"><p>Did you mean: <a href="/search?q={{ check }}&t={{ search_type }}"><h3>{{ check }}</h3></a></p></div>
Expand Down
36 changes: 18 additions & 18 deletions templates/results_layout.html
Expand Up @@ -29,11 +29,11 @@
<button class="material-icons-round clickable settings-icon-link settings-icon-link-search">menu</button>
</div>
<div class="search-menu settings-menu-hidden">
<h2>Settings</h2>
<h2>{{ lang_data.settings.settings_header }}</h2>
<div class="settings-content">
<button id="settingsButton">All settings</button>
<button id="settingsButton">{{ lang_data.settings.all_settings }}</button>
<div class="theme-settings">
<p><span class="highlight">Theme: </span> <span id="theme_name">User Theme</span></p>
<p><span class="highlight">{{ lang_data.settings.theme }}: </span> <span id="theme_name">{{ lang_data.settings.user_theme }}</span></p>
<div class="themes-settings-menu">
<div><img class="view-image-search clickable" id="dark_blur" alt="Dark (Default)" src="/themepreview/preview1.webp"></div>
<div><img class="view-image-search clickable" id="dark_default" alt="Dark (no background)" src="/themepreview/preview2.webp"></div>
Expand Down Expand Up @@ -118,57 +118,57 @@ <h1 class="logomobile"><a class="no-decoration" href="./">Araa</a></h1>
{% if type == "text" %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable search-active" name="t" value="text">search</button>
<button name="t" value="text" class="clickable search-active">Web</button>
<button name="t" value="text" class="clickable search-active">{{ lang_data.buttons.text }}</button>
</div>
{% else %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="text">search</button>
<button name="t" value="text" class="clickable">Web</button>
<button name="t" value="text" class="clickable">{{ lang_data.buttons.text }}</button>
</div>
{% endif %}
{% if type == "image" %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable search-active" name="t" value="image">image</button>
<button name="t" value="image" class="clickable search-active">Images</button>
<button name="t" value="image" class="clickable search-active">{{ lang_data.buttons.image }}</button>
</div>
{% else %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="image">image</button>
<button name="t" value="image" class="clickable">Images</button>
<button name="t" value="image" class="clickable">{{ lang_data.buttons.image }}</button>
</div>
{% endif %}
{% if type == "video" %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable search-active" name="t" value="video">movie</button>
<button name="t" value="video" class="clickable search-active">Videos</button>
<button name="t" value="video" class="clickable search-active">{{ lang_data.buttons.video }}</button>
</div>
{% else %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="video">movie</button>
<button name="t" value="video" class="clickable">Videos</button>
<button name="t" value="video" class="clickable">{{ lang_data.buttons.video }}</button>
</div>
{% endif %}
{% if type == "reddit" %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable search-active" name="t" value="reddit">forum</button>
<button name="t" value="reddit" class="clickable search-active">Reddit</button>
<button name="t" value="reddit" class="clickable search-active">{{ lang_data.buttons.reddit }}</button>
</div>
{% else %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="reddit">forum</button>
<button name="t" value="reddit" class="clickable">Reddit</button>
<button name="t" value="reddit" class="clickable">{{ lang_data.buttons.reddit }}</button>
</div>
{% endif %}
{% if TORRENTSEARCH_ENABLED == True %}
{% if type == "torrent" %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable search-active" name="t" value="torrent">share</button>
<button name="t" value="torrent" class="clickable search-active">Torrents</button>
<button name="t" value="torrent" class="clickable search-active">{{ lang_data.buttons.torrent }}</button>
</div>
{% else %}
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="torrent">share</button>
<button name="t" value="torrent" class="clickable">Torrents</button>
<button name="t" value="torrent" class="clickable">{{ lang_data.buttons.torrent }}</button>
</div>
{% endif %}
{% else %}
Expand All @@ -189,14 +189,14 @@ <h1 class="logomobile"><a class="no-decoration" href="./">Araa</a></h1>
{% block body %}{% endblock %}
<div class="footer">
<a href="/">Araa</a>
<a href="/settings">Settings</a>
<a target="_blank" href="{{ repo_url }}">Source code <span class="emoji-code">❤️</span></a>
<a href="/settings">{{ lang_data.footer.settings }}</a>
<a target="_blank" href="{{ repo_url }}">{{ lang_data.footer.source_code }} <span class="emoji-code">❤️</span></a>
{% if API_ENABLED == True %}
<a target="_blank" class="mobile-none" href="/api?q=firefox&t=text&api=true">API</a>
<a target="_blank" class="mobile-none" href="/suggestions?q=firefox">Suggestions API</a>
<a target="_blank" class="mobile-none" href="/api?q=firefox&t=text&api=true">{{ lang_data.api_links.api_link }}</a>
<a target="_blank" class="mobile-none" href="/suggestions?q=firefox">{{ lang_data.api_links.suggestions_api_link }}</a>
{% else %}
{% endif %}
<a target="_blank" class="mobile-none" href="{{ repo_url }}/commit/{{ commit }}">Commit: {{ commit }}</a>
<a target="_blank" class="mobile-none" href="{{ repo_url }}/commit/{{ commit }}">{{ lang_data.footer.commit }}: {{ commit }}</a>
</div>
</body>

Expand Down

0 comments on commit 621d166

Please sign in to comment.