Skip to content

Commit

Permalink
Leave constants as global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ollimeier committed Oct 23, 2024
1 parent f42a1c5 commit 81337ec
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions scripts/rebuild_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
import subprocess
from urllib.request import urlopen


def prettier(path, repoDir):
repoDir = pathlib.Path(__file__).resolve().parent.parent
langDir = repoDir / "src" / "fontra" / "client" / "lang"
assert langDir.is_dir()
localizationJSPath = repoDir / "src" / "fontra" / "client" / "core" / "localization.js"
assert localizationJSPath.is_file()

languagesBlockTemplate = """\
// Don't edit this block, see scripts/rebuild_languages.py
export const languages = [
{languagesList}
];
"""

languageSourceTemplate = """\
// Don't edit this file: it is generated by scripts/rebuild_languages.py
// The strings are maintained here: {url}
export const strings = {{
{stringsBlock}
}};
"""


def prettier(path):

subprocess.run(
[
Expand Down Expand Up @@ -90,22 +111,10 @@ def main(wip):
string = languageStrings["en"].get(key, "!missing!")
strings[key] = string

repoDir = pathlib.Path(__file__).resolve().parent.parent
langDir = repoDir / "src" / "fontra" / "client" / "lang"
assert langDir.is_dir()
localizationJSPath = (
repoDir / "src" / "fontra" / "client" / "core" / "localization.js"
)
assert localizationJSPath.is_file()

localizationJSSource = localizationJSPath.read_text(encoding="utf-8")

languagesList = "\n".join(f" {jsonDump(langs)}," for langs in languages)
languagesBlock = f"""// Don't edit this block, see scripts/rebuild_languages.py
export const languages = [
{languagesList}
];
"""
languagesBlock = languagesBlockTemplate.format(languagesList=languagesList)

indexStart = localizationJSSource.find(languagesBlock.splitlines()[0])
assert indexStart > 0
Expand All @@ -119,15 +128,7 @@ def main(wip):
)

localizationJSPath.write_text(localizationJSSource, encoding="utf-8")
prettier(localizationJSPath, repoDir)

languageSourceTemplate = """\
// Don't edit this file: it is generated by scripts/rebuild_languages.py
// The strings are maintained here: {url}
export const strings = {{
{stringsBlock}
}};
"""
prettier(localizationJSPath)

for languageCode, strings in languageStrings.items():
languagePath = langDir / f"{languageCode.strip()}.js"
Expand All @@ -140,7 +141,7 @@ def main(wip):
stringsBlock=stringsBlock, url=languageSpreadsheetURL
)
languagePath.write_text(languageSource, encoding="utf-8")
prettier(languagePath, repoDir)
prettier(languagePath)


if __name__ == "__main__":
Expand Down

0 comments on commit 81337ec

Please sign in to comment.