Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@mui/lab": "^7.0.0-beta.14",
"@mui/material": "^7.1.2",
"@mui/material-nextjs": "^7.1.1",
"@openstreetmap/id-tagging-schema": "^6.12.0",
"@openstreetmap/id-tagging-schema": "^7.0.1",
"@sentry/nextjs": "^8.34.0",
"@teritorio/openmaptiles-gl-language": "^1.5.4",
"@turf/turf": "^7.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchBox/options/preset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const getPresetsForSearch = async () => {
tags,
tagsAsOneString: tagsAsStrings.join(', '),
texts: [
...getPresetTermsTranslation(presetKey).split(','),
...getPresetTermsTranslation(presetKey),
...tagsAsStrings,
presetKey,
],
Expand Down
28 changes: 14 additions & 14 deletions src/services/tagging/__tests__/idTaggingScheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ describe('idTaggingScheme', () => {
'level',
'polling_station',
'wheelchair',
'wikimedia_commons',
'wikidata',
'start_date',
'short_name',
'reg_name',
'panoramax',
'official_name',
'note',
'nat_name',
'mapillary',
'loc_name',
'fixme',
'ele_node',
'description',
'alt_name',
'description',
'ele_node',
'fixme',
'loc_name',
'mapillary',
'nat_name',
'note',
'official_name',
'panoramax',
'ref/linz/place_id-NZ',
'reg_name',
'short_name',
'start_date',
'wikidata',
'wikimedia_commons',
'architect',
]);
});
Expand Down
39 changes: 39 additions & 0 deletions src/services/tagging/__tests__/translations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { intl } from '../../intl';
import {
getPresetTermsTranslation,
mockSchemaTranslations,
} from '../translations';

intl.lang = 'en';

describe('getPresetTermsTranslation', () => {
it('returns terms as-is when already a string[] (current id-tagging-schema format)', () => {
mockSchemaTranslations({
en: { presets: { presets: { shop: { terms: ['retailer', 'store'] } } } },
});

expect(getPresetTermsTranslation('shop')).toEqual(['retailer', 'store']);
});

it('splits a comma-separated string (older id-tagging-schema format)', () => {
mockSchemaTranslations({
en: { presets: { presets: { shop: { terms: 'retailer,store' } } } },
});

expect(getPresetTermsTranslation('shop')).toEqual(['retailer', 'store']);
});

it('returns [] when the preset has no terms', () => {
mockSchemaTranslations({
en: { presets: { presets: { shop: { name: 'Shop' } } } },
});

expect(getPresetTermsTranslation('shop')).toEqual([]);
});

it('returns [] for an unknown preset key', () => {
mockSchemaTranslations({ en: { presets: { presets: {} } } });

expect(getPresetTermsTranslation('does-not-exist')).toEqual([]);
});
});
17 changes: 14 additions & 3 deletions src/services/tagging/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import { getOurTranslations } from './ourPresets';
// https://cdn.jsdelivr.net/npm/@openstreetmap/id-tagging-schema@6.1.0/dist/translations/en.min.json
const cdnUrl = `https://cdn.jsdelivr.net/npm/@openstreetmap/id-tagging-schema`;

// Pinned to the same major as the `@openstreetmap/id-tagging-schema` dependency
// in package.json (bump both together) - an unpinned `latest` silently pulled
// in a breaking change to the `terms` format once (see getPresetTermsTranslation).
const CDN_VERSION_RANGE = '7';

// TODO download up-to-date or use node_module?
let translations = {};
export const fetchSchemaTranslations = async () => {
if (translations[intl.lang]) return;

try {
const presetsPackage = await fetchJson(`${cdnUrl}/package.json`);
const presetsPackage = await fetchJson(
`${cdnUrl}@${CDN_VERSION_RANGE}/package.json`,
);
const { version } = presetsPackage;

// this request is cached in browser
Expand Down Expand Up @@ -43,8 +50,12 @@ export const mockSchemaTranslations = (mockTranslations) => {
export const getPresetTranslation = (key: string): string =>
translations?.[intl.lang]?.presets?.presets?.[key]?.name ?? `[${key}]`;

export const getPresetTermsTranslation = (key: string) =>
translations?.[intl.lang]?.presets?.presets?.[key]?.terms ?? '';
export const getPresetTermsTranslation = (key: string): string[] => {
const terms = translations?.[intl.lang]?.presets?.presets?.[key]?.terms;
if (Array.isArray(terms)) return terms;
if (typeof terms === 'string') return terms ? terms.split(',') : [];
return [];
};

export const getAllTranslations = () => translations?.[intl.lang];

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"allowArbitraryExtensions": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1515,10 +1515,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@openstreetmap/id-tagging-schema@^6.12.0":
version "6.12.0"
resolved "https://registry.yarnpkg.com/@openstreetmap/id-tagging-schema/-/id-tagging-schema-6.12.0.tgz#963c0340850b4e40680a87344fea89e90b9a1a70"
integrity sha512-uAe0YyXv/QjQGhudc2FBmTg+TT+mks8H8p1FlN/hby9KKpvF9CwLU/ZLe3oDqK0jye6t3GlJ9HAyN/hxk7fTwQ==
"@openstreetmap/id-tagging-schema@^7.0.1":
version "7.0.1"
resolved "https://registry.yarnpkg.com/@openstreetmap/id-tagging-schema/-/id-tagging-schema-7.0.1.tgz#c383d34ba993ad7488f485cb8fd64e548e8022ce"
integrity sha512-y58T1905O3SJZlf3SLjhKkxxLsZAvYRNpmjE8zsag39lHh23f0IC20wNRDGrl9iUcBdD8zNs5Z/DxEDa3+Fbcg==

"@opentelemetry/api-logs@0.53.0":
version "0.53.0"
Expand Down
Loading