Skip to content

Commit

Permalink
[backport 2.4] Translate layers names in API layer switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-morvan committed Sep 17, 2019
1 parent d08ec00 commit 13833e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Map from './src/Map.js';
config.themesUrl = 'https://geomapfish-demo-2-4.camptocamp.com/themes?' +
'version=2&background=background&interface=api';

config.localeUrl = 'https://geomapfish-demo-2-5.camptocamp.com/locale.json'

const lib = {
Map
};
Expand Down
36 changes: 32 additions & 4 deletions api/src/Themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ function getThemesPromise() {
return themesPromise;
}

/**
* @type {Promise<Object<string, string>>} Promise
* @hidden
*/
let localePromise;

/**
* @hidden
* @return {Promise<Object<string, string>>} Promise
*/
function getLocalePromise() {
if (!constants.localeUrl) {
// Fallback to an empty dict
return Promise.resolve({});
}
if (!localePromise) {
localePromise = fetch(constants.localeUrl).then(response => response.json()).then(data =>
// Return the first property as data should looks like { 'fr': { ... } }
data[Object.keys(data)[0]]
);
}
return localePromise;
}

/**
* @type {Promise<Map<string, overlayDefinition>>|undefined}
* @hidden
Expand Down Expand Up @@ -196,9 +220,11 @@ export function createWMSLayer(config, ogcServer) {
minResolution: config.minResolutionHint,
maxResolution: config.maxResolutionHint
});
layer.set('title', config.name);
layer.set('config.name', config.name);
return Promise.resolve(layer);
return getLocalePromise().then(translations => {
layer.set('title', translations[config.name] || config.name);
return layer;
});
}

/**
Expand All @@ -207,7 +233,9 @@ export function createWMSLayer(config, ogcServer) {
* @hidden
*/
export function createWMTSLayer(config) {
return getWMTSCapability(config.url).then((capability) => {
return Promise.all([getLocalePromise(), getWMTSCapability(config.url)]).then(result => {
const translations = result[0];
const capability = result[1];
const options = optionsFromCapabilities(capability, {
crossOrigin: 'anonymous',
layer: config.layer,
Expand All @@ -219,7 +247,7 @@ export function createWMTSLayer(config) {
preload: Infinity,
source: source
});
layer.set('title', config.name);
layer.set('title', translations[config.name] || config.name);
layer.set('config.name', config.name);
return layer;
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import EPSG21781 from '@geoblocks/proj/src/EPSG_21781.js';

export default {
themesUrl: undefined,
localeUrl: undefined,

projection: EPSG21781,
resolutions: [250, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.25, 0.1, 0.05],
extent: undefined,
Expand Down

0 comments on commit 13833e5

Please sign in to comment.