@@ -27,8 +27,14 @@ var parsedOptions = links.parse(window.location.search.slice(1));
2727var mergedOptions = L . extend ( leafletOptions . defaultState , parsedOptions ) ;
2828var language = mergedOptions . language ;
2929
30- // Create mode selector early so it can be injected when geocoders are created
31- var modeSelector = modeSelectorModule . createModeSelector ( localization . get ( language ) , leafletOptions . services ) ;
30+ // Build and translate services early so modeSelector can use translated labels
31+ var services = leafletOptions . services ;
32+ for ( var i = 0 , len = services . length ; i < len ; i ++ ) {
33+ var profileLabelKey = services [ i ] . labelKey || services [ i ] . label ;
34+ services [ i ] . labelKey = profileLabelKey ;
35+ services [ i ] . label = localization . t ( language , profileLabelKey ) || profileLabelKey ;
36+ }
37+ var modeSelector = modeSelectorModule . createModeSelector ( localization . get ( language ) , services ) ;
3238
3339// load only after language was chosen
3440var ItineraryBuilder = require ( './itinerary_builder' ) ( mergedOptions . language ) ;
@@ -174,20 +180,16 @@ var controlOptions = {
174180 language : mergedOptions . language , // we are injecting own translations via osrm-text-instructions
175181 showAlternatives : options . lrm . showAlternatives ,
176182 units : mergedOptions . units ,
177- serviceUrl : leafletOptions . services [ 0 ] . path ,
183+ serviceUrl : services [ 0 ] . path ,
178184 useHints : false ,
179- services : leafletOptions . services ,
185+ services : services ,
180186 useZoomParameter : options . lrm . useZoomParameter ,
181187 routeDragInterval : options . lrm . routeDragInterval ,
182188 collapsible : options . lrm . collapsible ,
183189 itineraryBuilder : new ItineraryBuilder ( )
184190} ;
185- // translate profile names
186- for ( var profile = 0 , len = controlOptions . services . length ; profile < len ; profile ++ ) {
187- var profileLabelKey = controlOptions . services [ profile ] . labelKey || controlOptions . services [ profile ] . label ;
188- controlOptions . services [ profile ] . labelKey = profileLabelKey ;
189- controlOptions . services [ profile ] . label = localization . t ( language , profileLabelKey ) || profileLabelKey ;
190- }
191+ // profile labels already translated earlier
192+
191193
192194// Load and set initial profile BEFORE creating router and lrmControl
193195// This ensures the router uses the correct serviceUrl when calculating initial routes
@@ -204,13 +206,13 @@ if (urlProfile !== undefined && urlProfile !== null) {
204206}
205207
206208// Ensure valid profile index
207- if ( activeProfileIndex < 0 || activeProfileIndex >= leafletOptions . services . length ) {
209+ if ( activeProfileIndex < 0 || activeProfileIndex >= services . length ) {
208210 activeProfileIndex = 0 ;
209211}
210212
211213// Set the initial serviceUrl and profile on controlOptions
212- controlOptions . serviceUrl = leafletOptions . services [ activeProfileIndex ] . path ;
213- controlOptions . profile = leafletOptions . services [ activeProfileIndex ] . profile ;
214+ controlOptions . serviceUrl = services [ activeProfileIndex ] . path ;
215+ controlOptions . profile = services [ activeProfileIndex ] . profile ;
214216
215217var router = ( new L . Routing . OSRMv1 ( controlOptions ) ) ;
216218routerPatches . applyPatches ( router ) ;
@@ -294,7 +296,7 @@ if (toolsControl && toolsControl.on) {
294296 L . DomEvent . on ( modeSelector . select , 'change' , function ( event ) {
295297 var profileIndex = parseInt ( event . target . value , 10 ) ;
296298 clearProfileSelectorSelection ( event . target ) ;
297- routerPatches . setActiveService ( router , profileIndex , leafletOptions . services ) ;
299+ routerPatches . setActiveService ( router , profileIndex , services ) ;
298300 ls . set ( 'profile' , profileIndex ) ;
299301
300302 // Also update the state object so profile is preserved on language change
@@ -335,7 +337,7 @@ if (toolsControl && toolsControl.on) {
335337if ( modeSelector && modeSelector . select ) {
336338 L . DomEvent . on ( modeSelector . select , 'change' , function ( event ) {
337339 var profileIndex = parseInt ( event . target . value , 10 ) ;
338- var selectedProfile = leafletOptions . services [ profileIndex ] && leafletOptions . services [ profileIndex ] . profile ;
340+ var selectedProfile = services [ profileIndex ] && services [ profileIndex ] . profile ;
339341 var bikeLayer = overlay && overlay [ 'Bike' ] ;
340342 if ( ! bikeLayer ) return ;
341343
0 commit comments