Skip to content

Commit

Permalink
Fixed #4: language name (native) on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
rgaudin committed Apr 22, 2024
1 parent 37e5705 commit 603fd2a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN \
&& /usr/local/bin/gen-home_env/bin/pip3 install --no-cache-dir -U pip \
&& /usr/local/bin/gen-home_env/bin/pip3 install \
--no-cache-dir \
Jinja2==3.1.2 PyYAML==6.0.1 humanfriendly==10.0 iso639-lang==2.2.3 libzim==3.4.0 \
Jinja2==3.1.2 PyYAML==6.0.1 humanfriendly==10.0 libzim==3.4.0 pycountry==23.12.11 \
# install tailwind CSS cli
&& curl -L -o /usr/local/bin/tailwindcss https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.3/tailwindcss-linux-arm64 \
&& chmod +x /usr/local/bin/tailwindcss \
Expand Down
42 changes: 37 additions & 5 deletions gen-home.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@

from __future__ import annotations

import collections
import gettext
import os
import pathlib
import re
import traceback
import urllib.parse

import humanfriendly
import iso639
import pycountry
import yaml
from jinja2 import Environment, FileSystemLoader, select_autoescape

Expand All @@ -32,6 +34,9 @@
# we don't NEED cython ext but it's faster so use it if avail.
from yaml import SafeLoader

LanguageDef = collections.namedtuple(
"LanguageDef", ["alpha_3", "alpha_2", "native", "english"]
)

src_dir = pathlib.Path(os.getenv("SRC_DIR", "/src")).expanduser().resolve()
packages_path = (
Expand Down Expand Up @@ -70,6 +75,33 @@ def round_to(size: int, scale: int) -> int:
return str(size)


def get_lang_def(alpha_3: str) -> LanguageDef:
"""LanguageDef tuple with parsed/prepared language info"""
try:
language = pycountry.languages.get(alpha_3=alpha_3)
if not language:
raise ValueError("")
except Exception:
return LanguageDef(alpha_3, alpha_3[:2], alpha_3, alpha_3)

try:
alpha_2 = language.alpha_2
except AttributeError:
alpha_2 = alpha_3[:2]

try:
translator = gettext.translation(
"iso639-3", pycountry.LOCALES_DIR, languages=[alpha_2]
)
native = translator.gettext(language.name).title()
except Exception:
native = language.name

return LanguageDef(
alpha_3=alpha_3, alpha_2=alpha_2, native=native, english=language.name
)


env.filters["fsize"] = format_fsize


Expand Down Expand Up @@ -166,8 +198,8 @@ def visible(self):
return False

@property
def langs(self) -> list[str]:
return [lang[:2] for lang in self.get("languages", [])]
def langs(self) -> list[LanguageDef]:
return [get_lang_def(lang) for lang in self.get("languages", [])]


def gen_home(fpath: pathlib.Path):
Expand All @@ -186,8 +218,8 @@ def gen_home(fpath: pathlib.Path):
context["languages"] = {}
context["categories"] = set()
for package in context["packages"]:
for lang in package.get("languages", []):
context["languages"][lang] = iso639.Lang(lang).name
for lang in package.langs:
context["languages"][lang.alpha_3] = lang.native
for tag in package.get("tags", []):
if tag.startswith("_category:"):
package["category"] = tag.split(":", 1)[-1]
Expand Down
5 changes: 3 additions & 2 deletions templates/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ <h2 class="flex-auto text-lg text-left">Kiwix Reader</h2>
"
data-target="{{ package.url }}"
data-category="{{ package.category }}"
data-langs="{% if package.langs %}{% for lang in package.languages %}{{ lang }} {% endfor %}{% endif %}"
data-langs="{% if package.langs %}{% for lang in package.langs %}{{ lang }} {% endfor %}{% endif %}"
data-name="{{ package.title }}"
data-size="{% if package.download and package.download.size %}{{ package.download.size }}{% else %}0{% endif %}">

Expand Down Expand Up @@ -267,7 +267,8 @@ <h2 class="flex-auto text-lg text-left">Kiwix Reader</h2>
{% if loop.index0 < 2 %}
<span
class="rounded-md border-kwbordergrey border-solid border text-center text-[0.7em] pt-[0.32em] pb-[0.32em] px-[0.3em] uppercase me-1 last:me-0"
>{{ lang }}</span>
title="{{ lang.native }}"
>{{ lang.alpha_2 }}</span>
{% endif %}
{% endfor %}
{% endif %}
Expand Down
6 changes: 3 additions & 3 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"
data-target="{{ package.url }}"
data-category="{{ package.category }}"
data-langs="{% if package.langs %}{% for lang in package.languages %}{{ lang }} {% endfor %}{% endif %}"
data-langs="{% if package.langs %}{% for lang in package.langs %}{{ lang.alpha_3 }} {% endfor %}{% endif %}"
data-name="{{ package.title }}"
data-size="{% if package.download and package.download.size %}{{ package.download.size }}{% else %}0{% endif %}"
>
Expand Down Expand Up @@ -147,7 +147,7 @@
">
{% for lang in package.langs %}
{% if loop.index0 <= 2 and lang != 'mu' %}
{% if loop.index0 <= 2 and lang.alpha_3 != 'mul' %}
<div class="
rounded-md
border-kwbordergrey
Expand All @@ -170,7 +170,7 @@
md:mb-1
md:last:mb-1
"><div class="text-[0.7em] pt-[0.1em] uppercase">{{ lang }}</div></div>
"><div class="text-[0.7em] pt-[0.1em] uppercase" title="{{ lang.native }}">{{ lang.alpha_2 }}</div></div>
{% endif %}
{% endfor %}
</div>
Expand Down

0 comments on commit 603fd2a

Please sign in to comment.