Skip to content

Commit b85d561

Browse files
authored
Merge pull request #141 from Project-OSRM/phrases
Add phrases for post instruction processing
2 parents fa5014d + 805bfb2 commit b85d561

File tree

17 files changed

+89
-22
lines changed

17 files changed

+89
-22
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
All notable changes to this project will be documented in this file. For change log formatting, see http://keepachangelog.com/
33

4+
# 0.6.0
5+
6+
- Adds `phrase` types to the English localiztion. [#141](https://github.com/Project-OSRM/osrm-text-instructions/pull/141)
7+
- Adds `distance`, `name` and `namedistance` options to the continue and continue straight instructions. [#141](https://github.com/Project-OSRM/osrm-text-instructions/pull/141)
8+
- Adds `tokenize` to the top level api so that external users can fill in osrm-text-instructions template strings. [#141](https://github.com/Project-OSRM/osrm-text-instructions/pull/141)
9+
410
# 0.5.4 2017-09-06
511

612
- Improves the wording of "continue" instructions. [#142](https://github.com/Project-OSRM/osrm-text-instructions/pull/142)

index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,31 @@ module.exports = function(version, _options) {
180180

181181
// Replace tokens
182182
// NOOP if they don't exist
183-
instruction = instruction
184-
.replace('{way_name}', wayName)
185-
.replace('{destination}', (step.destinations || '').split(',')[0])
186-
.replace('{exit}', (step.exits || '').split(';')[0])
187-
.replace('{exit_number}', this.ordinalize(language, step.maneuver.exit || 1))
188-
.replace('{rotary_name}', step.rotary_name)
189-
.replace('{lane_instruction}', laneInstruction)
190-
.replace('{modifier}', instructions[language][version].constants.modifier[modifier])
191-
.replace('{direction}', this.directionFromDegree(language, step.maneuver.bearing_after))
192-
.replace('{nth}', nthWaypoint)
193-
.replace(/ {2}/g, ' '); // remove excess spaces
183+
var replaceTokens = {
184+
'way_name': wayName,
185+
'destination': (step.destinations || '').split(',')[0],
186+
'exit': (step.exits || '').split(';')[0],
187+
'exit_number': this.ordinalize(language, step.maneuver.exit || 1),
188+
'rotary_name': step.rotary_name,
189+
'lane_instruction': laneInstruction,
190+
'modifier': instructions[language][version].constants.modifier[modifier],
191+
'direction': this.directionFromDegree(language, step.maneuver.bearing_after),
192+
'nth': nthWaypoint
193+
};
194+
195+
return this.tokenize(instruction, replaceTokens, language);
196+
},
197+
tokenize: function(instruction, tokens, language) {
198+
var output = Object.keys(tokens).reduce(function(memo, token) {
199+
return memo.replace('{' + token + '}', tokens[token]);
200+
}, instruction)
201+
.replace(/ {2}/g, ' '); // remove excess spaces
194202

195203
if (instructions[language].meta.capitalizeFirstLetter) {
196-
instruction = this.capitalizeFirstLetter(instruction);
204+
return this.capitalizeFirstLetter(output);
197205
}
198206

199-
return instruction;
207+
return output;
200208
}
201209
};
202210
};

languages/translations/de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Fähre nehmen Richtung {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Sie haben Ihr {nth} Ziel erreicht"
@@ -413,4 +414,4 @@
413414
}
414415
}
415416
}
416-
}
417+
}

languages/translations/en.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"destination": "Take the ferry towards {destination}"
5151
}
5252
},
53+
"phrase": {
54+
"two linked by distance": "{instruction_one} then in {distance} {instruction_two}",
55+
"two linked": "{instruction_one} then {instruction_two}",
56+
"one in distance": "In {distance}, {instruction_one}"
57+
},
5358
"arrive": {
5459
"default": {
5560
"default": "You have arrived at your {nth} destination"
@@ -81,12 +86,15 @@
8186
"default": "Continue {modifier}",
8287
"name": "Continue {modifier} to stay on {way_name}",
8388
"destination": "Continue {modifier} towards {destination}",
84-
"exit": "Continue {modifier} onto {way_name}"
89+
"exit": "Continue {modifier} onto {way_name}",
90+
"distance": "Continue for {distance}",
91+
"namedistance": "Continue on {way_name} for {distance}"
8592
},
8693
"straight": {
8794
"default": "Continue straight",
8895
"name": "Continue straight to stay on {way_name}",
89-
"destination": "Continue towards {destination}"
96+
"destination": "Continue towards {destination}",
97+
"distance": "Continue straight for {distance}"
9098
},
9199
"slight left": {
92100
"default": "Continue slightly left",

languages/translations/es.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Coge el ferry a {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Has llegado a tu {nth} destino"
@@ -418,4 +419,4 @@
418419
}
419420
}
420421
}
421-
}
422+
}

languages/translations/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Prendre le ferry en direction de {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Vous êtes arrivés à votre {nth} destination"

languages/translations/id.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Naik ferry menuju {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Anda telah tiba di tujuan ke-{nth}"

languages/translations/it.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Prendi il traghetto verso {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Sei arrivato alla tua {nth} destinazione"

languages/translations/nl.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Neem het veer naar {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Je bent gearriveerd op de {nth} bestemming."

languages/translations/pl.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Weź prom w kierunku {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Dojechano do miejsca docelowego {nth}"

languages/translations/pt-BR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Pegue a balsa sentido {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Você chegou ao seu {nth} destino"
@@ -418,4 +419,4 @@
418419
}
419420
}
420421
}
421-
}
422+
}

languages/translations/ru.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Погрузитесь на паром в направлении {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Вы прибыли в {nth} пункт назначения"
@@ -418,4 +419,4 @@
418419
}
419420
}
420421
}
421-
}
422+
}

languages/translations/sv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Ta färjan mot {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Du är framme vid din {nth} destination"
@@ -418,4 +419,4 @@
418419
}
419420
}
420421
}
421-
}
422+
}

languages/translations/uk.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Скористайтесь поромом у напрямку {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Ви прибули у ваш {nth} пункт призначення"
@@ -418,4 +419,4 @@
418419
}
419420
}
420421
}
421-
}
422+
}

languages/translations/vi.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "Lên phà đi {destination}"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "Đến nơi {nth}"
@@ -418,4 +419,4 @@
418419
}
419420
}
420421
}
421-
}
422+
}

languages/translations/zh-Hans.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"destination": "乘坐开往{destination}的轮渡"
5151
}
5252
},
53+
"phrase": {},
5354
"arrive": {
5455
"default": {
5556
"default": "您已经到达您的{nth}个目的地"

test/index_test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@ var tape = require('tape');
44

55
var instructions = require('../index');
66

7+
tape.test('v5 tokenize', function(assert) {
8+
var v5Instructions = instructions('v5');
9+
10+
var tokenString = 'Can {first} {second}';
11+
12+
var hasBoth = v5Instructions.tokenize(tokenString, {
13+
first: 'osrm',
14+
second: 'do routing'
15+
}, 'en');
16+
assert.equal(hasBoth, 'Can osrm do routing', 'does find and replace');
17+
18+
var hasFirst = v5Instructions.tokenize(tokenString, {
19+
first: 'osrm',
20+
second: ''
21+
}, 'en');
22+
assert.equal(hasFirst, 'Can osrm ', 'does find and replace and does not drop trailing spaces');
23+
24+
var hasSecond = v5Instructions.tokenize(tokenString, {
25+
second: 'swim',
26+
first: ''
27+
}, 'en');
28+
assert.equal(hasSecond, 'Can swim', 'does find and replace and drops internal extra spaces');
29+
30+
var missingSecond = v5Instructions.tokenize(tokenString, {
31+
first: 'osrm'
32+
}, 'en');
33+
assert.equal(missingSecond, 'Can osrm {second}', 'does not replace tokens which are not provided');
34+
35+
36+
assert.end();
37+
});
38+
739
tape.test('v5 directionFromDegree', function(assert) {
840
var v5Instructions = instructions('v5');
941

0 commit comments

Comments
 (0)