0.15.0 • Published 2 months ago

osrm-text-instructions v0.15.0

Weekly downloads
4,667
License
BSD-2-Clause
Repository
github
Last release
2 months ago

OSRM Text Instructions

OSRM Text Instructions is a Node.js library that transforms route data generated by OSRM into localized turn instructions to be displayed visually or read aloud by a text-to-speech engine. OSRM Text Instructions is the basis of guidance instructions in osrm-frontend, the Mapbox Directions API, and the Mapbox Navigation SDK.

  • Global: Text instructions are available in over a dozen languages via Transifex. Abbreviations and advanced grammatical transformations are available in some languages.
  • Customizable: Flexible options allow you to format and tweak the results to your liking.
  • Cross-platform: A data-driven approach facilitates implementations in other programming languages. OSRM Text Instructions is also available in Swift and Objective-C (for iOS, macOS, tvOS, and watchOS) and in Java (for Android and Java SE).
  • Well-tested: A data-driven test suite ensures compatibility across languages and platforms.

Usage

NPM

var version = 'v5';
var osrmTextInstructions = require('osrm-text-instructions')(version);

response.legs.forEach(function(leg) {
  leg.steps.forEach(function(step) {
    instruction = osrmTextInstructions.compile('en', step, options)
  });
});

If you are unsure if the user's locale is supported by osrm-text-inustrctions, use @mapbox/locale-utils for finding the best fitting language.

Parameters require('osrm-text-instructions')(version)

parameterrequired?valuesdescription
versionrequiredv5Major OSRM version

Parameters compile(language, step, options)

parameterrequired?valuesdescription
languagerequireden de zh-Hans fr nl ru and moreCompiling instructions for the selected language code.
steprequiredOSRM route step objectThe RouteStep as it comes out of OSRM
optionsoptionalObjectSee below
Options
keytypedescription
legCountintegerNumber of legs in the route
legIndexintegerZero-based index of the leg containing the step; together with legCount, this option determines which waypoint the user has arrived at
formatTokenfunctionFunction that formats the given token value after grammaticalization and capitalization but before the value is inserted into the instruction string; useful for wrapping tokens in markup
waypointNamestringOptional custom name for the leg's destination, replaces "your {nth} destination"

formatToken takes two parameters:

  • token: A string that indicates the kind of token, such as way_name or direction
  • value: A grammatical string for this token, capitalized if the token appears at the beginning of the instruction

and returns a string.

Architecture

  • index.js contains the main transformation logic in JavaScript.
  • languages/ contains the localization files, including raw format strings, abbreviation files, and grammar rules.
  • languages.js loads the localizations and contains some localization-related helper functions.
  • test/ contains data-driven integration tests and test fixtures for all supported languages.

Contributing

We welcome feedback, code contributions, and translations! Please see CONTRIBUTING.md for details.

Transifex

0.15.0

2 months ago

0.14.1-beta.3

4 years ago

0.14.1-beta.2

4 years ago

0.14.1-beta.1

4 years ago

0.14.0

4 years ago

0.14.0-beta.4

4 years ago

0.14.0-beta.3

4 years ago

0.14.0-beta.2

4 years ago

0.14.0-beta.1

4 years ago

0.13.4

5 years ago

0.13.3

5 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.0

6 years ago

0.11.5

6 years ago

0.11.4

6 years ago

0.11.3

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.7

6 years ago

0.10.6

6 years ago

0.10.5

7 years ago

0.10.4

7 years ago

0.10.3

7 years ago

0.10.2

7 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago