| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  | /* | 
					
						
							|  |  |  | * 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"; | 
					
						
							| 
									
										
										
										
											2022-07-15 01:10:10 +02:00
										 |  |  | import * as perCountry from "../../assets/language_in_country.json" | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  | 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[] = [] | 
					
						
							| 
									
										
										
										
											2022-07-15 01:10:10 +02:00
										 |  |  |      | 
					
						
							|  |  |  |     const countryToLanguage : Record<string, string[]> = perCountry | 
					
						
							|  |  |  |     const officialLanguagesPerCountry = Utils.TransposeMap(countryToLanguage); | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |     languages.forEach((l, code) => { | 
					
						
							|  |  |  |         const then : Record<string, string>= {} | 
					
						
							|  |  |  |         l.forEach((tr, lng) => { | 
					
						
							|  |  |  |             const languageCodeWeblate = WikidataUtils.languageRemapping[lng] ?? lng; | 
					
						
							|  |  |  |             if(!LanguageUtils.usedLanguages.has(languageCodeWeblate)){ | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             then[languageCodeWeblate] = tr | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-07-15 01:10:10 +02:00
										 |  |  |          | 
					
						
							|  |  |  |         const officialCountries = Utils.Dedup(officialLanguagesPerCountry[code]?.map(s => s.toLowerCase()) ?? []) | 
					
						
							|  |  |  |         const prioritySearch = officialCountries.length > 0 ? "_country~" + officialCountries.map(c => "((^|;)"+c+"($|;))").join("|") : undefined | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         mappings.push(<MappingConfigJson>{ | 
					
						
							|  |  |  |             if: "language:" + code + "=yes", | 
					
						
							|  |  |  |             ifnot: "language:" + code + "=", | 
					
						
							|  |  |  |             searchTerms: { | 
					
						
							|  |  |  |                 "*": [code] | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2022-07-15 01:10:10 +02:00
										 |  |  |             then, | 
					
						
							|  |  |  |             priorityIf: prioritySearch | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        schoolmappings.push(<MappingConfigJson>{ | 
					
						
							|  |  |  |             if: "school:language=" + code, | 
					
						
							| 
									
										
										
										
											2022-07-15 01:10:10 +02:00
										 |  |  |             then, | 
					
						
							|  |  |  |             priorityIf: prioritySearch, | 
					
						
							|  |  |  |             searchTerms: { | 
					
						
							|  |  |  |                 "*":[code] | 
					
						
							|  |  |  |            } | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     | 
					
						
							|  |  |  |     const wikidataLayer = <LayerConfigJson>{ | 
					
						
							|  |  |  |         id: "wikidata", | 
					
						
							| 
									
										
										
										
											2022-07-20 22:57:39 +02:00
										 |  |  |         description: { | 
					
						
							|  |  |  |             en: "Various tagrenderings which are generated from Wikidata. Automatically generated with a script, don't edit manually" | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         "#dont-translate": "*", | 
					
						
							|  |  |  |         "source": { | 
					
						
							|  |  |  |             "osmTags": "id~*" | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-07-20 22:57:39 +02:00
										 |  |  |         title: null, | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         "mapRendering": null, | 
					
						
							|  |  |  |         tagRenderings: [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 id: "language", | 
					
						
							|  |  |  |                 // @ts-ignore
 | 
					
						
							|  |  |  |                 description: "Enables to pick *a single* 'language:<lng>=yes' within the mappings", | 
					
						
							|  |  |  |                 mappings, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 builtin: "wikidata.language", | 
					
						
							|  |  |  |                 override: { | 
					
						
							|  |  |  |                     id: "language-multi", | 
					
						
							|  |  |  |                     // @ts-ignore
 | 
					
						
							|  |  |  |                     description: "Enables to pick *multiple* 'language:<lng>=yes' within the mappings", | 
					
						
							|  |  |  |                     multiAnswer: true | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 id:"school-language", | 
					
						
							|  |  |  |                 // @ts-ignore
 | 
					
						
							|  |  |  |                 description: "Enables to pick a single 'school:language=<lng>' 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() |