GithubHelp home page GithubHelp logo

isabella232 / mapbox-gl-language Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mapbox/mapbox-gl-language

0.0 0.0 0.0 68 KB

Switch language of your Mapbox GL JS style

Home Page: https://mapbox.github.io/mapbox-gl-language/

License: BSD 3-Clause "New" or "Revised" License

HTML 9.03% JavaScript 90.97%

mapbox-gl-language's Introduction

Mapbox GL Language Build Status npm

Adds support for switching the language of your map style in Mapbox GL JS maps.

Requires mapbox-gl-js. For other platforms, such as Android and iOS, see this help document.

Multiple language supported with style transforms

Automatic style transformations for different languages

Switch language based on user agent

Switch language based on user agent

Usage

mapbox-gl-language is a Mapbox GL JS plugin that you can easily add on top of your map.

When using a CDN

<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-language/v0.10.1/mapbox-gl-language.js'></script>

When using modules

Check how to use Mapbox GL JS in a module bundler.

npm install --save mapbox-gl @mapbox/mapbox-gl-language
var mapboxgl = require('mapbox-gl')
var MapboxLanguage = require('@mapbox/mapbox-gl-language');

Example

mapboxgl.accessToken = 'YOUR_ACCESS_TOKEN';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v10',
    center: [-77.0259, 38.9010],
    zoom: 9
});

// Add RTL support if you want to support Arabic
// mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.10.1/mapbox-gl-rtl-text.js');

var language = new MapboxLanguage();
map.addControl(language);

Check examples/ for more usage examples.

API

Table of Contents

MapboxLanguage

Create a new Mapbox GL JS plugin that modifies the layers of the map style to use the text-field that matches the browser language.

As of Mapbox GL Language v1.0.0, this plugin no longer supports token values (e.g. {name}). v1.0+ expects the text-field property of a style to use an expression of the form ['get', 'name_en'] or ['get', 'name']; these expressions can be nested. Note that get expressions used as inputs to other expressions may not be handled by this plugin. For example:

["match", 
  ["get", "name"], 
  "California", 
  "Golden State", 
  ["coalesce", 
    ["get", "name_en"], 
    ["get", "name"]
  ]
]

Only styles based on Mapbox v8 styles are supported.

Parameters

  • options object Options to configure the plugin.
    • options.supportedLanguages Array<string>? List of supported languages
    • options.languageTransform Function? Custom style transformation to apply
    • options.languageField RegExp RegExp to match if a text-field is a language field (optional, default /^name_/). Note that as of Mapbox GL Language v1.0.0, token values (e.g. {name}) are no longer supported.
    • options.getLanguageField Function? Given a language choose the field in the vector tiles
    • options.languageSource string? Name of the source that contains the different languages.
    • options.defaultLanguage string? Name of the default language to initialize style after loading.
    • options.excludedLayerIds Array<string>? Name of the layers that should be excluded from translation.

setLanguage

Explicitly change the language for a style.

Parameters
  • style object Mapbox GL style to modify
  • language string The language iso code

Returns object the modified style

Develop

Run the linter and watch for changes to rebuild with browserify.

npm install
npm run test

Languages

Showcasing the languages supported by Mapbox Streets.

Supported Styles

You can configure the plugin to support your own custom style using style transforms and custom language fields. By default, this plugin works best with official Mapbox styles or styles derived from official Mapbox styles. The styles must be based on Mapbox v8 styles:

  • mapbox://styles/mapbox/streets-v11
  • mapbox://styles/mapbox/outdoors-v11
  • mapbox://styles/mapbox/dark-v10
  • mapbox://styles/mapbox/light-v10
  • mapbox://styles/mapbox/satellite-streets-v9
  • mapbox://styles/mapbox/traffic-day-v2
  • mapbox://styles/mapbox/traffic-night-v2

mapbox-gl-language's People

Contributors

lukasmartinelli avatar ryanhamley avatar 1ec5 avatar sergei-zelinsky avatar manoharuss avatar wanghongrui avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.