From 1271f241603918db41a37c61f4222f9de8896c18 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Thu, 21 Apr 2022 12:39:28 +0200 Subject: [PATCH] Fix delete button, remove Translations.WT --- Logic/Actors/TitleHandler.ts | 2 +- Logic/Web/Wikidata.ts | 20 ++++++++ UI/BigComponents/SimpleAddUI.ts | 2 +- UI/Popup/DeleteWizard.ts | 2 +- UI/Popup/ImportButton.ts | 2 +- UI/Popup/MoveWizard.ts | 2 +- UI/Popup/TagRenderingQuestion.ts | 2 +- UI/i18n/Translations.ts | 47 ++++++++++--------- .../mapcomplete-changes.json | 42 ++++++----------- scripts/generateLayouts.ts | 8 ++-- scripts/generateWikiPage.ts | 2 +- 11 files changed, 69 insertions(+), 62 deletions(-) diff --git a/Logic/Actors/TitleHandler.ts b/Logic/Actors/TitleHandler.ts index d20bea7b9..8593ecf18 100644 --- a/Logic/Actors/TitleHandler.ts +++ b/Logic/Actors/TitleHandler.ts @@ -16,7 +16,7 @@ export default class TitleHandler { const currentTitle: UIEventSource = state.selectedElement.map( selected => { const layout = state.layoutToUse - const defaultTitle = Translations.WT(layout?.title)?.txt ?? "MapComplete" + const defaultTitle = layout?.title?.txt ?? "MapComplete" if (selected === undefined) { return defaultTitle diff --git a/Logic/Web/Wikidata.ts b/Logic/Web/Wikidata.ts index dc4329ebd..936475001 100644 --- a/Logic/Web/Wikidata.ts +++ b/Logic/Web/Wikidata.ts @@ -147,6 +147,26 @@ export default class Wikidata { Wikidata._cache.set(key, src) return src; } + + public static async searchAdvanced(text: string, options: WikidataSearchoptions & { + instanceOf: number}){ + const sparql = `SELECT * WHERE { + SERVICE wikibase:mwapi { + bd:serviceParam wikibase:api "EntitySearch" . + bd:serviceParam wikibase:endpoint "www.wikidata.org" . + bd:serviceParam mwapi:search "${text}" . + bd:serviceParam mwapi:language "${options.lang}" . + ?item wikibase:apiOutputItem mwapi:item . + ?num wikibase:apiOrdinal true . + } + ?item (wdt:P279|wdt:P31) wd:Q${options.instanceOf} + } ORDER BY ASC(?num) LIMIT ${options.maxCount}` + const url = wds.sparqlQuery(sparql) + + const result = await Utils.downloadJson(url, {"User-Agent": "MapComplete script"}) + return result.results.bindings + + } public static async search( search: string, diff --git a/UI/BigComponents/SimpleAddUI.ts b/UI/BigComponents/SimpleAddUI.ts index 5bc1d1700..931092863 100644 --- a/UI/BigComponents/SimpleAddUI.ts +++ b/UI/BigComponents/SimpleAddUI.ts @@ -191,7 +191,7 @@ export default class SimpleAddUI extends Toggle { preset.icon(), new Combine([ title.SetClass("font-bold"), - Translations.WT(preset.description)?.FirstSentence() + preset.description?.FirstSentence() ]).SetClass("flex flex-col") ) } diff --git a/UI/Popup/DeleteWizard.ts b/UI/Popup/DeleteWizard.ts index ba289a2e2..1cfece179 100644 --- a/UI/Popup/DeleteWizard.ts +++ b/UI/Popup/DeleteWizard.ts @@ -202,7 +202,7 @@ export default class DeleteWizard extends Toggle { private static generateDeleteTagRenderingConfig(softDeletionTags: TagsFilter, nonDeleteOptions: { if: TagsFilter; then: Translation }[], extraDeleteReasons: { explanation: Translation; changesetMessage: string }[], - currentTags: any) { + currentTags: any): TagRenderingConfig { const t = Translations.t.delete nonDeleteOptions = nonDeleteOptions ?? [] let softDeletionTagsStr = [] diff --git a/UI/Popup/ImportButton.ts b/UI/Popup/ImportButton.ts index 8b7611b48..8db42618d 100644 --- a/UI/Popup/ImportButton.ts +++ b/UI/Popup/ImportButton.ts @@ -613,7 +613,7 @@ export class ImportPointButton extends AbstractImportButton { icon: () => new Img(args.icon), layerToAddTo: state.filteredLayers.data.filter(l => l.layerDef.id === args.targetLayer)[0], name: args.text, - title: Translations.WT(args.text), + title: Translations.T(args.text), preciseInput: preciseInputSpec, // must be explicitely assigned, if 'undefined' won't work otherwise boundsFactor: 3 } diff --git a/UI/Popup/MoveWizard.ts b/UI/Popup/MoveWizard.ts index 6ed7d0657..b6eb6e712 100644 --- a/UI/Popup/MoveWizard.ts +++ b/UI/Popup/MoveWizard.ts @@ -86,7 +86,7 @@ export default class MoveWizard extends Toggle { moveReason.setData(reason) moveButton = new SubtleButton( reason.icon.SetStyle("height: 1.5rem; width: 1.5rem;"), - Translations.WT(reason.invitingText) + Translations.T(reason.invitingText) ).onClick(() => { currentStep.setData("pick_location") }) diff --git a/UI/Popup/TagRenderingQuestion.ts b/UI/Popup/TagRenderingQuestion.ts index 31db46e3c..60521e273 100644 --- a/UI/Popup/TagRenderingQuestion.ts +++ b/UI/Popup/TagRenderingQuestion.ts @@ -207,7 +207,7 @@ export default class TagRenderingQuestion extends Combine { applicableMappings.map((mapping, i) => { return { value: new And([mapping.if, ...allIfNotsExcept(i)]), - shown: Translations.WT(mapping.then) + shown: Translations.T(mapping.then) } }) ) diff --git a/UI/i18n/Translations.ts b/UI/i18n/Translations.ts index 8186226fb..6a817de9a 100644 --- a/UI/i18n/Translations.ts +++ b/UI/i18n/Translations.ts @@ -22,6 +22,25 @@ export default class Translations { return s; } + /** + * Converts a string or an object into a typed translation. + * Translation objects ('Translation' and 'TypedTranslation') are converted/returned + * + * Translations.T("some text") // => new TypedTranslation({"*": "some text"}) + * Translations.T("some text").txt // => "some text" + * + * const t = new Translation({"nl": "vertaling", "en": "translation"}) + * Translations.T(t) // => new TypedTranslation({"nl": "vertaling", "en": "translation"}) + * + * const t = new TypedTranslation({"nl": "vertaling", "en": "translation"}) + * Translations.T(t) // => t + * + * const json: any = {"en": "English", "nl": "Nederlands"}; + * const translation = Translations.T(new Translation(json)); + * translation.textFor("en") // => "English" + * translation.textFor("nl") // => "Nederlands" + * + */ static T(t: string | any, context = undefined): TypedTranslation { if (t === undefined || t === null) { return undefined; @@ -30,7 +49,7 @@ export default class Translations { t = "" + t } if (typeof t === "string") { - return new TypedTranslation({"*": t}, context); + return new TypedTranslation({"*": t}, context); } if (t.render !== undefined) { const msg = "Creating a translation, but this object contains a 'render'-field. Use the translation directly" @@ -40,30 +59,12 @@ export default class Translations { if (t instanceof TypedTranslation) { return t; } - return new TypedTranslation(t, context); + if(t instanceof Translation){ + return new TypedTranslation(t.translations) + } + return new TypedTranslation(t, context); } - /** - * 'Wrap Translation': given an object containing translations OR a string, returns a translation object - * - * const json: any = {"en": "English", "nl": "Nederlands"}; - * const translation = Translations.WT(new Translation(json)); - * translation.textFor("en") // => "English" - * translation.textFor("nl") // => "Nederlands" - */ - public static WT(s: string | Translation): Translation { - if (s === undefined || s === null) { - return undefined; - } - if (typeof (s) === "string") { - return new Translation({'*': s}); - } - if (s instanceof Translation) { - return s.Clone() /* MUST CLONE HERE! */; - } - console.error("Trying to Translation.WT, but got ", s) - throw "??? Not a valid translation" - } public static CountTranslations() { const queue: any = [Translations.t]; diff --git a/assets/themes/mapcomplete-changes/mapcomplete-changes.json b/assets/themes/mapcomplete-changes/mapcomplete-changes.json index 2f479a092..c90abd4c4 100644 --- a/assets/themes/mapcomplete-changes/mapcomplete-changes.json +++ b/assets/themes/mapcomplete-changes/mapcomplete-changes.json @@ -1,16 +1,13 @@ { "id": "mapcomplete-changes", "title": { - "en": "Changes made with MapComplete", - "de": "Änderungen mit MapComplete" + "en": "Changes made with MapComplete" }, "shortDescription": { - "en": "Shows changes made by MapComplete", - "de": "Zeigt Änderungen von MapComplete" + "en": "Shows changes made by MapComplete" }, "description": { - "en": "This maps shows all the changes made with MapComplete", - "de": "Diese Karte zeigt alle Änderungen die mit MapComplete gemacht wurden" + "en": "This maps shows all the changes made with MapComplete" }, "maintainer": "", "icon": "./assets/svg/logo.svg", @@ -25,8 +22,7 @@ { "id": "mapcomplete-changes", "name": { - "en": "Changeset centers", - "de": "Schwerpunkte von Änderungssätzen" + "en": "Changeset centers" }, "minzoom": 0, "source": { @@ -40,41 +36,35 @@ ], "title": { "render": { - "en": "Changeset for {theme}", - "de": "Änderungen für {theme}" + "en": "Changeset for {theme}" } }, "description": { - "en": "Shows all MapComplete changes", - "de": "Zeigt alle MapComplete Änderungen" + "en": "Shows all MapComplete changes" }, "tagRenderings": [ { "id": "render_id", "render": { - "en": "Changeset {id}", - "de": "Änderung {id}" + "en": "Changeset {id}" } }, { "id": "contributor", "render": { - "en": "Change made by {_last_edit:contributor}", - "de": "Änderung wurde von {_last_edit:contributor} gemacht" + "en": "Change made by {_last_edit:contributor}" } }, { "id": "theme", "render": { - "en": "Change with theme {theme}", - "de": "Änderung mit Thema {theme}" + "en": "Change with theme {theme}" }, "mappings": [ { "if": "theme~http.*", "then": { - "en": "Change with unofficial theme {theme}", - "de": "Änderung mit inoffiziellem Thema {theme}" + "en": "Change with unofficial theme {theme}" } } ] @@ -338,8 +328,7 @@ } ], "question": { - "en": "Themename contains {search}", - "de": "Themenname enthält {search}" + "en": "Themename contains {search}" } } ] @@ -355,8 +344,7 @@ } ], "question": { - "en": "Made by contributor {search}", - "de": "Erstellt von {search}" + "en": "Made by contributor {search}" } } ] @@ -372,8 +360,7 @@ } ], "question": { - "en": "Not made by contributor {search}", - "de": "Nicht erstellt von {search}" + "en": "Not made by contributor {search}" } } ] @@ -388,8 +375,7 @@ { "id": "link_to_more", "render": { - "en": "More statistics can be found here", - "de": "Weitere Statistiken finden Sie hier" + "en": "More statistics can be found here" } }, { diff --git a/scripts/generateLayouts.ts b/scripts/generateLayouts.ts index e3c311eda..546d26350 100644 --- a/scripts/generateLayouts.ts +++ b/scripts/generateLayouts.ts @@ -161,8 +161,8 @@ async function createManifest(layout: LayoutConfig, alreadyWritten: string[]): P console.log(icon) throw "Icon is not an svg for " + layout.id } - const ogTitle = Translations.WT(layout.title).txt; - const ogDescr = Translations.WT(layout.description ?? "").txt; + const ogTitle = Translations.T(layout.title).txt; + const ogDescr = Translations.T(layout.description ?? "").txt; const manifest = { name: name, @@ -186,8 +186,8 @@ async function createLandingPage(layout: LayoutConfig, manifest, whiteIcons, alr Locale.language.setData(layout.language[0]); const targetLanguage = layout.language[0] - const ogTitle = Translations.WT(layout.title).textFor(targetLanguage).replace(/"/g, '\\"'); - const ogDescr = Translations.WT(layout.shortDescription ?? "Easily add and edit geodata with OpenStreetMap").textFor(targetLanguage).replace(/"/g, '\\"'); + const ogTitle = Translations.T(layout.title).textFor(targetLanguage).replace(/"/g, '\\"'); + const ogDescr = Translations.T(layout.shortDescription ?? "Easily add and edit geodata with OpenStreetMap").textFor(targetLanguage).replace(/"/g, '\\"'); let ogImage = layout.socialImage; let twitterImage = ogImage if (ogImage === LayoutConfig.defaultSocialImage && layout.official) { diff --git a/scripts/generateWikiPage.ts b/scripts/generateWikiPage.ts index 1c06c0f53..0e7a7ac28 100644 --- a/scripts/generateWikiPage.ts +++ b/scripts/generateWikiPage.ts @@ -18,7 +18,7 @@ function generateWikiEntry(layout: { hideFromOverview: boolean, id: string, shor |name= [https://mapcomplete.osm.be/${layout.id} ${layout.id}] |region= Worldwide |lang= ${languages} -|descr= A MapComplete theme: ${Translations.WT(layout.shortDescription) +|descr= A MapComplete theme: ${Translations.T(layout.shortDescription) .textFor("en") .replace(".*<\/a>/, "]]")