Skip to content

Commit

Permalink
Add localization (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor authored Jul 21, 2024
1 parent 2dee388 commit 29a108e
Show file tree
Hide file tree
Showing 12 changed files with 943 additions and 587 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ coverage.xml
cover/

# Translations
*.mo
*.pot
# *.mo
# *.pot

# Django stuff:
*.log
Expand Down
27 changes: 14 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

from package_build.download_parts import download_parts
from package_build.file_list import show_file_list
from package_build.i18n import _
from package_build.metadata import get_dict_metadata, get_hook_metadata
from package_build.models import DictInfoEntry
from package_build.package import build_package, package_up_to_date
from package_build.parse_metadata import parse_metadata

st.header("DF localization package builder")
st.write("Build/download a package, unpack into the game's directory with file repalacement.")
st.header(_("DF localization package builder"))
st.write(_("Build/download a package, unpack into the game's directory with file repalacement."))

hook_metadata = parse_metadata(get_hook_metadata())
dict_metadata = get_dict_metadata()
df_version_options = hook_metadata.df_version_options

column1, column2 = st.columns(2)
with column1:
df_version: str = st.selectbox(label="DF version", options=sorted(df_version_options, reverse=True))
df_version: str = st.selectbox(label=_("DF version"), options=sorted(df_version_options, reverse=True))
operating_system: str = st.selectbox(
label="Operating system/platform",
label=_("Operating system/platform"),
options=df_version_options[df_version].operating_systems,
)

Expand All @@ -34,27 +35,27 @@


with column2:
df_variant: str = st.selectbox(label="DF variant", options=variants)
dict_entry: DictInfoEntry = st.selectbox(label="Language", options=dict_metadata)
df_variant: str = st.selectbox(label=_("DF variant"), options=variants)
dict_entry: DictInfoEntry = st.selectbox(label=_("Language"), options=dict_metadata)

hook_info = hook_metadata.hook_info.get((df_version, df_variant, operating_system))

root_dir = Path(__file__).parent

if not hook_info:
st.write("Cannot create package with these parameters")
st.write(_("Cannot create package with these parameters"))
else:
package_name = f"dfint_{df_version}_{df_variant}_{operating_system}_{dict_entry.code}.zip"
package_path = root_dir / package_name

if not package_up_to_date(package_path):
button_generate = st.button("Generate package")
button_generate = st.button(_("Generate package"))
if button_generate:
with st.status("Downloading files...", expanded=True) as status:
with st.status(_("Downloading files..."), expanded=True) as status:
parts = download_parts(hook_info, dict_entry)
status.update(label="Download complete!", state="complete", expanded=False)
status.update(label=_("Downloading complete!"), state="complete", expanded=False)

with st.status("Building package...", expanded=True) as status:
with st.status(_("Building package..."), expanded=True) as status:
build_dir = root_dir / "build"

build_package(
Expand All @@ -64,11 +65,11 @@
parts=parts,
is_win=operating_system.startswith("win"),
)
status.update(label="Package ready!", state="complete", expanded=False)
status.update(label=_("Package is ready!"), state="complete", expanded=False)

if package_up_to_date(package_path):
st.download_button(
label="Download package",
label=_("Download package"),
file_name=package_path.name,
data=package_path.read_bytes(),
mime="application/zip",
Expand Down
16 changes: 9 additions & 7 deletions package_build/download_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import requests
import streamlit as st

from package_build.i18n import _

from .models import DictInfoEntry, HookInfoEntry


Expand All @@ -24,25 +26,25 @@ class DownloadedParts(NamedTuple):

# @st.cache_data
def download_parts(hook_info: HookInfoEntry, dict_info: DictInfoEntry) -> DownloadedParts:
st.write("Download library...")
st.write(_("Downloading library..."))
library = download(hook_info.lib)

st.write("Download dfhooks library...")
st.write(_("Downloading dfhooks library..."))
dfhooks = download(hook_info.dfhooks)

st.write("Download config...")
st.write(_("Downloading config..."))
config = download(hook_info.config)

st.write("Download offseets...")
st.write(_("Downloading offsets..."))
offsets = download(hook_info.offsets)

st.write("Download csv dictionary...")
st.write(_("Downloading csv dictionary..."))
csv_file = download(dict_info.csv)

st.write("Download font file...")
st.write(_("Downloading font file..."))
font_file = download(dict_info.font)

st.write("Download encoding config...")
st.write(_("Downloading encoding config..."))
encoding_config = download(dict_info.encoding)

return DownloadedParts(
Expand Down
15 changes: 8 additions & 7 deletions package_build/file_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@

import streamlit as st

from package_build.i18n import _, ngettext
from package_build.package import get_file_modification_datetime, package_up_to_date


def show_file_list(root_dir: Path) -> None:
st.subheader("Package files awailable to download")
st.subheader(_("Package files awailable to download"))

file_list = [file for file in root_dir.glob("*.zip") if package_up_to_date(file)]

if not file_list:
st.write("No package files available.")
st.write(_("No package files available."))
return

column1, column2, column3 = st.columns([3, 2, 1], vertical_alignment="center")
column1.write("Package name")
column2.write("When created")
column1.write(_("Package name"))
column2.write(_("When created"))

for package_path in sorted(file_list):
column1, column2, column3 = st.columns([3, 2, 1], vertical_alignment="center")
column1.write(package_path.relative_to(root_dir).name)
hours_ago = (datetime.now(tz=timezone.utc) - get_file_modification_datetime(package_path)).seconds // 3600
if hours_ago == 0:
column2.write("less than an hour ago")
column2.write(_("less than an hour ago"))
else:
column2.write(f"{hours_ago} hours ago")
column2.write(ngettext("%(num)d hour ago", "%(num)d hours ago", hours_ago) % {"num": hours_ago})

column3.download_button(
label="Download",
label=_("Download"),
file_name=package_path.name,
data=package_path.read_bytes(),
mime="application/zip",
Expand Down
26 changes: 26 additions & 0 deletions package_build/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import gettext
import re
from pathlib import Path

from streamlit.web.server.websocket_headers import _get_websocket_headers


def get_preferred_languages() -> list[str]:
headers = _get_websocket_headers()
return re.findall(r"([a-zA-Z-]{2,})", headers["Accept-Language"]) or []


locale_dir = Path(__file__).parent / "locale"

languages = get_preferred_languages()

lang = gettext.translation(
"messages",
localedir=str(locale_dir),
languages=languages,
fallback=True,
)

lang.install()
_ = lang.gettext
ngettext = lang.ngettext
140 changes: 140 additions & 0 deletions package_build/locale/messages.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-20 21:46+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

#: app.py:13
msgid "DF localization package builder"
msgstr ""

#: app.py:14
msgid ""
"Build/download a package, unpack into the game's directory with file "
"repalacement."
msgstr ""

#: app.py:22
msgid "DF version"
msgstr ""

#: app.py:24
msgid "Operating system/platform"
msgstr ""

#: app.py:38
msgid "DF variant"
msgstr ""

#: app.py:39
msgid "Language"
msgstr ""

#: app.py:46
msgid "Cannot create package with these parameters"
msgstr ""

#: app.py:52
msgid "Generate package"
msgstr ""

#: app.py:54
msgid "Downloading files..."
msgstr ""

#: app.py:56
msgid "Downloading complete!"
msgstr ""

#: app.py:58
msgid "Building package..."
msgstr ""

#: app.py:68
msgid "Package is ready!"
msgstr ""

#: app.py:72
msgid "Download package"
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/metadata.py:22
msgid "Getting hook metadata..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/metadata.py:28
msgid "Getting dict metadata..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:29
msgid "Downloading library..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:32
msgid "Downloading dfhooks library..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:35
msgid "Downloading config..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:38
msgid "Downloading offsets..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:41
msgid "Downloading csv dictionary..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:44
msgid "Downloading font file..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/download_parts.py:47
msgid "Downloading encoding config..."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:11
msgid "Package files awailable to download"
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:14
msgid "No package files available."
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:18
msgid "Package name"
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:19
msgid "When created"
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:29
msgid "less than an hour ago"
msgstr ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:31
#, python-format
msgid "%(num)d hour ago"
msgid_plural "%(num)d hours ago"
msgstr[0] ""
msgstr[1] ""

#: /home/insolor/Projects/DwarfFortress/package-build/package_build/file_list.py:34
msgid "Download"
msgstr ""
Binary file added package_build/locale/ru/LC_MESSAGES/messages.mo
Binary file not shown.
Loading

0 comments on commit 29a108e

Please sign in to comment.