forked from MapComplete/MapComplete
Fix: fix #1506, language-URL-parameter works again and is properly documented
This commit is contained in:
parent
3b33d69c82
commit
2942a2dae7
3 changed files with 52 additions and 49 deletions
|
@ -92,7 +92,7 @@ export default class UserRelatedState {
|
||||||
this.osmConnection.GetLongPreference("identity", "mangrove")
|
this.osmConnection.GetLongPreference("identity", "mangrove")
|
||||||
)
|
)
|
||||||
|
|
||||||
this.InitializeLanguage(availableLanguages)
|
this.language.addCallbackAndRunD((language) => Locale.language.setData(language))
|
||||||
|
|
||||||
this.installedUserThemes = this.InitInstalledUserThemes()
|
this.installedUserThemes = this.InitInstalledUserThemes()
|
||||||
|
|
||||||
|
@ -178,28 +178,6 @@ export default class UserRelatedState {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private InitializeLanguage(availableLanguages?: string[]) {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
if (availableLanguages?.indexOf(currentLanguage) < 0) {
|
|
||||||
console.log(
|
|
||||||
"Resetting language to",
|
|
||||||
availableLanguages[0],
|
|
||||||
"as",
|
|
||||||
currentLanguage,
|
|
||||||
" is unsupported"
|
|
||||||
)
|
|
||||||
// The current language is not supported -> switch to a supported one
|
|
||||||
Locale.language.setData(availableLanguages[0])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Locale.language.ping()
|
|
||||||
}
|
|
||||||
|
|
||||||
private InitInstalledUserThemes(): Store<string[]> {
|
private InitInstalledUserThemes(): Store<string[]> {
|
||||||
const prefix = "mapcomplete-unofficial-theme-"
|
const prefix = "mapcomplete-unofficial-theme-"
|
||||||
const postfix = "-combined-length"
|
const postfix = "-combined-length"
|
||||||
|
|
|
@ -54,6 +54,7 @@ export class QueryParameters {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static wasInitialized(key: string): boolean {
|
public static wasInitialized(key: string): boolean {
|
||||||
|
this.init()
|
||||||
return QueryParameters._wasInitialized.has(key)
|
return QueryParameters._wasInitialized.has(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,11 +77,10 @@ export class QueryParameters {
|
||||||
if (window?.location?.search) {
|
if (window?.location?.search) {
|
||||||
const params = window.location.search.substr(1).split("&")
|
const params = window.location.search.substr(1).split("&")
|
||||||
for (const param of params) {
|
for (const param of params) {
|
||||||
const kv = param.split("=")
|
const [key, value] = param.split("=")
|
||||||
const key = decodeURIComponent(kv[0])
|
|
||||||
QueryParameters.addOrder(key)
|
QueryParameters.addOrder(key)
|
||||||
QueryParameters._wasInitialized.add(key)
|
QueryParameters._wasInitialized.add(key)
|
||||||
const v = decodeURIComponent(kv[1])
|
const v = decodeURIComponent(value)
|
||||||
const source = new UIEventSource<string>(v)
|
const source = new UIEventSource<string>(v)
|
||||||
source.addCallback(() => QueryParameters.Serialize())
|
source.addCallback(() => QueryParameters.Serialize())
|
||||||
QueryParameters.knownSources[key] = source
|
QueryParameters.knownSources[key] = source
|
||||||
|
@ -131,4 +131,5 @@ export class QueryParameters {
|
||||||
QueryParameters._wasInitialized.clear()
|
QueryParameters._wasInitialized.clear()
|
||||||
QueryParameters.order = []
|
QueryParameters.order = []
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,26 +11,50 @@ export default class Locale {
|
||||||
public static showLinkOnMobile: UIEventSource<boolean> = new UIEventSource<boolean>(false)
|
public static showLinkOnMobile: UIEventSource<boolean> = new UIEventSource<boolean>(false)
|
||||||
public static language: UIEventSource<string> = Locale.setup()
|
public static language: UIEventSource<string> = Locale.setup()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the UIEventSource containing the identifier of the current language
|
||||||
|
*
|
||||||
|
* If the QueryParameter 'language' is set, this query parameter will be used as backing source value
|
||||||
|
* If not set, a localStorageSource will be used. This will use the navigator language by default
|
||||||
|
*
|
||||||
|
* Note that other parts of the code (most notably the UserRelatedState) might sync language selection with OSM.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static setup() {
|
private static setup() {
|
||||||
|
|
||||||
|
let source: UIEventSource<string>
|
||||||
|
|
||||||
|
if (QueryParameters.wasInitialized("language") || Utils.runningFromConsole) {
|
||||||
|
console.log("Language was initialized via URL-parameter - using the URL parameter as store instead of local storage", QueryParameters.wasInitialized("language"))
|
||||||
|
source = QueryParameters.GetQueryParameter(
|
||||||
|
"language",
|
||||||
|
undefined,
|
||||||
|
["The language to display MapComplete in.",
|
||||||
|
"The user display language is determined in the following order:",
|
||||||
|
"- If the user did log in and did set their language before with MapComplete, use this language",
|
||||||
|
"- If the user visited MapComplete before and did change their language, use the language as set by this URL-parameter. This will _disable_ saving the language to localStorage in case a non-logged-in user changes their language",
|
||||||
|
"- Use the navigator-language (if available)",
|
||||||
|
"- Use English",
|
||||||
|
"",
|
||||||
|
"Translations are never complete. If a translation in a certain language is missing, English is used as fallback."].join("\n"),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
let browserLanguage = "en"
|
let browserLanguage = "en"
|
||||||
if (typeof navigator !== "undefined") {
|
if (typeof navigator !== "undefined") {
|
||||||
browserLanguage = navigator.languages?.[0] ?? navigator.language ?? "en"
|
browserLanguage = navigator.languages?.[0] ?? navigator.language ?? "en"
|
||||||
|
console.log("Browser language is", browserLanguage)
|
||||||
}
|
}
|
||||||
const source = LocalStorageSource.Get("language", browserLanguage)
|
source = LocalStorageSource.Get("language", browserLanguage)
|
||||||
|
}
|
||||||
|
|
||||||
if (!Utils.runningFromConsole) {
|
if (!Utils.runningFromConsole) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
window.setLanguage = function (language: string) {
|
window.setLanguage = function (language: string) {
|
||||||
source.setData(language)
|
source.setData(language)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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."
|
|
||||||
)
|
|
||||||
Locale.language.setData(qp.data)
|
|
||||||
}
|
|
||||||
|
|
||||||
QueryParameters.GetBooleanQueryParameter(
|
QueryParameters.GetBooleanQueryParameter(
|
||||||
"fs-translation-mode",
|
"fs-translation-mode",
|
||||||
|
|
Loading…
Add table
Reference in a new issue