Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add phrases for post instruction processing #141

Merged
merged 4 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file. For change log formatting, see http://keepachangelog.com/

# 0.6.0

- Adds `phrase` types to the English localiztion. [#141](https://github.com/Project-OSRM/osrm-text-instructions/pull/141)
- Adds `distance`, `name` and `namedistance` options to the continue and continue straight instructions. [#141](https://github.com/Project-OSRM/osrm-text-instructions/pull/141)
- 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)

# 0.5.4 2017-09-06

- Improves the wording of "continue" instructions. [#142](https://github.com/Project-OSRM/osrm-text-instructions/pull/142)
Expand Down
34 changes: 21 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,31 @@ module.exports = function(version, _options) {

// Replace tokens
// NOOP if they don't exist
instruction = instruction
.replace('{way_name}', wayName)
.replace('{destination}', (step.destinations || '').split(',')[0])
.replace('{exit}', (step.exits || '').split(';')[0])
.replace('{exit_number}', this.ordinalize(language, step.maneuver.exit || 1))
.replace('{rotary_name}', step.rotary_name)
.replace('{lane_instruction}', laneInstruction)
.replace('{modifier}', instructions[language][version].constants.modifier[modifier])
.replace('{direction}', this.directionFromDegree(language, step.maneuver.bearing_after))
.replace('{nth}', nthWaypoint)
.replace(/ {2}/g, ' '); // remove excess spaces
var replaceTokens = {
'way_name': wayName,
'destination': (step.destinations || '').split(',')[0],
'exit': (step.exits || '').split(';')[0],
'exit_number': this.ordinalize(language, step.maneuver.exit || 1),
'rotary_name': step.rotary_name,
'lane_instruction': laneInstruction,
'modifier': instructions[language][version].constants.modifier[modifier],
'direction': this.directionFromDegree(language, step.maneuver.bearing_after),
'nth': nthWaypoint
};

return this.tokenize(instruction, replaceTokens, language);
},
tokenize: function(instruction, tokens, language) {
var output = Object.keys(tokens).reduce(function(memo, token) {
return memo.replace('{' + token + '}', tokens[token]);
}, instruction)
.replace(/ {2}/g, ' '); // remove excess spaces

if (instructions[language].meta.capitalizeFirstLetter) {
instruction = this.capitalizeFirstLetter(instruction);
return this.capitalizeFirstLetter(output);
}

return instruction;
return output;
}
};
};
3 changes: 2 additions & 1 deletion languages/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Fähre nehmen Richtung {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Sie haben Ihr {nth} Ziel erreicht"
Expand Down Expand Up @@ -413,4 +414,4 @@
}
}
}
}
}
12 changes: 10 additions & 2 deletions languages/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"destination": "Take the ferry towards {destination}"
}
},
"phrase": {
"two linked by distance": "{instruction_one} then in {distance} {instruction_two}",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the spaces should be removed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any camel casing in here, so I've merged this like this.

two linked by distance => twolinkedbydistance

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcwhittemore retracting my comment, this should spaces.

"two linked": "{instruction_one} then {instruction_two}",
"one in distance": "In {distance}, {instruction_one}"
},
"arrive": {
"default": {
"default": "You have arrived at your {nth} destination"
Expand Down Expand Up @@ -81,12 +86,15 @@
"default": "Continue {modifier}",
"name": "Continue {modifier} to stay on {way_name}",
"destination": "Continue {modifier} towards {destination}",
"exit": "Continue {modifier} onto {way_name}"
"exit": "Continue {modifier} onto {way_name}",
"distance": "Continue for {distance}",
"namedistance": "Continue on {way_name} for {distance}"
},
"straight": {
"default": "Continue straight",
"name": "Continue straight to stay on {way_name}",
"destination": "Continue towards {destination}"
"destination": "Continue towards {destination}",
"distance": "Continue straight for {distance}"
},
"slight left": {
"default": "Continue slightly left",
Expand Down
3 changes: 2 additions & 1 deletion languages/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Coge el ferry a {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Has llegado a tu {nth} destino"
Expand Down Expand Up @@ -418,4 +419,4 @@
}
}
}
}
}
1 change: 1 addition & 0 deletions languages/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Prendre le ferry en direction de {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Vous êtes arrivés à votre {nth} destination"
Expand Down
1 change: 1 addition & 0 deletions languages/translations/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Naik ferry menuju {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Anda telah tiba di tujuan ke-{nth}"
Expand Down
1 change: 1 addition & 0 deletions languages/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Prendi il traghetto verso {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Sei arrivato alla tua {nth} destinazione"
Expand Down
1 change: 1 addition & 0 deletions languages/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Neem het veer naar {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Je bent gearriveerd op de {nth} bestemming."
Expand Down
1 change: 1 addition & 0 deletions languages/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Weź prom w kierunku {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Dojechano do miejsca docelowego {nth}"
Expand Down
3 changes: 2 additions & 1 deletion languages/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Pegue a balsa sentido {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Você chegou ao seu {nth} destino"
Expand Down Expand Up @@ -418,4 +419,4 @@
}
}
}
}
}
3 changes: 2 additions & 1 deletion languages/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Погрузитесь на паром в направлении {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Вы прибыли в {nth} пункт назначения"
Expand Down Expand Up @@ -418,4 +419,4 @@
}
}
}
}
}
3 changes: 2 additions & 1 deletion languages/translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Ta färjan mot {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Du är framme vid din {nth} destination"
Expand Down Expand Up @@ -418,4 +419,4 @@
}
}
}
}
}
3 changes: 2 additions & 1 deletion languages/translations/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Скористайтесь поромом у напрямку {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Ви прибули у ваш {nth} пункт призначення"
Expand Down Expand Up @@ -418,4 +419,4 @@
}
}
}
}
}
3 changes: 2 additions & 1 deletion languages/translations/vi.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "Lên phà đi {destination}"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "Đến nơi {nth}"
Expand Down Expand Up @@ -418,4 +419,4 @@
}
}
}
}
}
1 change: 1 addition & 0 deletions languages/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"destination": "乘坐开往{destination}的轮渡"
}
},
"phrase": {},
"arrive": {
"default": {
"default": "您已经到达您的{nth}个目的地"
Expand Down
32 changes: 32 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@ var tape = require('tape');

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

tape.test('v5 tokenize', function(assert) {
var v5Instructions = instructions('v5');

var tokenString = 'Can {first} {second}';

var hasBoth = v5Instructions.tokenize(tokenString, {
first: 'osrm',
second: 'do routing'
}, 'en');
assert.equal(hasBoth, 'Can osrm do routing', 'does find and replace');

var hasFirst = v5Instructions.tokenize(tokenString, {
first: 'osrm',
second: ''
}, 'en');
assert.equal(hasFirst, 'Can osrm ', 'does find and replace and does not drop trailing spaces');

var hasSecond = v5Instructions.tokenize(tokenString, {
second: 'swim',
first: ''
}, 'en');
assert.equal(hasSecond, 'Can swim', 'does find and replace and drops internal extra spaces');

var missingSecond = v5Instructions.tokenize(tokenString, {
first: 'osrm'
}, 'en');
assert.equal(missingSecond, 'Can osrm {second}', 'does not replace tokens which are not provided');


assert.end();
});

tape.test('v5 directionFromDegree', function(assert) {
var v5Instructions = instructions('v5');

Expand Down