Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add localization for opensearch base map #312

Merged
merged 5 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { i18n } from '@osd/i18n';

export const fromMBtoBytes = (sizeInMB: number) => {
return sizeInMB * 1024 * 1024;
};

//refer https://github.com/opensearch-project/i18n-plugin/blob/main/DEVELOPER_GUIDE.md#new-locale for OSD supported languages
const OSD_LANGUAGES = ['en', 'es', 'fr', 'de', 'ja', 'ko', 'zh'], // all these codes are also supported in vector tiles map
FALLBACK_LANGUAGE = 'en';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


export const getMapLanguage = () => {
const OSDLanguage = i18n.getLocale().toLowerCase(),
parts = OSDLanguage.split('-');
const languageCode = parts.length > 1 ? parts[0] : OSDLanguage;
return OSD_LANGUAGES.includes(languageCode) ? languageCode : FALLBACK_LANGUAGE;
};
15 changes: 14 additions & 1 deletion public/model/OSMLayerFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Map as Maplibre, LayerSpecification } from 'maplibre-gl';
import { Map as Maplibre, LayerSpecification, SymbolLayerSpecification } from 'maplibre-gl';
import { OSMLayerSpecification } from './mapLayerType';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { getLayers, hasLayer } from './map/layer_operations';
import { getMapLanguage } from '../../common/util';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -41,6 +42,17 @@ const updateLayerConfig = (layerConfig: OSMLayerSpecification, maplibreRef: Mapl
handleStyleLayers(layerConfig, maplibreRef);
};

const localizationMapLayer = (maplibreRef: MaplibreRef, styleLayer: LayerSpecification) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: setLanguage

// if a layer contains label, we will set its language.
if (styleLayer.layout && (styleLayer as SymbolLayerSpecification).layout?.['text-field']) {
const language = getMapLanguage();
maplibreRef.current?.setLayoutProperty(styleLayer.id, 'text-field', [
'get',
'name:' + language,
]);
}
};

const addNewLayer = (
layerConfig: OSMLayerSpecification,
maplibreRef: MaplibreRef,
Expand All @@ -61,6 +73,7 @@ const addNewLayer = (
styleLayer.source = layerConfig.id;
}
maplibreRef.current?.addLayer(styleLayer, beforeMbLayerId);
localizationMapLayer(maplibreRef, styleLayer);
maplibreRef.current?.setLayoutProperty(styleLayer.id, 'visibility', layerConfig.visibility);
maplibreRef.current?.setLayerZoomRange(
styleLayer.id,
Expand Down