Skip to content

Commit

Permalink
feat: add utility function for localization to controls to shorten ke…
Browse files Browse the repository at this point in the history
…y lookup statement
  • Loading branch information
Grammostola committed Oct 2, 2024
1 parent 8c514a5 commit f607251
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
32 changes: 18 additions & 14 deletions src/controls/measure.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const Measure = function Measure({
const source = new VectorSource();
const modify = new Modify({ source, style: modifyStyle });

function localize(key) {
return localization.getStringByKeys({ targetParentKey: 'measure', targetKey: key });
}

function styleFunction(feature, segments, drawType, tip) {
const styleScale = feature.get('styleScale') || 1;
const labelStyle = drawStyles.getLabelStyle(styleScale);
Expand Down Expand Up @@ -191,7 +195,7 @@ const Measure = function Measure({
const styleScale = feature.get('styleScale') || 1;
const featureStyle = drawStyles.getLabelStyle(styleScale);
feature.setStyle(featureStyle);
feature.getStyle().getText().setText(localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'fetchingElevation' }));
feature.getStyle().getText().setText(localize('fetchingElevation'));

fetch(url).then(response => response.json({
cache: false
Expand Down Expand Up @@ -234,8 +238,8 @@ const Measure = function Measure({
}

function createRadiusModal(feature) {
const title = localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'bufferModalTitle' });
const ok = localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'bufferModalOkButton' });
const title = localize('bufferModalTitle');
const ok = localize('bufferModalOkButton');
const content = `<div>
<input type="number" id="bufferradius">
<button id="bufferradiusBtn">${ok}</button>
Expand Down Expand Up @@ -344,7 +348,7 @@ const Measure = function Measure({
function addInteraction() {
const drawType = type || 'LineString';
const activeTip = '';
const idleTip = localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'startMeasureTooltip' });
const idleTip = localize('startMeasureTooltip');
let tip = idleTip;
measure = new Draw({
source,
Expand Down Expand Up @@ -691,7 +695,7 @@ const Measure = function Measure({
addNode();
},
icon: '#ic_add_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'addNodeTooltip' }),
tooltipText: localize('addNodeTooltip'),
tooltipPlacement: 'east'
});
buttons.push(addNodeButton);
Expand All @@ -710,7 +714,7 @@ const Measure = function Measure({
measure.getOverlay().changed();
},
icon: '#ic_linear_scale_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'showSectionsTooltip' }),
tooltipText: localize('showSectionsTooltip'),
tooltipPlacement: 'east'
});
buttons.push(showSegmentLabelButton);
Expand Down Expand Up @@ -751,7 +755,7 @@ const Measure = function Measure({
toggleMeasure();
},
icon: '#ic_straighten_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'mainButtonTooltip' }),
tooltipText: localize('mainButtonTooltip'),
tooltipPlacement: 'east'
});
buttons.push(measureButton);
Expand All @@ -765,7 +769,7 @@ const Measure = function Measure({
},
data: { tool: 'length' },
icon: '#ic_timeline_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'lengthTooltip' }),
tooltipText: localize('lengthTooltip'),
tooltipPlacement: 'east'
});
buttons.push(lengthToolButton);
Expand All @@ -781,7 +785,7 @@ const Measure = function Measure({
},
data: { tool: 'area' },
icon: '#o_polygon_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'areaTooltip' }),
tooltipText: localize('areaTooltip'),
tooltipPlacement: 'east'
});
buttons.push(areaToolButton);
Expand All @@ -796,7 +800,7 @@ const Measure = function Measure({
},
data: { tool: 'elevation' },
icon: '#ic_height_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'elevationTooltip' }),
tooltipText: localize('elevationTooltip'),
tooltipPlacement: 'east'
});
buttons.push(elevationToolButton);
Expand All @@ -811,7 +815,7 @@ const Measure = function Measure({
},
data: { tool: 'buffer' },
icon: '#ic_adjust_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'bufferTooltip' }),
tooltipText: localize('bufferTooltip'),
tooltipPlacement: 'east'
});
buttons.push(bufferToolButton);
Expand All @@ -837,7 +841,7 @@ const Measure = function Measure({
undoLastPoint();
},
icon: '#ic_undo_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'undoTooltip' }),
tooltipText: localize('undoTooltip'),
tooltipPlacement: 'east'
});
buttons.push(undoButton);
Expand All @@ -849,7 +853,7 @@ const Measure = function Measure({
viewer.removeOverlays(overlayArray);
},
icon: '#ic_delete_24px',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'clearTooltip' }),
tooltipText: localize('clearTooltip'),
tooltipPlacement: 'east'
});
buttons.push(clearButton);
Expand All @@ -864,7 +868,7 @@ const Measure = function Measure({
toggleSnap();
},
icon: '#fa-magnet',
tooltipText: localization.getStringByKeys({ targetParentKey: 'measure', targetKey: 'snapTooltip' }),
tooltipText: localize('snapTooltip'),
tooltipPlacement: 'east'
});
buttons.push(toggleSnapButton);
Expand Down
10 changes: 7 additions & 3 deletions src/controls/sharemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ const ShareMap = function ShareMap(options = {}) {
localization
} = options;

function localize(key) {
return localization.getStringByKeys({ targetParentKey: 'sharemap', targetKey: key });
}

const {
icon = '#ic_screen_share_outline_24px',
title = localization.getStringByKeys({ targetParentKey: 'sharemap', targetKey: 'title' }),
title = localize('title'),
storeMethod,
serviceEndpoint
} = options;
Expand All @@ -22,7 +26,7 @@ const ShareMap = function ShareMap(options = {}) {
let modal;

const createContent = function createContent() { // Kopiera och klistra in länken för att dela kartan.
const shareMapInstruction = localization.getStringByKeys({ targetParentKey: 'sharemap', targetKey: 'shareMapInstruction' });
const shareMapInstruction = localize('shareMapInstruction');
return `<div class="o-share-link"><input type="text"></div><i>${shareMapInstruction}</i>`;
};

Expand Down Expand Up @@ -52,7 +56,7 @@ const ShareMap = function ShareMap(options = {}) {
click() {
mapMenu.close();
modal = Modal({
title: localization.getStringByKeys({ targetParentKey: 'sharemap', targetKey: 'linkToMap' }),
title: localize('linkToMap'),
content: createContent(),
target
});
Expand Down
4 changes: 2 additions & 2 deletions src/style/drawstyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ const tipStyle = new Style({
})
});

function modifyStyle(loc) {
function modifyStyle(localization) {
return new Style({
image: new CircleStyle({
radius: 5,
Expand All @@ -329,7 +329,7 @@ function modifyStyle(loc) {
})
}),
text: new Text({
text: loc.getStringByKeys({ targetParentKey: 'drawStyles', targetKey: 'modifyTooltip' }),
text: localization.getStringByKeys({ targetParentKey: 'drawStyles', targetKey: 'modifyTooltip' }),
font: '12px Calibri,sans-serif',
fill: new Fill({
color: 'rgba(255, 255, 255, 1)'
Expand Down

0 comments on commit f607251

Please sign in to comment.