From 64ccc694d3952f692c34bce9e2cfa84bd3c5db9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 10 Feb 2026 19:19:36 +0400 Subject: [PATCH] Prefer place name in UA/browser language closes #16 --- app/components/place-details.gjs | 4 ++-- app/components/places-sidebar.gjs | 4 ++-- app/services/osm.js | 3 ++- app/utils/osm.js | 32 +++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 app/utils/osm.js diff --git a/app/components/place-details.gjs b/app/components/place-details.gjs index 5c28c5e..5e7ad89 100644 --- a/app/components/place-details.gjs +++ b/app/components/place-details.gjs @@ -2,6 +2,7 @@ import Component from '@glimmer/component'; import { fn } from '@ember/helper'; import { on } from '@ember/modifier'; import { humanizeOsmTag } from '../utils/format-text'; +import { getLocalizedName } from '../utils/osm'; import Icon from '../components/icon'; import PlaceEditForm from './place-edit-form'; @@ -22,8 +23,7 @@ export default class PlaceDetails extends Component { get name() { return ( this.place.title || - this.tags.name || - this.tags['name:en'] || + getLocalizedName(this.tags) || 'Unnamed Place' ); } diff --git a/app/components/places-sidebar.gjs b/app/components/places-sidebar.gjs index 8866d3f..359028c 100644 --- a/app/components/places-sidebar.gjs +++ b/app/components/places-sidebar.gjs @@ -7,6 +7,7 @@ import or from 'ember-truth-helpers/helpers/or'; import PlaceDetails from './place-details'; import Icon from './icon'; import humanizeOsmTag from '../helpers/humanize-osm-tag'; +import { getLocalizedName } from '../utils/osm'; export default class PlacesSidebar extends Component { @service storage; @@ -85,8 +86,7 @@ export default class PlacesSidebar extends Component { } else { // It's a fresh POI -> Save it const placeData = { - title: - place.osmTags.name || place.osmTags['name:en'] || 'Untitled Place', + title: getLocalizedName(place.osmTags, 'Untitled Place'), lat: place.lat, lon: place.lon, tags: [], diff --git a/app/services/osm.js b/app/services/osm.js index 9ba722f..d4579c6 100644 --- a/app/services/osm.js +++ b/app/services/osm.js @@ -1,4 +1,5 @@ import Service, { service } from '@ember/service'; +import { getLocalizedName } from '../utils/osm'; export default class OsmService extends Service { @service settings; @@ -61,7 +62,7 @@ out center; normalizePoi(poi) { return { - title: poi.tags?.name || poi.tags?.['name:en'] || 'Untitled Place', + title: getLocalizedName(poi.tags), lat: poi.lat || poi.center?.lat, lon: poi.lon || poi.center?.lon, url: poi.tags?.website, diff --git a/app/utils/osm.js b/app/utils/osm.js new file mode 100644 index 0000000..7974090 --- /dev/null +++ b/app/utils/osm.js @@ -0,0 +1,32 @@ +export function getLocalizedName(tags, defaultName = 'Untitled Place') { + if (!tags) return defaultName; + + // 1. Get user's preferred languages + const languages = navigator.languages || [navigator.language || 'en']; + + // 2. Try to find a match for each preferred language + for (const lang of languages) { + if (!lang) continue; + + // Handle "en-US", "de-DE", etc. -> look for "name:en", "name:de" + const shortLang = lang.split('-')[0]; + const tagKey = `name:${shortLang}`; + + if (tags[tagKey]) { + return tags[tagKey]; + } + } + + // 3. Fallback to standard "name" + if (tags.name) { + return tags.name; + } + + // 4. Fallback to "name:en" (common in international places without local name) + if (tags['name:en']) { + return tags['name:en']; + } + + // 5. Final fallback + return defaultName; +}