Files
marco/app/utils/osm.js
Râu Cao 7bcb572dbf If place key's value is "yes", display key instead
For example, building=yes with no other useful tags (e.g. amenity) will
show as Building now
2026-02-24 11:46:59 +04:00

71 lines
1.4 KiB
JavaScript

import { humanizeOsmTag } from './format-text';
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;
}
const PLACE_TYPE_KEYS = [
'amenity',
'shop',
'tourism',
'historic',
'leisure',
'office',
'craft',
'building',
'landuse',
'public_transport',
'highway',
'aeroway',
'waterway',
'natural',
'place',
'border_type',
'admin_title',
];
export function getPlaceType(tags) {
if (!tags) return null;
for (const key of PLACE_TYPE_KEYS) {
const value = tags[key];
if (value) {
if (value === 'yes') {
return humanizeOsmTag(key);
}
return humanizeOsmTag(value);
}
}
return null;
}