-
Notifications
You must be signed in to change notification settings - Fork 148
feat:add map language control #385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e53907f
c114189
e7162f1
fbce090
a098f0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| function getMapLanguageKey(directionsLanguage: string): string | null { | ||
| const base = directionsLanguage.split('-')[0]; | ||
| if (base === 'en' || base === 'de') { | ||
| return base; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function updateMapLabels( | ||
| map: maplibregl.Map | undefined, | ||
| directionsLanguage: string | ||
| ) { | ||
| if (!map) return; | ||
|
|
||
| const style = map.getStyle(); | ||
| if (!style?.layers) return; | ||
|
|
||
| const langKey = getMapLanguageKey(directionsLanguage); | ||
|
|
||
| for (const layer of style.layers) { | ||
| if (layer.type !== 'symbol') continue; | ||
|
|
||
| const textField = (layer.layout as Record<string, unknown>)?.['text-field']; | ||
|
|
||
| // Shortbread: simple string like "{name}", "{name_en}", "{name_de}" | ||
| if (typeof textField === 'string') { | ||
| if (!textField.match(/\{name(_[a-z]{2})?\}/)) continue; | ||
|
|
||
| const newTextField = langKey ? `{name_${langKey}}` : '{name}'; | ||
| map.setLayoutProperty(layer.id, 'text-field', newTextField); | ||
| continue; | ||
| } | ||
|
|
||
| // Alidade Smooth: expression-based, e.g. ["get", "name:latin"] | ||
| // or ["coalesce", ["get", "name:en"], ["get", "name:latin"]] | ||
| if (Array.isArray(textField)) { | ||
| const json = JSON.stringify(textField); | ||
| if (!json.includes('"name:') && !json.includes('"name"')) continue; | ||
|
Comment on lines
+25
to
+38
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these ifs look a bit "smelly". I'm not sure if somehow making this more dependent on the MVT endpoint instead of field types (which is still a provider dependency, any MVT source can do anything to support languages) or name or whatever would make it more or less "smelly". wdyt @mustaphaturhan ? |
||
|
|
||
| if (langKey) { | ||
| map.setLayoutProperty(layer.id, 'text-field', [ | ||
| 'coalesce', | ||
| ['get', `name:${langKey}`], | ||
| ['get', 'name:latin'], | ||
| ['get', 'name'], | ||
| ]); | ||
| } else { | ||
| map.setLayoutProperty(layer.id, 'text-field', [ | ||
| 'coalesce', | ||
| ['get', 'name:latin'], | ||
| ['get', 'name'], | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍