-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1713 from googlefonts/language-spreadsheet
Move Fontra UI Strings to a public spreadsheet
- Loading branch information
Showing
7 changed files
with
436 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import csv | ||
import io | ||
import json | ||
import os | ||
import pathlib | ||
import subprocess | ||
from urllib.request import urlopen | ||
|
||
|
||
def prettier(path): | ||
subprocess.run( | ||
[ | ||
os.fspath(repoDir / "node_modules" / ".bin" / "prettier"), | ||
os.fspath(path), | ||
"--write", | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
def jsonDump(s): | ||
return json.dumps(s, ensure_ascii=False) | ||
|
||
|
||
def downloadSheet(url): | ||
print("downloading", url) | ||
response = urlopen(url) | ||
data = response.read() | ||
file = io.StringIO(data.decode("utf-8")) | ||
reader = csv.reader(file) | ||
return list(reader) | ||
|
||
|
||
languageSpreadsheetURL = ( | ||
"https://docs.google.com/" | ||
"spreadsheets/d/1woTU8dZCHJh7yvdk-N1kgQBUj4Sn3SdRsbKgn6ltJQs/" | ||
"export?format=csv&gid=1383907145" | ||
) | ||
|
||
rows = downloadSheet(languageSpreadsheetURL) | ||
|
||
numHeaders = 5 | ||
headers = rows[:numHeaders] | ||
assert headers[0][0] == "Documentation", headers[0][0] | ||
assert headers[1][2] == "English", headers[1][2] | ||
assert headers[2][2] == "English", headers[2][2] | ||
assert headers[3][2] == "en", headers[3][2] | ||
|
||
rows = rows[numHeaders:] | ||
|
||
startColumn = 2 | ||
|
||
languages = [] | ||
languageStrings = {} | ||
|
||
for columnIndex in range(startColumn, len(headers[1])): | ||
languageInEnglish = headers[1][columnIndex] | ||
languageInLanguage = headers[2][columnIndex] | ||
languageCode = headers[3][columnIndex] | ||
languageStatus = headers[4][columnIndex] | ||
assert languageCode | ||
if not languageStatus.strip(): | ||
continue | ||
|
||
languages.append( | ||
dict( | ||
code=languageCode, | ||
langEn=languageInEnglish, | ||
langLang=languageInLanguage, | ||
status=languageStatus, | ||
) | ||
) | ||
|
||
languageStrings[languageCode] = strings = {} | ||
|
||
for row in rows: | ||
key = row[1] | ||
if not key.strip(): | ||
continue | ||
string = row[columnIndex] | ||
if not string or string == "-": | ||
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} | ||
]; | ||
""" | ||
|
||
indexStart = localizationJSSource.find(languagesBlock.splitlines()[0]) | ||
assert indexStart > 0 | ||
indexEnd = localizationJSSource.find("];\n", indexStart) | ||
assert indexEnd > indexStart | ||
|
||
localizationJSSource = ( | ||
localizationJSSource[:indexStart] | ||
+ languagesBlock | ||
+ localizationJSSource[indexEnd + 3 :] | ||
) | ||
|
||
localizationJSPath.write_text(localizationJSSource, encoding="utf-8") | ||
prettier(localizationJSPath) | ||
|
||
|
||
languageSourceTemplate = """// Don't edit this file: it is generated by scripts/rebuild_languages.py | ||
export const strings = {{ | ||
{stringsBlock} | ||
}}; | ||
""" | ||
|
||
for languageCode, strings in languageStrings.items(): | ||
languagePath = langDir / f"{languageCode.strip()}.js" | ||
print("writing", languagePath) | ||
lines = [] | ||
for k, v in sorted(strings.items()): | ||
lines.append(f" {jsonDump(k.strip())}: {jsonDump(v)},") | ||
stringsBlock = "\n".join(lines) | ||
languageSource = languageSourceTemplate.format(stringsBlock=stringsBlock) | ||
languagePath.write_text(languageSource, encoding="utf-8") | ||
prettier(languagePath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.