Files
marco/app/utils/osm.js
2026-02-23 20:16:24 +04:00

58 lines
1.3 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;
}
export function getPlaceType(tags) {
if (!tags) return null;
const rawType =
tags.amenity ||
tags.shop ||
tags.tourism ||
tags.leisure ||
tags.historic ||
tags.office ||
tags.craft ||
tags.building ||
tags.landuse ||
tags.place ||
tags.natural ||
tags.public_transport ||
tags.aeroway ||
tags.border_type ||
tags.admin_title;
return humanizeOsmTag(rawType);
}