Skip to content

Commit

Permalink
refactor: Use yaml to mark languages as rtl (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenverCoder1 committed Oct 14, 2022
1 parent 6f52f06 commit d129d99
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
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"

1 comment on commit d129d99

@vercel
Copy link

@vercel vercel bot commented on d129d99 Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.