2023-07-16 02:46:53 +02:00
import { UIEventSource } from "../../Logic/UIEventSource"
import { LocalStorageSource } from "../../Logic/Web/LocalStorageSource"
import { Utils } from "../../Utils"
import { QueryParameters } from "../../Logic/Web/QueryParameters"
2020-07-20 12:39:43 +02:00
export default class Locale {
2022-04-03 03:00:45 +02:00
public static showLinkToWeblate : UIEventSource < boolean > = new UIEventSource < boolean > ( false )
2023-02-09 02:45:19 +01:00
/ * *
* Indicates that - if showLinkToWeblate is true - a link on mobile mode is shown as well
* /
public static showLinkOnMobile : UIEventSource < boolean > = new UIEventSource < boolean > ( false )
2020-07-31 04:58:58 +02:00
public static language : UIEventSource < string > = Locale . setup ( )
2020-07-25 18:00:08 +02:00
2023-07-16 02:46:53 +02:00
/ * *
* 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
* /
2020-07-31 04:58:58 +02:00
private static setup() {
2023-07-16 02:46:53 +02:00
let source : UIEventSource < string >
if ( QueryParameters . wasInitialized ( "language" ) || Utils . runningFromConsole ) {
2023-07-16 19:37:11 +02:00
if ( ! Utils . runningFromConsole ) {
console . debug ( "Language was initialized via URL-parameter - using the URL parameter as store instead of local storage" , QueryParameters . wasInitialized ( "language" ) )
}
2023-07-16 02:46:53 +02:00
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" ,
"" ,
2023-07-16 02:50:02 +02:00
"Note that this URL-parameter is not added to the URL-bar by default." ,
"" ,
2023-07-16 02:46:53 +02:00
"Translations are never complete. If a translation in a certain language is missing, English is used as fallback." ] . join ( "\n" ) ,
)
} else {
let browserLanguage = "en"
if ( typeof navigator !== "undefined" ) {
browserLanguage = navigator . languages ? . [ 0 ] ? ? navigator . language ? ? "en"
console . log ( "Browser language is" , browserLanguage )
}
source = LocalStorageSource . Get ( "language" , browserLanguage )
2023-06-07 00:15:04 +02:00
}
2023-07-16 02:46:53 +02:00
2021-01-06 02:21:50 +01:00
if ( ! Utils . runningFromConsole ) {
2020-07-31 17:11:44 +02:00
// @ts-ignore
window . setLanguage = function ( language : string ) {
source . setData ( language )
}
2022-04-03 03:49:09 +02:00
}
2023-06-06 00:03:50 +02:00
2022-04-03 03:49:09 +02:00
QueryParameters . GetBooleanQueryParameter (
"fs-translation-mode" ,
false ,
"If set, will show a translation button next to every string."
) . addCallbackAndRunD ( ( tr ) = > {
Locale . showLinkToWeblate . setData ( Locale . showLinkToWeblate . data || tr )
} )
2022-04-03 03:00:45 +02:00
2020-07-31 04:58:58 +02:00
return source
2020-07-25 18:00:08 +02:00
}
2020-07-20 12:39:43 +02:00
}