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

[Backport 2.x] feat: add localization for opensearch base map #351

Merged
merged 1 commit into from
Mar 20, 2023
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
4 changes: 4 additions & 0 deletions common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export const LAYER_ICON_TYPE_MAP: { [key: string]: string } = {
[DASHBOARDS_MAPS_LAYER_TYPE.CUSTOM_MAP]: 'globe',
};

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

export enum TOOLTIP_STATE {
DISPLAY_FEATURES = 'DISPLAY_FEATURES',
FILTER_DRAW_SHAPE = 'FILTER_DRAW_SHAPE',
Expand Down
9 changes: 9 additions & 0 deletions common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { i18n } from '@osd/i18n';
import { OSD_LANGUAGES, FALLBACK_LANGUAGE } from './index';

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

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 setLanguage = (maplibreRef: MaplibreRef, styleLayer: LayerSpecification) => {
// 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);
setLanguage(maplibreRef, styleLayer);
maplibreRef.current?.setLayoutProperty(styleLayer.id, 'visibility', layerConfig.visibility);
maplibreRef.current?.setLayerZoomRange(
styleLayer.id,
Expand Down