Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ jobs:
- name: Run mypy
run: |
echo "::add-matcher::.github/matchers/mypy.json"
mypy --show-column-numbers weblate_language_data
mypy --show-column-numbers weblate_language_data scripts/*.py
echo "::remove-matcher owner=mypy::"
11 changes: 9 additions & 2 deletions scripts/add-iso.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def convert_name(name: str) -> str:
item.get("inverted_name", item["name"])
)

lines: list[list[str]]

with open("languages.csv", newline="") as handle:
reader = csv.reader(handle, delimiter=",")
header = next(reader)
Expand Down Expand Up @@ -70,12 +72,17 @@ def convert_name(name: str) -> str:
[
code,
names[code].split(";")[-1].strip(),
plurals,
str(plurals),
formula,
],
)

with open("languages.csv", "w", newline="") as handle:
writer = csv.writer(handle, delimiter=",", lineterminator="\n")
writer.writerow(header)
writer.writerows(sorted(lines, key=itemgetter(0)))
writer.writerows(
sorted(
lines,
key=itemgetter(0), # type: ignore[arg-type]
)
)
11 changes: 8 additions & 3 deletions scripts/export-cldr-case.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
#
# SPDX-License-Identifier: MIT

from __future__ import annotations

import csv
import json

# Read languages
with open("languages.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
next(reader)
LANGUAGES = list(reader)
LANGUAGE_CODES = {lang[0] for lang in LANGUAGES}
LANGUAGE_CODES = {lang[0] for lang in reader}

# Read
with open("case-insensitive.csv") as csvfile:
Expand All @@ -27,8 +28,12 @@
with open(
"modules/cldr-json/cldr-json/cldr-core/supplemental/languageData.json",
) as handle:
LANGUAGES = json.load(handle)["supplemental"]["languageData"]
LANGUAGES: dict[str, dict[str, str]] = json.load(handle)["supplemental"][
"languageData"
]

base: str
script: str | None
for code in LANGUAGE_CODES:
if "_" in code:
base, script = code.split("_", 1)
Expand Down
2 changes: 1 addition & 1 deletion scripts/export-cldr-population.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
with open(
"modules/cldr-json/cldr-json/cldr-core/supplemental/territoryInfo.json",
) as handle:
languages = defaultdict(int)
languages: dict[str, float] = defaultdict(float)
for code, territory in json.load(handle)["supplemental"]["territoryInfo"].items():
population = int(territory["_population"])
if "languagePopulation" not in territory:
Expand Down
21 changes: 17 additions & 4 deletions scripts/export-cldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import json
import re
from typing import Literal, TypedDict

MAPPINGS = {
"ar_001": "ar",
Expand Down Expand Up @@ -112,7 +113,7 @@ def convert_atom(atom: str) -> str | bool:
)


def convert_formula(cldr_formula_and_examples: str) -> str:
def convert_formula(cldr_formula_and_examples: str) -> str | bool:
# Skip formulas which do not trigger integer
if "@integer" not in cldr_formula_and_examples:
return False
Expand Down Expand Up @@ -169,7 +170,9 @@ def convert_formula(cldr_formula_and_examples: str) -> str:
return " || ".join(chunks)


def reverse_formula(formula: str) -> str:
def reverse_formula(formula: str | bool) -> str:
if isinstance(formula, bool):
raise TypeError(f"Unable to reverse the formula {formula!r}")
if re.match(r"^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$", formula):
return formula.replace(" == ", " != ")
if re.match(r"^n( % \d+)? != \d+(\.\.\d+|,\d+)*?$", formula):
Expand All @@ -193,14 +196,16 @@ def reverse_formula(formula: str) -> str:
if formula == "(n == 0 || n == 1) || n >= 11 && n <= 99":
return "n >= 2 && (n < 11 || n > 99)"

raise ValueError(f"Unable to reverse the formula '{formula}'")
raise ValueError(f"Unable to reverse the formula {formula!r}")


def merge_formulas(formulas: list[str]) -> str:
def merge_formulas(formulas: list[str | Literal[True]]) -> str:
max_n = len(formulas) - 1
formula = f"{max_n}"
for n in range(max_n - 1, -1, -1):
part = formulas[n]
if isinstance(part, bool):
raise TypeError(f"Not supported part {part!r}")

if not re.match(r"^\([^()]+\)$", part):
part = f"({part})"
Expand All @@ -211,6 +216,14 @@ def merge_formulas(formulas: list[str]) -> str:
return formula


class LanguageDict(TypedDict, total=False):
code: str
name: str
plurals: int
formula: str


LANGUAGES: dict[str, LanguageDict]
# Load language names
with open(
"modules/cldr-json/cldr-json/cldr-localenames-full/main/en/languages.json",
Expand Down
8 changes: 4 additions & 4 deletions scripts/export-plural-tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import pprint
import subprocess

BASE = "modules/cldr-json/cldr-json/cldr-core/supplemental/plurals.json"
ALIASES = "modules/cldr-json/cldr-json/cldr-core/supplemental/aliases.json"
BASE_FILE = "modules/cldr-json/cldr-json/cldr-core/supplemental/plurals.json"
ALIASES_FILE = "modules/cldr-json/cldr-json/cldr-core/supplemental/aliases.json"

HEADER = '''# Copyright © Michal Čihař <michal@weblate.org>
#
Expand Down Expand Up @@ -93,7 +93,7 @@
result = {}
decimals = {}

with open(BASE) as handle:
with open(BASE_FILE) as handle:
data = json.load(handle)

for locale, rules in data["supplemental"]["plurals-type-cardinal"].items():
Expand All @@ -105,7 +105,7 @@
decimals[locale] = [name.replace("pluralRule-count-", "") for name in rules]

# Process CLDR
with open(ALIASES) as handle:
with open(ALIASES_FILE) as handle:
aliases = json.load(handle)

for code, alias in aliases["supplemental"]["metadata"]["alias"][
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-language-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_population(code):
except KeyError:
continue
if existing != number:
CLDRPLURALS.append((code, LANGUAGE_NAMES[code], number, equation))
CLDRPLURALS.append([code, LANGUAGE_NAMES[code], number, equation])

# Read default countries
with open("default_countries.csv") as csvfile:
Expand Down
Loading