Skip to content

Commit 3ed4b81

Browse files
Map constructor can take language as a string (#131)
* Map constructor can take language as a string * adding readme entry
1 parent a1bedfe commit 3ed4b81

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.4.1
44
### Bug Fixes
55
- The class `AJAXError` is now imported as part of the `maplibregl` namespace (CommonJS limitation from Maplibre GL JS) (https://github.com/maptiler/maptiler-sdk-js/pull/129)
6+
- The `Map` constructor can now also take a language as a string (https://github.com/maptiler/maptiler-sdk-js/pull/131)
67

78
## 2.4.0
89
### New Features

images/screenshots/visitor_athen.png

1.94 MB
Loading

images/screenshots/visitor_osaka.png

2.08 MB
Loading

readme.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,39 @@ Whenever a label is not supported in the defined language, it falls back to `Lan
468468
Here is a sample of some compatible languages:
469469
![](images/screenshots/multilang.gif)
470470

471-
# Built-in support for right-to-left languages
471+
## Built-in support for right-to-left languages
472472
Languages that are written right-to-left such as Arabic and Hebrew are fully supported by default. No need to install any plugins!
473473

474474
<p align="center">
475475
<img src="images/screenshots/lang-arabic.jpeg" width="48%"></img>
476476
<img src="images/screenshots/lang-hebrew.jpeg" width="48%"></img>
477477
</p>
478478

479+
## Visitor language modes
480+
The *visitor* language modes are special built-in modes made to display labels in two different languages, concatenated when available:
481+
- `Language.VISITOR` concatenates labels in the language of your system and the *local* language
482+
- `Language.VISITOR_ENGLISH` concatenates labels in English and the *local* language
483+
484+
```ts
485+
const map = new Map({
486+
// some options...
487+
language: Language.VISITOR,
488+
})
489+
490+
// or
491+
492+
const map = new Map({
493+
// some options...
494+
language: Language.VISITOR_ENGLISH,
495+
})
496+
```
497+
498+
We believe these two modes can be very handy to help the end users identify places, especially when the local labels are not using a latin charset. Here is how it looks like:
499+
500+
![](images/screenshots/visitor_athen.png)
501+
![](images/screenshots/visitor_osaka.png)
502+
503+
479504
# Custom Events and Map Lifecycle
480505
## Events
481506
### The `ready` event

src/Map.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ export type MapOptions = Omit<MapOptionsML, "style" | "maplibreLogo"> & {
7575
style?: ReferenceMapStyle | MapStyleVariant | StyleSpecification | string;
7676

7777
/**
78-
* Define the language of the map. This can be done directly with a language ISO code (eg. "en")
78+
* Define the language of the map. This can be done directly with a language ISO code (eg. "en"),
79+
* the ISO code prepended with the OSM flag (eg. "name:en" or even just "name"),
7980
* or with a built-in shorthand (eg. Language.ENGLISH).
8081
* Note that this is equivalent to setting the `config.primaryLanguage` and will overwrite it.
8182
*/
82-
language?: LanguageInfo;
83+
language?: LanguageInfo | string;
8384

8485
/**
8586
* Define the MapTiler Cloud API key to be used. This is strictly equivalent to setting
@@ -300,7 +301,13 @@ export class Map extends maplibregl.Map {
300301
registerLocalCacheProtocol();
301302
}
302303

303-
this.primaryLanguage = options.language ?? config.primaryLanguage;
304+
if (typeof options.language === "undefined") {
305+
this.primaryLanguage = config.primaryLanguage;
306+
} else {
307+
const providedlanguage = toLanguageInfo(options.language, Language);
308+
this.primaryLanguage = providedlanguage ?? config.primaryLanguage;
309+
}
310+
304311
this.forceLanguageUpdate =
305312
this.primaryLanguage === Language.STYLE || this.primaryLanguage === Language.STYLE_LOCK ? false : true;
306313
this.languageAlwaysBeenStyle = this.primaryLanguage === Language.STYLE;

0 commit comments

Comments
 (0)