From c6283ac7205f69c1a42e8cf16e5f4f1ab2c239f8 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 6 Jun 2023 00:03:50 +0200 Subject: [PATCH] Fix: setting the language does not overwrite the user setting anymore --- Logic/State/UserRelatedState.ts | 4 +++- UI/AllThemesGui.ts | 2 +- UI/LanguagePicker.ts | 13 +++++++------ UI/ProfessionalGui.ts | 4 ++-- UI/SpecialVisualization.ts | 1 + UI/i18n/Locale.ts | 14 ++++++++------ 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Logic/State/UserRelatedState.ts b/Logic/State/UserRelatedState.ts index bd130cf1b4..b214c70a4c 100644 --- a/Logic/State/UserRelatedState.ts +++ b/Logic/State/UserRelatedState.ts @@ -36,6 +36,7 @@ export default class UserRelatedState { public readonly showAllQuestionsAtOnce: UIEventSource public readonly showTags: UIEventSource<"no" | undefined | "always" | "yes">; public readonly homeLocation: FeatureSource + public readonly language: UIEventSource /** * The number of seconds that the GPS-locations are stored in memory. * Time in seconds @@ -83,6 +84,7 @@ export default class UserRelatedState { "Either 'true' or 'false'. If set, all questions will be shown all at once", }) ) + this.language = this.osmConnection.GetPreference("language") this.showTags = >this.osmConnection.GetPreference("show_tags") this.mangroveIdentity = new MangroveIdentity( @@ -181,7 +183,7 @@ export default class UserRelatedState { } private InitializeLanguage(availableLanguages?: string[]) { - Locale.language.syncWith(this.osmConnection.GetPreference("language")) + this.language.addCallbackAndRunD(language => Locale.language.setData(language)) Locale.language.addCallback((currentLanguage) => { if (Locale.showLinkToWeblate.data) { return true // Disable auto switching as we are in translators mode diff --git a/UI/AllThemesGui.ts b/UI/AllThemesGui.ts index 43c64f8cc4..722eec27ef 100644 --- a/UI/AllThemesGui.ts +++ b/UI/AllThemesGui.ts @@ -27,7 +27,7 @@ export default class AllThemesGui { }) const state = new UserRelatedState(osmConnection) const intro = new Combine([ - new LanguagePicker(Translations.t.index.title.SupportedLanguages(), "").SetClass( + new LanguagePicker(Translations.t.index.title.SupportedLanguages(), state.language).SetClass( "flex absolute top-2 right-3" ), new IndexText(), diff --git a/UI/LanguagePicker.ts b/UI/LanguagePicker.ts index b93cca591a..3a5f3950f9 100644 --- a/UI/LanguagePicker.ts +++ b/UI/LanguagePicker.ts @@ -7,29 +7,30 @@ import { Translation } from "./i18n/Translation" import Lazy from "./Base/Lazy" import Toggle from "./Input/Toggle" import LanguageUtils from "../Utils/LanguageUtils" +import {UIEventSource} from "../Logic/UIEventSource"; export default class LanguagePicker extends Toggle { - constructor(languages: string[], label: string | BaseUIElement = "") { + constructor(languages: string[], assignTo: UIEventSource) { console.log("Constructing a language pîcker for languages", languages) if (languages === undefined || languages.length <= 1) { super(undefined, undefined, undefined) } else { - const normalPicker = LanguagePicker.dropdownFor(languages, label) - const fullPicker = new Lazy(() => LanguagePicker.dropdownFor(allLanguages, label)) + const normalPicker = LanguagePicker.dropdownFor(languages, assignTo ?? Locale.language) + const fullPicker = new Lazy(() => LanguagePicker.dropdownFor(allLanguages, assignTo ?? Locale.language)) super(fullPicker, normalPicker, Locale.showLinkToWeblate) const allLanguages: string[] = LanguageUtils.usedLanguagesSorted } } - private static dropdownFor(languages: string[], label: string | BaseUIElement): BaseUIElement { + private static dropdownFor(languages: string[], assignTo: UIEventSource): BaseUIElement { return new DropDown( - label, + undefined, languages .filter((lang) => lang !== "_context") .map((lang) => { return { value: lang, shown: LanguagePicker.hybrid(lang) } }), - Locale.language + assignTo ) } diff --git a/UI/ProfessionalGui.ts b/UI/ProfessionalGui.ts index de7eee3f59..a9955a47ff 100644 --- a/UI/ProfessionalGui.ts +++ b/UI/ProfessionalGui.ts @@ -8,6 +8,7 @@ import BaseUIElement from "./BaseUIElement" import LanguagePicker from "./LanguagePicker" import TableOfContents from "./Base/TableOfContents" import LeftIndex from "./Base/LeftIndex" +import Locale from "./i18n/Locale"; class Snippet extends Toggleable { constructor(translations, ...extraContent: BaseUIElement[]) { @@ -89,8 +90,7 @@ class ProfessionalGui extends LeftIndex { }).SetClass("subtle"), new LanguagePicker( - Translations.t.professional.title.SupportedLanguages(), - "" + Translations.t.professional.title.SupportedLanguages(), Locale.language )?.SetClass("mt-4 self-end flex-col"), ].map((el) => el?.SetClass("pl-4")) diff --git a/UI/SpecialVisualization.ts b/UI/SpecialVisualization.ts index f26f5e489e..f716e7536b 100644 --- a/UI/SpecialVisualization.ts +++ b/UI/SpecialVisualization.ts @@ -68,6 +68,7 @@ export interface SpecialVisualizationState { readonly mangroveIdentity: MangroveIdentity readonly showAllQuestionsAtOnce: UIEventSource readonly preferencesAsTags: Store> + readonly language: UIEventSource } readonly lastClickObject: WritableFeatureSource diff --git a/UI/i18n/Locale.ts b/UI/i18n/Locale.ts index d989db3b37..2c405c980d 100644 --- a/UI/i18n/Locale.ts +++ b/UI/i18n/Locale.ts @@ -12,21 +12,23 @@ export default class Locale { public static language: UIEventSource = Locale.setup() private static setup() { - const source = LocalStorageSource.Get("language", "en") + const browserLanguage =navigator.languages?.[0] ?? navigator.language ?? "en" + const source = LocalStorageSource.Get("language", browserLanguage) if (!Utils.runningFromConsole) { // @ts-ignore window.setLanguage = function (language: string) { source.setData(language) } } - source.syncWith( - QueryParameters.GetQueryParameter( + if(QueryParameters.wasInitialized("language")){ + const qp = QueryParameters.GetQueryParameter( "language", undefined, "The language to display mapcomplete in. Will be ignored in case a logged-in-user did set their language before. If the specified language does not exist, it will default to the first language in the theme." - ), - true - ) + ) + Locale.language.setData(qp.data) + } + QueryParameters.GetBooleanQueryParameter( "fs-translation-mode", false,