Automate ISO aliases generation

This commit is contained in:
Michal Čihař 2020-09-29 11:50:32 +02:00
parent 10d3527165
commit 32e9bec596
3 changed files with 69 additions and 26 deletions

View file

@ -24,3 +24,6 @@ translate.csv: modules/translate/translate/lang/data.py scripts/export-translate

weblate_language_data/plural_tags.py: modules/cldr-core/supplemental/plurals.json scripts/export-plural-tags
./scripts/export-plural-tags

aliases.csv: scripts/export-iso-aliases modules/iso-codes/data/iso_639-2.json modules/iso-codes/data/iso_639-3.json
./scripts/export-iso-aliases

View file

@ -1,26 +0,0 @@
#! /usr/bin/env python3

import json

names = {}


def add_name(item):
if "alpha_2" not in item:
return
if "alpha_3" in item:
names[item["alpha_3"]] = item["alpha_2"]
if "bibliographic" in item:
names[item["bibliographic"]] = item["alpha_2"]


with open("/usr/share/iso-codes/json/iso_639-2.json", "r") as handle:
for item in json.load(handle)["639-2"]:
add_name(item)

with open("/usr/share/iso-codes/json/iso_639-3.json", "r") as handle:
for item in json.load(handle)["639-3"]:
add_name(item)

for row in sorted(names.items()):
print("{};{}".format(*row))

66
scripts/export-iso-aliases Executable file
View file

@ -0,0 +1,66 @@
#! /usr/bin/env python3
#
# Copyright © 2018 - 2020 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

import csv
import json

names = {}


def parse_csv(name):
result = {}
with open(name, "r") as csvfile:
reader = csv.reader(csvfile, delimiter=";")
for data in reader:
if data[0] == "#":
continue
if data[0] in result:
raise Exception("Duplicate {} in {}!".format(data[0], name))
result[data[0]] = data
return result


def add_name(item):
if "alpha_2" not in item:
return
if "alpha_3" in item:
names[item["alpha_3"]] = item["alpha_2"]
if "bibliographic" in item:
names[item["bibliographic"]] = item["alpha_2"]


with open("modules/iso-codes/data/iso_639-2.json", "r") as handle:
for item in json.load(handle)["639-2"]:
add_name(item)

with open("modules/iso-codes/data/iso_639-3.json", "r") as handle:
for item in json.load(handle)["639-3"]:
add_name(item)

aliases = parse_csv("aliases.csv")

lines = []
for name, alias in sorted(names.items()):
if name not in aliases:
lines.append(f"{name};{alias}\n")

if lines:
with open("aliases.csv", "a") as handle:
handle.writelines(sorted(lines))