Skip to content

Commit 2d2a50b

Browse files
DennisOSRMCopilot
andauthored
fix(i18n): translate profile selector labels on language change (#446)
- Add labelKey and default mappings for driving/bike/foot in src/leaflet_options.js - Build and translate services before creating mode selector and use services globally in src/index.js Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7e5aef4 commit 2d2a50b

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

src/index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ var parsedOptions = links.parse(window.location.search.slice(1));
2727
var mergedOptions = L.extend(leafletOptions.defaultState, parsedOptions);
2828
var 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
3440
var 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

215217
var router = (new L.Routing.OSRMv1(controlOptions));
216218
routerPatches.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) {
335337
if (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

src/leaflet_options.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,19 @@ var defaultLayer = layerMap[getDefaultLayer()] || streets;
312312
// Each service has a name, URL prefix, and internal profile for routing
313313
function buildServices() {
314314
var modes = parseModes();
315+
var defaultLabelMapping = {
316+
driving: 'Car',
317+
bike: 'Bike',
318+
foot: 'Foot',
319+
default: 'Car'
320+
};
315321
return modes.map(function(mode) {
322+
var name = mode.name;
323+
var label = mode.label || name;
324+
var labelKey = mode.labelKey || defaultLabelMapping[name] || label;
316325
return {
317-
label: mode.name,
326+
label: label,
327+
labelKey: labelKey,
318328
path: mode.path || (mode.url + '/route/v1'),
319329
profile: mode.profile
320330
};

0 commit comments

Comments
 (0)