Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use yaml to mark languages as rtl #52

Merged
merged 1 commit into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions api/index.py
Expand Up @@ -5,20 +5,21 @@
from flask.wrappers import Response

from .utils import (
data_uri_from_file,
data_uri_from_url,
estimate_duration_width,
fetch_views,
format_relative_time,
data_uri_from_url,
data_uri_from_file,
is_rtl,
seconds_to_duration,
trim_text,
)
from .validate import (
validate_color,
validate_int,
validate_lang,
validate_string,
validate_video_id,
validate_lang,
)

app = Flask(__name__)
Expand All @@ -38,7 +39,6 @@ def render():
publish_timestamp = validate_int(request, "timestamp", default=0)
duration_seconds = validate_int(request, "duration", default=0)
lang = validate_lang(request, "lang", default="en")
rtl = lang in ["ar", "he"]
video_id = validate_video_id(request, "id")
thumbnail = data_uri_from_url(f"https://i.ytimg.com/vi/{video_id}/mqdefault.jpg")
views = fetch_views(video_id)
Expand All @@ -63,7 +63,7 @@ def render():
thumbnail=thumbnail,
duration=duration,
duration_width=duration_width,
rtl=rtl,
rtl=is_rtl(lang),
),
status=200,
mimetype="image/svg+xml",
Expand Down
1 change: 1 addition & 0 deletions api/locale/he.yml
@@ -1,4 +1,5 @@
he:
direction: rtl
views: "%{number} צפיות"
seconds-ago:
one: "לפני שניה"
Expand Down
5 changes: 5 additions & 0 deletions api/utils.py
Expand Up @@ -125,3 +125,8 @@ def estimate_duration_width(duration: str) -> int:
num_digits = len([c for c in duration if c.isdigit()])
num_colons = len([c for c in duration if c == ":"])
return num_digits * 7 + num_colons * 5 + 8


def is_rtl(lang: str) -> bool:
"""Check if language is to be displayed right-to-left"""
return i18n.t("direction", locale=lang, default="ltr") == "rtl"