Skip to content

Commit edcd5d0

Browse files
use request context for i18n
1 parent 02a2afa commit edcd5d0

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

connect.js

+60-60
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ var app = new alexa.app('connect');
1717
app.express({ expressApp: express_app });
1818

1919
const successSound = "<audio src='soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_neutral_response_02'/>";
20-
const connectDeviceCard = (res) => ({
20+
const connectDeviceCard = (context) => ({
2121
type: "Simple",
22-
title: res.__("Connecting to a device using Spotify Connect"),
23-
content: res.__("To add a device to Spotify Connect,"
22+
title: context.__("Connecting to a device using Spotify Connect"),
23+
content: context.__("To add a device to Spotify Connect,"
2424
+ " log in to your Spotify account on a supported device"
2525
+ " such as an Echo, phone, or computer"
2626
+ "\nhttps://support.spotify.com/uk/article/spotify-connect/")
@@ -32,11 +32,11 @@ app.pre = function (req, res, _type) {
3232
i18n.configure({
3333
directory: __dirname + '/locales',
3434
defaultLocale: 'en-GB',
35-
register: res,
35+
register: req.context,
3636
});
3737

3838
if (req.data.request.locale) {
39-
res.setLocale(req.data.request.locale);
39+
req.context.setLocale(req.data.request.locale);
4040
}
4141
// Error if the application ID of the request is not for this skill
4242
if (req.applicationId != applicationId &&
@@ -45,22 +45,22 @@ app.pre = function (req, res, _type) {
4545
}
4646
// Check that the user has an access token, if they have linked their account
4747
if (!(req.context.System.user.accessToken || req.getSession().details.user.accessToken)) {
48-
res.say(res.__("You have not linked your Spotify account, check your Alexa app to link the account"));
48+
res.say(req.context.__("You have not linked your Spotify account, check your Alexa app to link the account"));
4949
res.linkAccount();
5050
}
5151
};
5252

5353
// Run after every request
5454
app.post = function (req, res, _type, exception) {
5555
if (exception) {
56-
return res.clear().say(res.__("An error occured: ") + exception).send();
56+
return res.clear().say(req.context.__("An error occured: ") + exception).send();
5757
}
5858
};
5959

6060
// Function for when skill is invoked without intent
6161
app.launch(function (req, res) {
62-
res.say(res.__("I can control your Spotify Connect devices, to start, ask me to list your devices"))
63-
.reprompt(res.__("To start, ask me to list your devices"));
62+
res.say(req.context.__("I can control your Spotify Connect devices, to start, ask me to list your devices"))
63+
.reprompt(req.context.__("To start, ask me to list your devices"));
6464
// Keep session open
6565
res.shouldEndSession(false);
6666
});
@@ -71,9 +71,9 @@ app.intent("AMAZON.HelpIntent", {
7171
"slots": {},
7272
"utterances": []
7373
}, function (req, res) {
74-
res.say(res.__("You can ask me to list your connect devices and then control them. "))
75-
.say(res.__("For example, tell me to play on a device after listing devices"))
76-
.reprompt(res.__("What would you like to do?"));
74+
res.say(req.context.__("You can ask me to list your connect devices and then control them. "))
75+
.say(req.context.__("For example, tell me to play on a device after listing devices"))
76+
.reprompt(req.context.__("What would you like to do?"));
7777
// Keep session open
7878
res.shouldEndSession(false);
7979
});
@@ -112,10 +112,10 @@ app.intent('PlayIntent', {
112112
req.getSession().set("statusCode", r.statusCode);
113113
res.say(successSound);
114114
}).catch((err) => {
115-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
115+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
116116
if (err.statusCode === 404) {
117-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
118-
res.card(connectDeviceCard(res));
117+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
118+
res.card(connectDeviceCard(req.context));
119119
}
120120
});
121121
}
@@ -135,10 +135,10 @@ app.intent('PauseIntent', {
135135
req.getSession().set("statusCode", r.statusCode);
136136
res.say(successSound);
137137
}).catch((err) => {
138-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
138+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
139139
if (err.statusCode === 404) {
140-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
141-
res.card(connectDeviceCard(res));
140+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
141+
res.card(connectDeviceCard(req));
142142
}
143143
});
144144
}
@@ -160,9 +160,9 @@ app.intent('SkipNextIntent', {
160160
req.getSession().set("statusCode", r.statusCode);
161161
res.say(successSound);
162162
}).catch((err) => {
163-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
163+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
164164
if (err.statusCode === 404) {
165-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
165+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
166166
res.card(connectDeviceCard(res));
167167
}
168168
});
@@ -186,9 +186,9 @@ app.intent('SkipPreviousIntent', {
186186
req.getSession().set("statusCode", r.statusCode);
187187
res.say(successSound);
188188
}).catch((err) => {
189-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
189+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
190190
if (err.statusCode === 404) {
191-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
191+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
192192
res.card(connectDeviceCard(res));
193193
}
194194
});
@@ -207,9 +207,9 @@ const setVolume = (volumePercent, req, res) => {
207207
// Handle sending as JSON
208208
json: true
209209
}).catch((err) => {
210-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
210+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
211211
if (err.statusCode === 404) {
212-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
212+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
213213
res.card(connectDeviceCard(res));
214214
}
215215
});
@@ -228,7 +228,7 @@ const getAndValidateVolumePercentFromSlot = (req, res, isPercentIntent) => {
228228
}
229229
else {
230230
// If not valid volume
231-
res.say(res.__(isPercentIntent
231+
res.say(req.context.__(isPercentIntent
232232
? "You can only set the volume percent between 0 and 100"
233233
: "You can only set the volume between 0 and 10"));
234234
// Keep session open
@@ -238,22 +238,22 @@ const getAndValidateVolumePercentFromSlot = (req, res, isPercentIntent) => {
238238
}
239239
else {
240240
// Not a number
241-
res.say(res.__(isPercentIntent
241+
res.say(req.context.__(isPercentIntent
242242
? "Try setting a volume percent between 0 and 100"
243243
: "Try setting a volume between 0 and 10"))
244-
.reprompt(res.__("What would you like to do?"));
244+
.reprompt(req.context.__("What would you like to do?"));
245245
// Keep session open
246246
res.shouldEndSession(false);
247247
return null;
248248
}
249249
}
250250
else {
251251
// No slot value
252-
res.say(res.__("I couldn't work out the volume to use."))
253-
.say(res.__(isPercentIntent
252+
res.say(req.context.__("I couldn't work out the volume to use."))
253+
.say(req.context.__(isPercentIntent
254254
? "Try setting a volume percent between 0 and 100"
255255
: "Try setting a volume between 0 and 10"))
256-
.reprompt(res.__("What would you like to do?"));
256+
.reprompt(req.context.__("What would you like to do?"));
257257
// Keep session open
258258
res.shouldEndSession(false);
259259
return null;
@@ -320,15 +320,15 @@ app.intent('GetDevicesIntent', {
320320
// Check if user has devices
321321
if (devices.length > 0) {
322322
// Comma separated list of device names
323-
res.say(res.__("I found these connect devices: "))
324-
.say([deviceNames.slice(0, -1).join(', '), deviceNames.slice(-1)[0]].join(deviceNames.length < 2 ? '' : ',' + res.__(' and ')) + ". ")
325-
.say(res.__("What would you like to do with these devices?")).reprompt(res.__("What would you like to do?"));
323+
res.say(req.context.__("I found these connect devices: "))
324+
.say([deviceNames.slice(0, -1).join(', '), deviceNames.slice(-1)[0]].join(deviceNames.length < 2 ? '' : ',' + req.context.__(' and ')) + ". ")
325+
.say(req.context.__("What would you like to do with these devices?")).reprompt(req.context.__("What would you like to do?"));
326326
// Keep session open
327327
res.shouldEndSession(false);
328328
}
329329
else {
330330
// No devices found
331-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
331+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
332332
res.card(connectDeviceCard(res));
333333
}
334334
})
@@ -376,29 +376,29 @@ app.intent('DevicePlayIntent', {
376376
// Handle sending as JSON
377377
json: true
378378
}).then((_r) => {
379-
res.say(res.__("Playing on device {{deviceName}}", { deviceName: device.name }));
379+
res.say(req.context.__("Playing on device {{deviceName}}", { deviceName: device.name }));
380380
}).catch((err) => {
381-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
381+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
382382
if (err.statusCode === 404) {
383-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
383+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
384384
res.card(connectDeviceCard(res));
385385
}
386386
});
387387
}
388388
else {
389389
// If device not found
390-
res.say(res.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
391-
.say(res.__("Try asking me to list devices first"));
390+
res.say(req.context.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
391+
.say(req.context.__("Try asking me to list devices first"));
392392
// Keep session open
393393
res.shouldEndSession(false);
394394
}
395395
});
396396
}
397397
else {
398398
// No slot value
399-
res.say(res.__("I couldn't work out which device to play on."))
400-
.say(res.__("Try asking me to list devices first"))
401-
.reprompt(res.__("What would you like to do?"));
399+
res.say(req.context.__("I couldn't work out which device to play on."))
400+
.say(req.context.__("Try asking me to list devices first"))
401+
.reprompt(req.context.__("What would you like to do?"));
402402
// Keep session open
403403
res.shouldEndSession(false);
404404
}
@@ -429,8 +429,8 @@ app.intent(
429429
if (!req.slot("TRACKNAME")) {
430430
// No slot value
431431
res
432-
.say(res.__("I couldn't work out which song you want to queue."))
433-
.reprompt(res.__("What would you like to do?"));
432+
.say(req.context.__("I couldn't work out which song you want to queue."))
433+
.reprompt(req.context.__("What would you like to do?"));
434434
// Keep session open
435435
res.shouldEndSession(false);
436436
}
@@ -473,21 +473,21 @@ app.intent(
473473
})
474474
.then((response) => {
475475
res.say(
476-
res.__("Queued track {{trackName}}", {
476+
req.context.__("Queued track {{trackName}}", {
477477
trackName,
478478
})
479479
);
480480
})
481481
.catch((err) => {
482482
res
483-
.say(res.__("Sorry, I couldn't queue that song."))
484-
.reprompt(res.__("What would you like to do?"));
483+
.say(req.context.__("Sorry, I couldn't queue that song."))
484+
.reprompt(req.context.__("What would you like to do?"));
485485
});
486486
})
487487
.catch((err) => {
488488
res
489-
.say(res.__("Sorry, I couldn't queue that song."))
490-
.reprompt(res.__("What would you like to do?"));
489+
.say(req.context.__("Sorry, I couldn't queue that song."))
490+
.reprompt(req.context.__("What would you like to do?"));
491491
});
492492
}
493493
);
@@ -527,29 +527,29 @@ app.intent('DeviceTransferIntent', {
527527
// Handle sending as JSON
528528
json: true
529529
}).then((_r) => {
530-
res.say(res.__("Transferring to {{deviceName}}", {deviceName: device.name }));
530+
res.say(req.context.__("Transferring to {{deviceName}}", {deviceName: device.name }));
531531
}).catch((err) => {
532-
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
532+
if (err.statusCode === 403) res.say(req.context.__("Make sure your Spotify account is premium"));
533533
if (err.statusCode === 404) {
534-
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
534+
res.say(req.context.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
535535
res.card(connectDeviceCard(res));
536536
}
537537
});
538538
}
539539
else {
540540
// If device not found
541-
res.say(res.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
542-
.say(res.__("Try asking me to list devices first"));
541+
res.say(req.context.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
542+
.say(req.context.__("Try asking me to list devices first"));
543543
// Keep session open
544544
res.shouldEndSession(false);
545545
}
546546
});
547547
}
548548
else {
549549
// No slot value
550-
res.say(res.__("I couldn't work out which device to transfer to."))
551-
.say(res.__("Try asking me to list devices first"))
552-
.reprompt(res.__("What would you like to do?"));
550+
res.say(req.context.__("I couldn't work out which device to transfer to."))
551+
.say(req.context.__("Try asking me to list devices first"))
552+
.reprompt(req.context.__("What would you like to do?"));
553553
// Keep session open
554554
res.shouldEndSession(false);
555555
}
@@ -578,16 +578,16 @@ app.intent('GetTrackIntent', {
578578
})
579579
.then(function (body) {
580580
if (body.is_playing) {
581-
res.say(res.__("This is {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
581+
res.say(req.context.__("This is {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
582582
}
583583
else {
584584
if (body.item.name) {
585585
// If not playing but last track known
586-
res.say(res.__("That was {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
586+
res.say(req.context.__("That was {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
587587
}
588588
else {
589589
// If unknown
590-
res.say(res.__("Nothing is playing"));
590+
res.say(req.context.__("Nothing is playing"));
591591
}
592592
}
593593
})

0 commit comments

Comments
 (0)