Add language names to the language picker
This commit is contained in:
parent
793d765ead
commit
d8a0ca321b
7 changed files with 38404 additions and 12 deletions
|
@ -1,6 +1,9 @@
|
||||||
import {DropDown} from "./Input/DropDown";
|
import {DropDown} from "./Input/DropDown";
|
||||||
import Locale from "./i18n/Locale";
|
import Locale from "./i18n/Locale";
|
||||||
import BaseUIElement from "./BaseUIElement";
|
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 {
|
export default class LanguagePicker {
|
||||||
|
|
||||||
|
@ -14,10 +17,31 @@ export default class LanguagePicker {
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DropDown(label, languages.map(lang => {
|
return new DropDown(label, languages.map(lang => {
|
||||||
return {value: lang, shown: lang}
|
return {value: lang, shown: LanguagePicker.hybrid(lang) }
|
||||||
}
|
}
|
||||||
), Locale.language);
|
), 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
11
Utils.ts
11
Utils.ts
|
@ -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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
assets/language_native.json
Normal file
1
assets/language_native.json
Normal file
File diff suppressed because one or more lines are too long
38279
assets/language_translations.json
Normal file
38279
assets/language_translations.json
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,9 +0,0 @@
|
||||||
{
|
|
||||||
"bench": {
|
|
||||||
"tagRenderings": {
|
|
||||||
"bench-material": {
|
|
||||||
"render": "材质: {material}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -42,7 +42,8 @@
|
||||||
"gittag": "ts-node scripts/printVersion.ts | bash",
|
"gittag": "ts-node scripts/printVersion.ts | bash",
|
||||||
"lint": "tslint --project . -c tslint.json '**.ts' ",
|
"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)",
|
"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": [
|
"keywords": [
|
||||||
"OpenStreetMap",
|
"OpenStreetMap",
|
||||||
|
|
85
scripts/fetchLanguages.ts
Normal file
85
scripts/fetchLanguages.ts
Normal 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!"))
|
Loading…
Reference in a new issue