/* * Uses the languages in and to every translation from wikidata to generate a language question in wikidata/wikidata * */ import WikidataUtils from "../../Utils/WikidataUtils" import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson" import { MappingConfigJson } from "../../Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson" import LanguageUtils from "../../Utils/LanguageUtils" import * as perCountry from "../../assets/language_in_country.json" import { Utils } from "../../Utils" function main() { const sourcepath = "assets/generated/languages-wd.json" console.log(`Converting language data file '${sourcepath}' into a tagMapping`) const languages = WikidataUtils.extractLanguageData( JSON.parse(readFileSync(sourcepath, "utf8")), {} ) const mappings: MappingConfigJson[] = [] const schoolmappings: MappingConfigJson[] = [] const countryToLanguage: Record = perCountry const officialLanguagesPerCountry = Utils.TransposeMap(countryToLanguage) languages.forEach((l, code) => { const then: Record = {} l.forEach((tr, lng) => { const languageCodeWeblate = WikidataUtils.languageRemapping[lng] ?? lng if (!LanguageUtils.usedLanguages.has(languageCodeWeblate)) { return } then[languageCodeWeblate] = tr }) const officialCountries = Utils.Dedup( officialLanguagesPerCountry[code]?.map((s) => s.toLowerCase()) ?? [] ) const prioritySearch = officialCountries.length > 0 ? "_country~" + officialCountries.map((c) => "((^|;)" + c + "($|;))").join("|") : undefined mappings.push({ if: "language:" + code + "=yes", ifnot: "language:" + code + "=", searchTerms: { "*": [code], }, then, priorityIf: prioritySearch, }) schoolmappings.push({ if: "school:language=" + code, then, priorityIf: prioritySearch, searchTerms: { "*": [code], }, }) }) const wikidataLayer = { id: "wikidata", description: { en: "Various tagrenderings which are generated from Wikidata. Automatically generated with a script, don't edit manually", }, "#dont-translate": "*", source: { osmTags: "id~*", }, title: null, mapRendering: null, tagRenderings: [ { id: "language", // @ts-ignore description: "Enables to pick *a single* 'language:=yes' within the mappings", mappings, }, { builtin: "wikidata.language", override: { id: "language-multi", // @ts-ignore description: "Enables to pick *multiple* 'language:=yes' within the mappings", multiAnswer: true, }, }, { id: "school-language", // @ts-ignore description: "Enables to pick a single 'school:language=' within the mappings", multiAnswer: true, mappings: schoolmappings, }, ], } const dir = "./assets/layers/wikidata/" if (!existsSync(dir)) { mkdirSync(dir) } const path = dir + "wikidata.json" writeFileSync(path, JSON.stringify(wikidataLayer, null, " ")) console.log("Written " + path) } main()