Skip to content

Cannot set the map language #63

Open

Description

@ygoe

My website supports two languages: en, de. The currently selected language is set in the <html lang> attribute. The user may choose the language they want out of these two. The map should follow that as well.

Without this plugin, my maps are always in German, no matter what I do.

With this plugin, the map follows the browser's first language (if supported, see #4) but not the page content's language from the above attribute.

I saw that there is a setLanguage method but it requires a parameter style which is not documented. I have no idea what I should pass to it. So I cannot consider that method. And then there's nothing left. The plugin cannot set the map language to the page language.

Activity

  1. goldengecko commented on Aug 7, 2023

    @goldengecko

    I'm trying to work this out too. From the documentation it looks as if the setLanguage function takes in a style and a new language, and returns the style modified to be in the new language.

    I saw someone else's code which does the following:
    this.map.setStyle(this.mapboxlanguage.setLanguage(this.map.getStyle(), newLanguage.code)) so I tried that, but it doesn't work for me.

    I tried splitting it out to look at the input style and output style, and it did indeed change the values from name_en to name_ja in my test of switching from English to Japanese.

    I wish the documentation was clear on how this is supposed to work. I don't know what the examples are supposed to do, and I'm not sure why it needs the this.map.addControl(this.mapboxlanguage) when it doesn't seem to add a control.

  2. goldengecko commented on Aug 7, 2023

    @goldengecko

    It turns out that the only thing I was missing is to set the language of the map.

    My theory of how it works is that you alter the styles so that it knows that there is a language lookup for the new language, then you tell the map to use that language.

    For clarity, here is my code (where this.map is the map created with new mapboxgl.Map and this.mapboxlanguage is created with new MapboxLanguage and added to the map with this.map.addControl(this.mapboxlanguage) and newLanguage is an object which has an attribute code which is the code for one of the supported languages e.g. "jp":

          this.map.setStyle(this.mapboxlanguage.setLanguage(this.map.getStyle(), newLanguage.code))
          this.map.language = newLanguage.code

    Hope this helps out. Perhaps the documentation could explain this better.

  3. 1ec5 commented on Aug 8, 2024

    @1ec5
    Contributor

    I don’t see how setting map.language would help; this plugin never consults that property.

    I was only able to work around this issue by turning off style diffing (which has the downside of a jarring flash as the old style gets wiped away completely):

    map.setStyle(newStyle, { diff: false });
  4. albertzak commented on Oct 14, 2025

    @albertzak

    thank you, @goldengecko and @1ec5 - combining your solutions was the thing that worked for me:

    const language = "de"
    map.once("style.load", () => {
        const ml = new MapboxLanguage({ defaultLanguage: language })
        map.addControl(ml)
        map.setStyle(ml.setLanguage(map.getStyle(), language), { diff: false })
    })
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions