-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #578 from ZeLonewolf/1ec5-uselang-20
undefined
- Loading branch information
Showing
7 changed files
with
67 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,43 @@ | ||
"use strict"; | ||
|
||
// Name fields in order of preference | ||
export const name_en = [ | ||
"coalesce", | ||
["get", "name:en"], | ||
["get", "name:latin"], | ||
["get", "name"], | ||
]; | ||
/** | ||
* Returns a `coalesce` expression that resolves to the feature's name in a | ||
* language that the user prefers. | ||
* | ||
* @param {boolean} includesLegacyFields - Whether to include the older fields | ||
* that include underscores, for layers that have not transitioned to the | ||
* colon syntax. | ||
*/ | ||
function getLocalizedNameExpression(includesLegacyFields) { | ||
let userLocales = navigator.languages ?? [navigator.language]; | ||
let locales = []; | ||
let localeSet = new Set(); // avoid duplicates | ||
for (let locale of userLocales) { | ||
// Add progressively less specific variants of each user-specified locale. | ||
let components = locale.split("-"); | ||
while (components.length > 0) { | ||
let parent = components.join("-"); | ||
if (!localeSet.has(parent)) locales.push(parent); | ||
localeSet.add(parent); | ||
components.pop(); | ||
} | ||
} | ||
if (locales.at(-1) === "en") { | ||
locales.push("latin"); | ||
} | ||
let nameFields = [ | ||
...locales.flatMap((l) => { | ||
let fields = [`name:${l}`]; | ||
// transportation_label uses an underscore instead of a colon. | ||
// https://github.com/openmaptiles/openmaptiles/issues/769 | ||
if (includesLegacyFields && (l === "de" || l === "en")) | ||
fields.push(`name_${l}`); | ||
return fields; | ||
}), | ||
"name", | ||
]; | ||
return ["coalesce", ...nameFields.map((f) => ["get", f])]; | ||
} | ||
|
||
export const localizedName = getLocalizedNameExpression(false); | ||
export const legacyLocalizedName = getLocalizedNameExpression(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters