React-mapbox-gl: How to change language of map labels in react-mapbox-gl?

Created on 13 Dec 2019  路  5Comments  路  Source: alex3165/react-mapbox-gl

Mapbox docs give this clear example:
https://docs.mapbox.com/mapbox-gl-js/example/language-switch/

react-mapbox-gl docs describe how to work with the original mapbox API:
https://github.com/alex3165/react-mapbox-gl/blob/master/docs/API.md#using-the-original-mapbox-api

But I cannot change the language of my map labels by combining these two pieces of documentation. (I have tried both methods described for working with the original mapbox documentation.)

I've also tried Mapbox GL Language plugin: https://blog.mapbox.com/how-to-localize-your-maps-in-mapbox-gl-js-da4cc6749f47

Using Mapbox GL Language plugin as described (combined with the docs on how to interact with original mapbox API) wipes out map labels completely.

I'm clearly doing something (perhaps many things!) wrong.

Can someone point me in the right direction to learn how to localize a react-mapbox-gl map? Or even better: share a working code sample of how you implemented map localization with react-mapbox-gl?

Thank you!!

Most helpful comment

@dearamerican @melamber
Inspired by the snippet in this comment, here's how I changed the labels into Traditional Chinese.

import React from 'react';
import ReactMapboxGl from 'react-mapbox-gl';

const AwesomeMap = ReactMapboxGl({
    accessToken: process.env.REACT_APP_MAPBOX_TOKEN,
});

const changeMapLanguage = (map) => {
    map.getStyle().layers.forEach((layer) => {
        if (layer.id.endsWith('-label')) {
            map.setLayoutProperty(layer.id, 'text-field', [
                'coalesce',
                ['get', 'name_zh-Hant'],
                ['get', 'name'],
            ]);
        }
    });
};

const App = () => (
    <AwesomeMap
        style="mapbox://styles/mapbox/streets-v11"
        containerStyle={{ width: '100vw', height: '100vh' }}
        onStyleLoad={changeMapLanguage}
    >
        {/* Map Layers Here */}
    </AwesomeMap>
);

export default App;

All 5 comments

@dearamerican do You have any luck with it?
The same for me. I couldn't find approach to change language for react-mapbox-gl.
Does anybody has information regarding it?

@dearamerican @melamber
Inspired by the snippet in this comment, here's how I changed the labels into Traditional Chinese.

import React from 'react';
import ReactMapboxGl from 'react-mapbox-gl';

const AwesomeMap = ReactMapboxGl({
    accessToken: process.env.REACT_APP_MAPBOX_TOKEN,
});

const changeMapLanguage = (map) => {
    map.getStyle().layers.forEach((layer) => {
        if (layer.id.endsWith('-label')) {
            map.setLayoutProperty(layer.id, 'text-field', [
                'coalesce',
                ['get', 'name_zh-Hant'],
                ['get', 'name'],
            ]);
        }
    });
};

const App = () => (
    <AwesomeMap
        style="mapbox://styles/mapbox/streets-v11"
        containerStyle={{ width: '100vw', height: '100vh' }}
        onStyleLoad={changeMapLanguage}
    >
        {/* Map Layers Here */}
    </AwesomeMap>
);

export default App;

@mingjunlu Thanks - worked for me 馃槃

@nataliahering No problem! I'm glad it helped 馃槃

I am not sure why this is working for you, as only the labels are translated. You miss everything else, such as parks, cities, rivers (and all the others) which are not purely labels.

I found an alternative solution:

mapbox.current.getStyle().layers.forEach((layer) => {
  if (layer.layout && layer.layout['text-field']) {
    mapbox.current.setLayoutProperty(layer.id, 'text-field', [
      'coalesce',
      ['get', 'name_' + LOCALE],
      ['get', 'name'],
    ]);
  }
});

It would be nice if Mapbox comes with a proper solution, with a global option, for example. But for now, that will do :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cyrilchapon picture cyrilchapon  路  3Comments

edv4rd0 picture edv4rd0  路  3Comments

Ricardo-C-Oliveira picture Ricardo-C-Oliveira  路  4Comments

jesster2k10 picture jesster2k10  路  3Comments

benderlidze picture benderlidze  路  3Comments