Skip to content

Commit

Permalink
Merge pull request #578 from ZeLonewolf/1ec5-uselang-20
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
1ec5 authored Nov 27, 2022
2 parents 0ff8c24 + f5db5ee commit 39596dc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 51 deletions.
5 changes: 5 additions & 0 deletions scripts/taginfo_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"contact_email": "zelonewolf@gmail.com"
},
"tags": [
{
"key": "name",
"object_types": ["node", "way", "relation", "area"],
"description": "Labels fall back to the local language if none of the user-preferred languages is available."
},
{
"key": "capital",
"value": "yes",
Expand Down
48 changes: 41 additions & 7 deletions src/constants/label.js
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);
12 changes: 3 additions & 9 deletions src/layer/aeroway.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
"use strict";

import * as Label from "../constants/label.js";
import * as Color from "../constants/color.js";

const name_en = [
"coalesce",
["get", "name:en"],
["get", "name:latin"],
["get", "name"],
];

const minorAirport = [
"any",
["!", ["has", "iata"]],
Expand Down Expand Up @@ -221,7 +215,7 @@ export const airportLabel = {
},
layout: {
visibility: "visible",
"text-field": name_en,
"text-field": Label.localizedName,
"text-font": ["Metropolis Bold"],
"text-size": 10,
...iconLayout,
Expand All @@ -245,7 +239,7 @@ export const minorAirportLabel = {
},
layout: {
visibility: "visible",
"text-field": name_en,
"text-field": Label.localizedName,
"text-font": ["Metropolis Bold"],
"text-size": 10,
},
Expand Down
13 changes: 3 additions & 10 deletions src/layer/park.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
"use strict";

import * as Label from "../constants/label.js";
import * as Color from "../constants/color.js";

// Name fields in order of preference
const name_en = [
"coalesce",
["get", "name:en"],
["get", "name:latin"],
["get", "name"],
];

export const fill = {
id: "protected-area-fill",
type: "fill",
Expand Down Expand Up @@ -50,7 +43,7 @@ export const label = {
},
layout: {
visibility: "visible",
"text-field": name_en,
"text-field": Label.localizedName,
"text-font": ["Metropolis Bold"],
"text-size": 10,
"symbol-sort-key": ["get", "rank"],
Expand Down Expand Up @@ -102,7 +95,7 @@ export const parkLabel = {
},
layout: {
visibility: "visible",
"text-field": name_en,
"text-field": Label.localizedName,
"text-font": ["Metropolis Bold"],
"text-size": 10,
"symbol-sort-key": ["get", "rank"],
Expand Down
20 changes: 10 additions & 10 deletions src/layer/place.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as label from "../constants/label.js";
import * as Label from "../constants/label.js";

const cityLabelPaint = {
"text-color": "#444",
Expand Down Expand Up @@ -59,7 +59,7 @@ export const village = {
[11, 0.5],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-anchor": "bottom",
"text-variable-anchor": [
"bottom",
Expand Down Expand Up @@ -122,7 +122,7 @@ export const town = {
[11, 0.7],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-anchor": "bottom",
"text-variable-anchor": [
"bottom",
Expand Down Expand Up @@ -181,7 +181,7 @@ export const city = {
[11, 0.9],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-anchor": "bottom",
"text-variable-anchor": [
"bottom",
Expand Down Expand Up @@ -223,7 +223,7 @@ export const state = {
[6, 14],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-padding": 1,
"text-transform": "uppercase",
"text-letter-spacing": 0.04,
Expand Down Expand Up @@ -266,7 +266,7 @@ export const countryOther = {
[7, 15],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-max-width": 6.25,
"text-transform": "none",
},
Expand Down Expand Up @@ -296,7 +296,7 @@ export const country3 = {
[7, 17],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-max-width": 6.25,
"text-transform": "none",
},
Expand Down Expand Up @@ -326,7 +326,7 @@ export const country2 = {
[5, 17],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-max-width": 6.25,
"text-transform": "none",
},
Expand Down Expand Up @@ -357,7 +357,7 @@ export const country1 = {
[6, 19],
],
},
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-max-width": ["step", ["zoom"], 6.25, 3, 12],
"text-transform": "none",
"text-offset": [
Expand All @@ -383,7 +383,7 @@ export const continent = {
layout: {
"text-font": ["Metropolis Light"],
"text-size": 13,
"text-field": label.name_en,
"text-field": Label.localizedName,
"text-justify": "center",
"text-transform": "uppercase",
},
Expand Down
8 changes: 2 additions & 6 deletions src/layer/transportation_label.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

import * as Label from "../constants/label.js";
import * as Color from "../constants/color.js";

const highwaySelector = ["match", ["get", "class"]];
Expand Down Expand Up @@ -79,12 +80,7 @@ export const label = {
["literal", ["Metropolis Regular Italic"]],
["literal", ["Metropolis Light"]],
],
"text-field": [
"concat",
["get", "name:latin"],
" ",
["get", "name:nonlatin"],
],
"text-field": Label.legacyLocalizedName,
"text-max-angle": 20,
"symbol-placement": "line",
"text-size": [
Expand Down
12 changes: 3 additions & 9 deletions src/layer/water.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

import * as Label from "../constants/label.js";
import * as Color from "../constants/color.js";

const bigRivers = ["river", "canal"];
Expand Down Expand Up @@ -75,16 +76,9 @@ const labelPaintProperties = {
"text-halo-blur": 0.25,
};

const nameField = [
"coalesce",
["get", "name:en"],
["get", "name_en"],
["get", "name"],
];

const labelLayoutProperties = {
"symbol-placement": "line",
"text-field": nameField,
"text-field": Label.localizedName,
"text-font": ["Metropolis Bold Italic"],
"text-max-angle": 55,
};
Expand Down Expand Up @@ -153,7 +147,7 @@ export const waterPointLabel = {
"source-layer": "water_name",
filter: ["all", ["==", ["geometry-type"], "Point"]],
layout: {
"text-field": nameField,
"text-field": Label.localizedName,
"text-font": ["Metropolis Bold Italic"],
"text-size": [
"interpolate",
Expand Down

0 comments on commit 39596dc

Please sign in to comment.