Add language names to the language picker

This commit is contained in:
pietervdvn 2022-02-24 02:33:20 +01:00
parent 793d765ead
commit d8a0ca321b
7 changed files with 38404 additions and 12 deletions

View file

@ -1,6 +1,9 @@
import {DropDown} from "./Input/DropDown";
import Locale from "./i18n/Locale";
import BaseUIElement from "./BaseUIElement";
import * as native from "../assets/language_native.json"
import * as language_translations from "../assets/language_translations.json"
import {Translation} from "./i18n/Translation";
export default class LanguagePicker {
@ -12,12 +15,33 @@ export default class LanguagePicker {
if (languages === undefined || languages.length <= 1) {
return undefined;
}
return new DropDown(label, languages.map(lang => {
return {value: lang, shown: lang}
return {value: lang, shown: LanguagePicker.hybrid(lang) }
}
), Locale.language);
}
private static hybrid(lang: string): Translation {
const nativeText = native[lang] ?? lang
const allTranslations = (language_translations["default"] ?? language_translations)
const translation = {}
const trans = allTranslations[lang]
console.log("Generating a hybrid for "+lang, trans)
if(trans === undefined){
return new Translation({"*": nativeText})
}
for (const key in trans) {
const translationInKey = allTranslations[lang][key]
if(nativeText.toLowerCase() === translationInKey.toLowerCase()){
translation[key] = nativeText
}else{
translation[key] = nativeText + " ("+translationInKey+")"
}
}
return new Translation(translation)
}
}

View file

@ -778,5 +778,16 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
}
}
public static MapToObj<T>(d : Map<string, T>, onValue: ((t:T) => any) = undefined): object{
const o = {}
d.forEach((value, key) => {
if(onValue !== undefined){
value = onValue(value)
}
o[key] = value;
})
return o
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -1,9 +0,0 @@
{
"bench": {
"tagRenderings": {
"bench-material": {
"render": "材质: {material}"
}
}
}
}

View file

@ -42,7 +42,8 @@
"gittag": "ts-node scripts/printVersion.ts | bash",
"lint": "tslint --project . -c tslint.json '**.ts' ",
"clean": "rm -rf .cache/ && (find *.html | grep -v \"\\(404\\|index\\|land\\|test\\|preferences\\|customGenerator\\|professional\\|automaton\\|import_helper\\|import_viewer\\|theme\\).html\" | xargs rm) && (ls | grep \"^index_[a-zA-Z_]\\+\\.ts$\" | xargs rm) && (ls | grep \".*.webmanifest$\" | xargs rm)",
"generate:dependency-graph": "node_modules/.bin/depcruise --exclude \"^node_modules\" --output-type dot Logic/State/MapState.ts > dependencies.dot && dot dependencies.dot -T svg -o dependencies.svg && rm dependencies.dot"
"generate:dependency-graph": "node_modules/.bin/depcruise --exclude \"^node_modules\" --output-type dot Logic/State/MapState.ts > dependencies.dot && dot dependencies.dot -T svg -o dependencies.svg && rm dependencies.dot",
"script": "ts-node"
},
"keywords": [
"OpenStreetMap",

85
scripts/fetchLanguages.ts Normal file
View file

@ -0,0 +1,85 @@
/**
* Fetches all 'modern languages' from wikidata, then exports their names in every language
*/
import * as wds from "wikidata-sdk"
import {Utils} from "../Utils";
import ScriptUtils from "./ScriptUtils";
import {existsSync, readFileSync, writeFileSync} from "fs";
import * as knownLanguages from "../assets/generated/used_languages.json"
async function fetch(target: string) {
ScriptUtils.fixUtils()
console.log("Fetching languages")
const sparql = 'SELECT ?lang ?label ?code \n' +
'WHERE \n' +
'{ \n' +
' ?lang wdt:P31 wd:Q1288568. \n' + // language instanceOf (p31) modern language(Q1288568)
' ?lang rdfs:label ?label. \n' +
' ?lang wdt:P424 ?code' + // Wikimedia language code seems to be close to the weblate entries
'} '
const url = wds.sparqlQuery(sparql)
// request the generated URL with your favorite HTTP request library
const result = await Utils.downloadJson(url, {"User-Agent": "MapComplete script"})
writeFileSync(target, JSON.stringify(result.results.bindings))
console.log("Written to "+target)
}
function extract(data){
console.log("Got "+data.length+" entries")
const perId = new Map<string, Map<string, string>>();
for (const element of data) {
//const id = element.lang.value.substring(prefixL)
const id = element.code.value
const labelLang = element.label["xml:lang"]
const value = element.label.value
if(!perId.has(id)){
perId.set(id, new Map<string, string>())
}
perId.get(id).set(labelLang, value)
}
console.log("Got "+perId.size+" languages")
return perId
}
function getNativeList(langs: Map<string, Map<string, string>>){
const native = {}
langs.forEach((translations, key ) =>{
native[key] = translations.get(key)
})
return native
}
function getTranslationsIn(targetLanguage: string, perId: Map<string, Map<string, string>>, whitelist = undefined){
const langs = {}
perId.forEach((translations, langCode) => {
if(whitelist !== undefined && whitelist.indexOf(langCode) < 0){
return
}
langs[langCode] = translations.get(targetLanguage)
})
return langs;
}
function main(wipeCache = false){
const cacheFile = "./assets/generated/languages-wd.json"
if(wipeCache || !existsSync(cacheFile)){
// await fetch(cacheFile);
}else{
console.log("Reusing the cached file")
}
const data = JSON.parse(readFileSync( cacheFile, "UTF8"))
const perId = extract(data)
const nativeList = getNativeList(perId)
writeFileSync("./assets/language_native.json", JSON.stringify(nativeList))
writeFileSync("./assets/language_translations.json",
JSON.stringify(Utils.MapToObj<Map<string, string>>(perId, value => Utils.MapToObj(value)), null, " "))
}
main()//.then(() => console.log("Done!"))