Skip to content

Commit

Permalink
RD-315: sourcing the language list from the client library (#113)
Browse files Browse the repository at this point in the history
* Using the languages from client lib

* changed some order in languages

* testing local client lib

* updage client lib + lint
  • Loading branch information
jonathanlurie authored Sep 9, 2024
1 parent 5f036b4 commit 317f1cf
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 213 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Others
- Updating from MapLibre v4.4.1 to v4.5.2
- Updating `MapTilerGeolocateControl` to match latest Maplibre update (https://github.com/maptiler/maptiler-sdk-js/pull/104)
- Now sourcing language list from `@maptiler/client` (https://github.com/maptiler/maptiler-client-js/pull/42)

## 2.2.2
### Bug Fixes
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maptiler/sdk",
"version": "2.2.2",
"version": "2.3.0",
"description": "The Javascript & TypeScript map SDK tailored for MapTiler Cloud",
"module": "dist/maptiler-sdk.mjs",
"types": "dist/maptiler-sdk.d.ts",
Expand Down Expand Up @@ -61,7 +61,7 @@
"vitest": "^0.34.2"
},
"dependencies": {
"@maptiler/client": "^1.8.1",
"@maptiler/client": "^2.0.0",
"events": "^3.3.0",
"js-base64": "^3.7.4",
"maplibre-gl": "^4.6.0",
Expand Down
119 changes: 68 additions & 51 deletions src/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import { config, MAPTILER_SESSION_ID, type SdkConfig } from "./config";
import { defaults } from "./defaults";
import { MaptilerLogoControl } from "./MaptilerLogoControl";
import { combineTransformRequest, displayNoWebGlWarning } from "./tools";
import { getBrowserLanguage, isLanguageSupported, Language, type LanguageString } from "./language";
import { getBrowserLanguage, Language, type LanguageInfo } from "./language";
import { styleToStyle } from "./mapstyle";
import { MaptilerTerrainControl } from "./MaptilerTerrainControl";
import { MaptilerNavigationControl } from "./MaptilerNavigationControl";
import { geolocation } from "@maptiler/client";
import { geolocation, getLanguageInfoFromFlag, toLanguageInfo } from "@maptiler/client";
import { MaptilerGeolocateControl } from "./MaptilerGeolocateControl";
import { ScaleControl } from "./MLAdapters/ScaleControl";
import { FullscreenControl } from "./MLAdapters/FullscreenControl";
Expand Down Expand Up @@ -79,7 +79,7 @@ export type MapOptions = Omit<MapOptionsML, "style" | "maplibreLogo"> & {
* or with a built-in shorthand (eg. Language.ENGLISH).
* Note that this is equivalent to setting the `config.primaryLanguage` and will overwrite it.
*/
language?: LanguageString;
language?: LanguageInfo;

/**
* Define the MapTiler Cloud API key to be used. This is strictly equivalent to setting
Expand Down Expand Up @@ -179,7 +179,7 @@ export type MapOptions = Omit<MapOptionsML, "style" | "maplibreLogo"> & {
export class Map extends maplibregl.Map {
private isTerrainEnabled = false;
private terrainExaggeration = 1;
private primaryLanguage: LanguageString;
private primaryLanguage: LanguageInfo;
private terrainGrowing = false;
private terrainFlattening = false;
private minimap?: Minimap;
Expand Down Expand Up @@ -788,15 +788,15 @@ export class Map extends maplibregl.Map {
return super.setGlyphs(glyphsUrl, options);
}

private getStyleLanguage(): string | null {
private getStyleLanguage(): LanguageInfo | null {
if (!this.style.stylesheet.metadata) return null;
if (typeof this.style.stylesheet.metadata !== "object") return null;

if (
"maptiler:language" in this.style.stylesheet.metadata &&
typeof this.style.stylesheet.metadata["maptiler:language"] === "string"
) {
return this.style.stylesheet.metadata["maptiler:language"];
return getLanguageInfoFromFlag(this.style.stylesheet.metadata["maptiler:language"]);
}

return null;
Expand All @@ -805,7 +805,7 @@ export class Map extends maplibregl.Map {
/**
* Define the primary language of the map. Note that not all the languages shorthands provided are available.
*/
setLanguage(language: LanguageString | string): void {
setLanguage(language: LanguageInfo | string): void {
this.minimap?.map?.setLanguage(language);
this.onStyleReady(() => {
this.setPrimaryLanguage(language);
Expand All @@ -816,13 +816,27 @@ export class Map extends maplibregl.Map {
* Define the primary language of the map. Note that not all the languages shorthands provided are available.
*/

private setPrimaryLanguage(language: LanguageString | string) {
private setPrimaryLanguage(lang: LanguageInfo | string) {
const styleLanguage = this.getStyleLanguage();

// lang could be a string and we want to manipulate a LanguageInfo object instead
const language = toLanguageInfo(lang, Language);

if (!language) {
console.warn(`The language "${language}" is not supported.`);
return;
}

// If the language is set to `STYLE` (which is the SDK default), but the language defined in
// the style is `auto`, we need to bypass some verification and modify the languages anyway
if (!(language === Language.STYLE && (styleLanguage === Language.AUTO || styleLanguage === Language.VISITOR))) {
if (language !== Language.STYLE) {
if (
!(
language.flag === Language.STYLE.flag &&
styleLanguage &&
(styleLanguage.flag === Language.AUTO.flag || styleLanguage.flag === Language.VISITOR.flag)
)
) {
if (language.flag !== Language.STYLE.flag) {
this.languageAlwaysBeenStyle = false;
}

Expand All @@ -836,87 +850,90 @@ export class Map extends maplibregl.Map {
}
}

if (!isLanguageSupported(language as string)) {
console.warn(`The language "${language}" is not supported.`);
return;
}

if (this.primaryLanguage === Language.STYLE_LOCK) {
if (this.primaryLanguage.flag === Language.STYLE_LOCK.flag) {
console.warn(
"The language cannot be changed because this map has been instantiated with the STYLE_LOCK language flag.",
);
return;
}

this.primaryLanguage = language as LanguageString;
let languageNonStyle: LanguageString = language as LanguageString;
this.primaryLanguage = language;
let languageNonStyle = language;

// STYLE needs to be translated into one of the other language,
// this is why it's addressed first
if (language === Language.STYLE) {
if (language.flag === Language.STYLE.flag) {
if (!styleLanguage) {
console.warn("The style has no default languages.");
return;
}

if (!isLanguageSupported(styleLanguage)) {
console.warn("The language defined in the style is not valid.");
console.warn("The style has no default languages or has an invalid one.");
return;
}

languageNonStyle = styleLanguage as LanguageString;
languageNonStyle = styleLanguage;
}

// may be overwritten below
let langStr: string | LanguageString = Language.LOCAL;
let langStr = Language.LOCAL.flag;

// will be overwritten below
let replacer: ExpressionSpecification | string = `{${langStr}}`;

if (languageNonStyle === Language.VISITOR) {
langStr = getBrowserLanguage();
if (languageNonStyle.flag === Language.VISITOR.flag) {
langStr = getBrowserLanguage().flag;
replacer = [
"case",
["all", ["has", langStr], ["has", Language.LOCAL]],
["all", ["has", langStr], ["has", Language.LOCAL.flag]],
[
"case",
["==", ["get", langStr], ["get", Language.LOCAL]],
["get", Language.LOCAL],

["format", ["get", langStr], { "font-scale": 0.8 }, "\n", ["get", Language.LOCAL], { "font-scale": 1.1 }],
["==", ["get", langStr], ["get", Language.LOCAL.flag]],
["get", Language.LOCAL.flag],

[
"format",
["get", langStr],
{ "font-scale": 0.8 },
"\n",
["get", Language.LOCAL.flag],
{ "font-scale": 1.1 },
],
],

["get", Language.LOCAL],
["get", Language.LOCAL.flag],
];
} else if (languageNonStyle === Language.VISITOR_ENGLISH) {
langStr = Language.ENGLISH;
} else if (languageNonStyle.flag === Language.VISITOR_ENGLISH.flag) {
langStr = Language.ENGLISH.flag;
replacer = [
"case",
["all", ["has", langStr], ["has", Language.LOCAL]],
["all", ["has", langStr], ["has", Language.LOCAL.flag]],
[
"case",
["==", ["get", langStr], ["get", Language.LOCAL]],
["get", Language.LOCAL],

["format", ["get", langStr], { "font-scale": 0.8 }, "\n", ["get", Language.LOCAL], { "font-scale": 1.1 }],
["==", ["get", langStr], ["get", Language.LOCAL.flag]],
["get", Language.LOCAL.flag],

[
"format",
["get", langStr],
{ "font-scale": 0.8 },
"\n",
["get", Language.LOCAL.flag],
{ "font-scale": 1.1 },
],
],
["get", Language.LOCAL],
["get", Language.LOCAL.flag],
];
} else if (languageNonStyle === Language.AUTO) {
langStr = getBrowserLanguage();
replacer = ["case", ["has", langStr], ["get", langStr], ["get", Language.LOCAL]];
} else if (languageNonStyle.flag === Language.AUTO.flag) {
langStr = getBrowserLanguage().flag;
replacer = ["case", ["has", langStr], ["get", langStr], ["get", Language.LOCAL.flag]];
}

// This is for using the regular names as {name}
else if (languageNonStyle === Language.LOCAL) {
langStr = Language.LOCAL;
langStr = Language.LOCAL.flag;
replacer = `{${langStr}}`;
}

// This section is for the regular language ISO codes
else {
langStr = languageNonStyle;
replacer = ["case", ["has", langStr], ["get", langStr], ["get", Language.LOCAL]];
langStr = languageNonStyle.flag;
replacer = ["case", ["has", langStr], ["get", langStr], ["get", Language.LOCAL.flag]];
}

const { layers } = this.getStyle();
Expand Down Expand Up @@ -975,7 +992,7 @@ export class Map extends maplibregl.Map {
* Get the primary language
* @returns
*/
getPrimaryLanguage(): LanguageString {
getPrimaryLanguage(): LanguageInfo {
return this.primaryLanguage;
}

Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EventEmitter from "events";
import type { LanguageString } from "./language";
import type { LanguageInfo } from "./language";
import { config as clientConfig, type FetchFunction } from "@maptiler/client";
import { v4 as uuidv4 } from "uuid";
import type { Unit } from "./unit";
Expand All @@ -14,13 +14,13 @@ class SdkConfig extends EventEmitter {
/**
* The primary language. By default, the language of the web browser is used.
*/
primaryLanguage: LanguageString = defaults.primaryLanguage;
primaryLanguage: LanguageInfo = defaults.primaryLanguage;

/**
* The secondary language, to overwrite the default language defined in the map style.
* This settings is highly dependant on the style compatibility and may not work in most cases.
*/
secondaryLanguage?: LanguageString;
secondaryLanguage?: LanguageInfo;

/**
* Setting on whether of not the SDK runs with a session logic.
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ export {
type GeolocationInfoOptions,
type GeolocationResult,
type GetDataOptions,
LanguageGeocoding,
type LanguageGeocodingOptions,
type LanguageGeocodingString,
MapStyle,
type MapStylePreset,
type MapStyleType,
Expand All @@ -243,14 +241,21 @@ export {
expandMapStyle,
geocoding,
geolocation,
getAutoLanguageGeocoding,
getBufferToPixelDataParser,
getTileCache,
mapStylePresetList,
math,
misc,
staticMaps,
styleToStyle,
type LanguageInfo,
areSameLanguages,
toLanguageInfo,
isLanguageInfo,
getAutoLanguage,
getLanguageInfoFromFlag,
getLanguageInfoFromCode,
getLanguageInfoFromKey,
} from "@maptiler/client";

export { config, SdkConfig } from "./config";
Expand Down
Loading

0 comments on commit 317f1cf

Please sign in to comment.