Skip to content

feat: partial port of #381. prefer browser language & safe config.json fetch#440

Merged
DennisOSRM merged 7 commits intogh-pagesfrom
feature/partial-port-381-language-default
May 3, 2026
Merged

feat: partial port of #381. prefer browser language & safe config.json fetch#440
DennisOSRM merged 7 commits intogh-pagesfrom
feature/partial-port-381-language-default

Conversation

@DennisOSRM
Copy link
Copy Markdown
Contributor

Partial port of functionality from #381 (large and outdated).

This PR:

  • Makes English the default language when not set in the URL or deducable from browser settings.
    Precedence: URL param (hl) > browser default > English.
  • Adds unit tests covering the language detection/selection logic (navigator.languages, navigator.language, regional variants, primary-subtag matching, and fallback to English).
  • Avoids noisy 404s by only fetching config.json when running in Docker (index.html + docker/entrypoint.sh changes).

All tests and linting passed locally.

This is a partial port of #381 — that PR is large and outdated; this extracts the relevant, up-to-date pieces.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

fbarbe00 and others added 6 commits May 3, 2026 13:19
…nPrecedence: URL param > browser language > runtime config > English\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…nPrecedence: URL param > browser language > English\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ig.json when OSRM_ENVIRONMENT='docker' to avoid noisy 404s in non-Docker environments.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tor cases and URL precedence

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 3, 2026 11:44
@DennisOSRM DennisOSRM changed the title Partial port: prefer browser language & safe config.json fetch feat: partial port of #381. prefer browser language & safe config.json fetch May 3, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR partially ports language-selection and runtime-config loading behavior so the frontend prefers the browser’s language over a hardcoded default, and avoids fetching config.json unless the app is running in the Docker runtime path.

Changes:

  • Reworks default language selection in leaflet_options to prefer browser locale candidates and fall back to English.
  • Replaces the old language override tests with coverage for browser-language matching and URL hl precedence.
  • Adds a Docker-specific signal in index.html and docker/entrypoint.sh so config.json is only fetched when the Docker entrypoint prepared runtime config.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/leaflet_options.js Adds browser-language detection logic and changes the default language fallback path.
test/leaflet_options.test.js Updates unit tests to cover browser locale selection and URL language precedence.
index.html Adds a meta-based environment check before loading config.json.
docker/entrypoint.sh Rewrites the new meta tag at container startup to indicate Docker runtime mode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/leaflet_options.js Outdated
Comment on lines +250 to +256
// exact match (e.g., 'pt-BR')
if (localization.get(lang)) {
return lang;
}
// primary subtag match (e.g., 'en-US' -> 'en')
var short = lang.split('-')[0];
if (localization.get(short)) {
Comment thread docker/entrypoint.sh
Comment on lines +64 to +69
awk -v env="$OSRM_ENVIRONMENT" '{
if ($0 ~ /<meta name="osrm-environment"/) {
sub(/content="[^"]*"/, "content=\"" env "\"")
}
print
}' /usr/share/nginx/html/index.html > "$TMPFILE" && mv "$TMPFILE" /usr/share/nginx/html/index.html || true
Comment on lines +201 to 247
describe('language precedence (URL > browser > en)', () => {
test('uses browser language exact match', () => {
global.window = { navigator: { languages: ['de'], language: 'de' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('de');
delete global.window;
});

test('defaults to en when language not provided', () => {
global.window = { osrmConfig: {} };
test('uses primary subtag when regional locale provided (en-US -> en)', () => {
global.window = { navigator: { languages: ['en-US'], language: 'en-US' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('en');
delete global.window;
});

test('prefers first candidate in navigator.languages array', () => {
global.window = { navigator: { languages: ['fr-CA', 'de'], language: 'fr-CA' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('fr');
delete global.window;
});

test('matches exact regional variant when available (pt-BR)', () => {
global.window = { navigator: { languages: ['pt-BR'], language: 'pt-BR' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('pt-BR');
delete global.window;
});

test('falls back to English when no supported browser languages', () => {
global.window = { navigator: { languages: ['xx','yy'], language: 'xx' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('en');
delete global.window;
});

test('URL param (hl) takes precedence over browser default when merged', () => {
// Simulate browser default 'en' but URL param asks for 'de'
global.window = { navigator: { languages: ['en'], language: 'en' } };
const leafletOptions = require('../src/leaflet_options');
const links = require('../src/links');
const parsed = links.parse('hl=de');
const merged = Object.assign({}, leafletOptions.defaultState, parsed);
expect(merged.language).toBe('de');
delete global.window;
});
});
Comment on lines +202 to +214
test('uses browser language exact match', () => {
global.window = { navigator: { languages: ['de'], language: 'de' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('de');
delete global.window;
});

test('defaults to en when language not provided', () => {
global.window = { osrmConfig: {} };
test('uses primary subtag when regional locale provided (en-US -> en)', () => {
global.window = { navigator: { languages: ['en-US'], language: 'en-US' } };
const leafletOptions = require('../src/leaflet_options');
expect(leafletOptions.defaultState.language).toBe('en');
delete global.window;
});
Comment thread src/leaflet_options.js
Comment on lines +235 to +239
if (typeof window !== 'undefined' && window.navigator) {
var nav = window.navigator;
var candidates = [];

if (Array.isArray(nav.languages)) {
…hing, add tests for language detection and entrypoint meta rewrite

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@DennisOSRM DennisOSRM merged commit be29244 into gh-pages May 3, 2026
5 checks passed
@DennisOSRM DennisOSRM deleted the feature/partial-port-381-language-default branch May 3, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants