@@ -44,45 +44,71 @@ const Map = L.Class.extend({
4444 this . Keyboard . _initKeyListener ( map ) ;
4545 } ,
4646 // eslint-disable-next-line default-param-last
47- setLang ( lang = 'en' , override , fallback = 'en' ) {
48- // Normalize the language code to lowercase and trim any whitespace
49- lang = lang . trim ( ) . toLowerCase ( ) ;
47+ setLang ( langInput = 'en' , override , fallback = 'en' ) {
48+ // 1. Normalize input and determine the intended language code
49+ const normalizedInput = langInput ?. trim ( ) . toLowerCase ( ) || '' ;
50+ const originalFallbackLang = fallback ?. trim ( ) . toLowerCase ( ) || 'en' ;
51+ let intendedLang = originalFallbackLang ; // Default intention is the fallback
5052
51- // First, check if the input is already in the expected format (e.g., 'fr')
52- if ( / ^ [ a - z ] { 2 } $ / . test ( lang ) ) {
53- // No further processing needed for single-letter codes
54- } else {
55- // Handle formats like 'fr-FR', 'FR', 'fr-fr', 'fr_FR'
56- const normalizedLang = lang
57- . replace ( / [ - _ \s ] / g, '-' )
58- . replace ( / ^ ( \w { 2 } ) $ / , '$1-' ) ;
59- const match = normalizedLang . match ( / ( [ a - z ] { 2 } ) - ? ( [ a - z ] { 2 } ) ? / ) ;
53+ const match = normalizedInput . match ( / ^ ( [ a - z ] { 2 , 3 } ) (?: [ - _ ] ( [ a - z ] { 2 } ) ) ? .* $ / ) ;
54+ let baseLang = null ;
55+ if ( match ) {
56+ [ , baseLang ] = match ;
57+ } else if ( / ^ [ a - z ] { 2 , 3 } $ / . test ( normalizedInput ) ) {
58+ baseLang = normalizedInput ;
59+ }
6060
61- if ( match ) {
62- // Construct potential keys to search for in the translations object
63- const potentialKeys = [
64- ` ${ match [ 1 ] } _ ${ match [ 2 ] } ` , // e.g., 'fr_BR '
65- ` ${ match [ 1 ] } ` , // e.g., 'fr'
66- ] ;
61+ if ( baseLang ) {
62+ intendedLang = baseLang ; // If input gives a valid base code, that's the intention
63+ } else if ( ! normalizedInput ) {
64+ intendedLang = 'en' ; // If input is empty/invalid, intention defaults to 'en '
65+ }
66+ // Now, intendedLang is the primary code derived from input, or the fallback, or 'en'.
6767
68- // Search through the translations object for a matching key
69- for ( const key of potentialKeys ) {
70- if ( translations [ key ] ) {
71- lang = key ; // Set lang to the matching key
72- break ; // Exit the loop once a match is found
73- }
74- }
75- }
68+ const oldLang = L . PM . activeLang || 'en' ; // Store the previous language, default to 'en' if undefined
69+
70+ // 2. Calculate merged translations for the event *if* override is provided
71+ let translationsForEvent = null ; // Initialize
72+ if ( override && typeof override === 'object' ) {
73+ // Find the best base for merging: intended lang -> fallback -> 'en' -> empty
74+ const baseForMerge =
75+ translations [ intendedLang ] ||
76+ translations [ originalFallbackLang ] ||
77+ translations . en ||
78+ { } ;
79+ // Calculate the merged result, but DO NOT assign back to global translations[intendedLang]
80+ translationsForEvent = merge ( { } , baseForMerge , override ) ;
7681 }
7782
78- const oldLang = L . PM . activeLang ;
79- if ( override ) {
80- translations [ lang ] = merge ( translations [ fallback ] , override ) ;
83+ // 3. Determine the final active language BASED ON ORIGINAL translations
84+ let finalLang = 'en' ; // Default final language to 'en'
85+ // Check the *original* translations, ignoring any temporary override merge
86+ if ( translations [ intendedLang ] ) {
87+ finalLang = intendedLang ; // Use intended if it now exists (due to override or initially)
88+ } else if ( translations [ originalFallbackLang ] ) {
89+ finalLang = originalFallbackLang ; // Otherwise, use fallback if it exists
8190 }
91+ // If neither intended nor fallback exists, finalLang remains 'en'
8292
83- L . PM . activeLang = lang ;
84- this . map . pm . Toolbar . reinit ( ) ;
85- this . _fireLangChange ( oldLang , lang , fallback , translations [ lang ] ) ;
93+ // 4. Set active language
94+ L . PM . activeLang = finalLang ;
95+ // 5. Reinitialize toolbar and fire event if the language actually changed
96+ if ( oldLang !== L . PM . activeLang ) {
97+ this . map . pm . Toolbar . reinit ( ) ;
98+ // Use the potentially merged translations *only* for the event payload
99+ // Fallback to the standard active translations if no override was performed
100+ const activeTranslations =
101+ translationsForEvent ||
102+ translations [ L . PM . activeLang ] ||
103+ translations . en ||
104+ { } ;
105+ this . _fireLangChange (
106+ oldLang ,
107+ L . PM . activeLang ,
108+ originalFallbackLang ,
109+ activeTranslations
110+ ) ;
111+ }
86112 } ,
87113 addControls ( options ) {
88114 this . Toolbar . addControls ( options ) ;
0 commit comments