Skip to content

Commit 6f8f236

Browse files
i18n on response
1 parent 3b7988a commit 6f8f236

File tree

1 file changed

+71
-70
lines changed

1 file changed

+71
-70
lines changed

connect.js

+71-70
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ 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 = (req) => ({
20+
const connectDeviceCard = (res) => ({
2121
type: "Simple",
22-
title: req.__("Connecting to a device using Spotify Connect"),
23-
content: req.__("To add a device to Spotify Connect,"
22+
title: res.__("Connecting to a device using Spotify Connect"),
23+
content: res.__("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/")
2727
});
2828
const applicationId = require('./package.json').alexa.applicationId;
2929

30+
i18n.configure({
31+
directory: __dirname + '/locales',
32+
defaultLocale: 'en-GB',
33+
register: req
34+
});
35+
3036
// Run every time the skill is accessed
3137
app.pre = function (req, res, _type) {
32-
i18n.configure({
33-
directory: __dirname + '/locales',
34-
defaultLocale: 'en-GB',
35-
register: req
36-
});
3738
if (req.data.request.locale) {
38-
i18n.setLocale(req, req.data.request.locale);
39+
i18n.setLocale(res, req.data.request.locale);
3940
}
4041
// Error if the application ID of the request is not for this skill
4142
if (req.applicationId != applicationId &&
@@ -44,22 +45,22 @@ app.pre = function (req, res, _type) {
4445
}
4546
// Check that the user has an access token, if they have linked their account
4647
if (!(req.context.System.user.accessToken || req.getSession().details.user.accessToken)) {
47-
res.say(req.__("You have not linked your Spotify account, check your Alexa app to link the account"));
48+
res.say(res.__("You have not linked your Spotify account, check your Alexa app to link the account"));
4849
res.linkAccount();
4950
}
5051
};
5152

5253
// Run after every request
5354
app.post = function (req, res, _type, exception) {
5455
if (exception) {
55-
return res.clear().say(req.__("An error occured: ") + exception).send();
56+
return res.clear().say(res.__("An error occured: ") + exception).send();
5657
}
5758
};
5859

5960
// Function for when skill is invoked without intent
6061
app.launch(function (req, res) {
61-
res.say(req.__("I can control your Spotify Connect devices, to start, ask me to list your devices"))
62-
.reprompt(req.__("To start, ask me to list your devices"));
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"));
6364
// Keep session open
6465
res.shouldEndSession(false);
6566
});
@@ -70,9 +71,9 @@ app.intent("AMAZON.HelpIntent", {
7071
"slots": {},
7172
"utterances": []
7273
}, function (req, res) {
73-
res.say(req.__("You can ask me to list your connect devices and then control them. "))
74-
.say(req.__("For example, tell me to play on a device after listing devices"))
75-
.reprompt(req.__("What would you like to do?"));
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?"));
7677
// Keep session open
7778
res.shouldEndSession(false);
7879
});
@@ -111,10 +112,10 @@ app.intent('PlayIntent', {
111112
req.getSession().set("statusCode", r.statusCode);
112113
res.say(successSound);
113114
}).catch((err) => {
114-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
115+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
115116
if (err.statusCode === 404) {
116-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
117-
res.card(connectDeviceCard(req));
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));
118119
}
119120
});
120121
}
@@ -134,10 +135,10 @@ app.intent('PauseIntent', {
134135
req.getSession().set("statusCode", r.statusCode);
135136
res.say(successSound);
136137
}).catch((err) => {
137-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
138+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
138139
if (err.statusCode === 404) {
139-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
140-
res.card(connectDeviceCard(req));
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));
141142
}
142143
});
143144
}
@@ -159,10 +160,10 @@ app.intent('SkipNextIntent', {
159160
req.getSession().set("statusCode", r.statusCode);
160161
res.say(successSound);
161162
}).catch((err) => {
162-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
163+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
163164
if (err.statusCode === 404) {
164-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
165-
res.card(connectDeviceCard(req));
165+
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
166+
res.card(connectDeviceCard(res));
166167
}
167168
});
168169
}
@@ -185,10 +186,10 @@ app.intent('SkipPreviousIntent', {
185186
req.getSession().set("statusCode", r.statusCode);
186187
res.say(successSound);
187188
}).catch((err) => {
188-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
189+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
189190
if (err.statusCode === 404) {
190-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
191-
res.card(connectDeviceCard(req));
191+
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
192+
res.card(connectDeviceCard(res));
192193
}
193194
});
194195
}
@@ -206,10 +207,10 @@ const setVolume = (volumePercent, req, res) => {
206207
// Handle sending as JSON
207208
json: true
208209
}).catch((err) => {
209-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
210+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
210211
if (err.statusCode === 404) {
211-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
212-
res.card(connectDeviceCard(req));
212+
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
213+
res.card(connectDeviceCard(res));
213214
}
214215
});
215216
};
@@ -227,7 +228,7 @@ const getAndValidateVolumePercentFromSlot = (req, res, isPercentIntent) => {
227228
}
228229
else {
229230
// If not valid volume
230-
res.say(req.__(isPercentIntent
231+
res.say(res.__(isPercentIntent
231232
? "You can only set the volume percent between 0 and 100"
232233
: "You can only set the volume between 0 and 10"));
233234
// Keep session open
@@ -237,22 +238,22 @@ const getAndValidateVolumePercentFromSlot = (req, res, isPercentIntent) => {
237238
}
238239
else {
239240
// Not a number
240-
res.say(req.__(isPercentIntent
241+
res.say(res.__(isPercentIntent
241242
? "Try setting a volume percent between 0 and 100"
242243
: "Try setting a volume between 0 and 10"))
243-
.reprompt(req.__("What would you like to do?"));
244+
.reprompt(res.__("What would you like to do?"));
244245
// Keep session open
245246
res.shouldEndSession(false);
246247
return null;
247248
}
248249
}
249250
else {
250251
// No slot value
251-
res.say(req.__("I couldn't work out the volume to use."))
252-
.say(req.__(isPercentIntent
252+
res.say(res.__("I couldn't work out the volume to use."))
253+
.say(res.__(isPercentIntent
253254
? "Try setting a volume percent between 0 and 100"
254255
: "Try setting a volume between 0 and 10"))
255-
.reprompt(req.__("What would you like to do?"));
256+
.reprompt(res.__("What would you like to do?"));
256257
// Keep session open
257258
res.shouldEndSession(false);
258259
return null;
@@ -319,16 +320,16 @@ app.intent('GetDevicesIntent', {
319320
// Check if user has devices
320321
if (devices.length > 0) {
321322
// Comma separated list of device names
322-
res.say(req.__("I found these connect devices: "))
323-
.say([deviceNames.slice(0, -1).join(', '), deviceNames.slice(-1)[0]].join(deviceNames.length < 2 ? '' : ',' + req.__(' and ')) + ". ")
324-
.say(req.__("What would you like to do with these devices?")).reprompt(req.__("What would you like to do?"));
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?"));
325326
// Keep session open
326327
res.shouldEndSession(false);
327328
}
328329
else {
329330
// No devices found
330-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
331-
res.card(connectDeviceCard(req));
331+
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
332+
res.card(connectDeviceCard(res));
332333
}
333334
})
334335
// Handle errors
@@ -375,29 +376,29 @@ app.intent('DevicePlayIntent', {
375376
// Handle sending as JSON
376377
json: true
377378
}).then((_r) => {
378-
res.say(req.__("Playing on device {{deviceName}}", { deviceName: device.name }));
379+
res.say(res.__("Playing on device {{deviceName}}", { deviceName: device.name }));
379380
}).catch((err) => {
380-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
381+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
381382
if (err.statusCode === 404) {
382-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
383-
res.card(connectDeviceCard(req));
383+
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
384+
res.card(connectDeviceCard(res));
384385
}
385386
});
386387
}
387388
else {
388389
// If device not found
389-
res.say(req.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
390-
.say(req.__("Try asking me to list devices first"));
390+
res.say(res.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
391+
.say(res.__("Try asking me to list devices first"));
391392
// Keep session open
392393
res.shouldEndSession(false);
393394
}
394395
});
395396
}
396397
else {
397398
// No slot value
398-
res.say(req.__("I couldn't work out which device to play on."))
399-
.say(req.__("Try asking me to list devices first"))
400-
.reprompt(req.__("What would you like to do?"));
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?"));
401402
// Keep session open
402403
res.shouldEndSession(false);
403404
}
@@ -428,8 +429,8 @@ app.intent(
428429
if (!req.slot("TRACKNAME")) {
429430
// No slot value
430431
res
431-
.say(req.__("I couldn't work out which song you want to queue."))
432-
.reprompt(req.__("What would you like to do?"));
432+
.say(res.__("I couldn't work out which song you want to queue."))
433+
.reprompt(res.__("What would you like to do?"));
433434
// Keep session open
434435
res.shouldEndSession(false);
435436
}
@@ -472,21 +473,21 @@ app.intent(
472473
})
473474
.then((response) => {
474475
res.say(
475-
req.__("Queued track {{trackName}}", {
476+
res.__("Queued track {{trackName}}", {
476477
trackName,
477478
})
478479
);
479480
})
480481
.catch((err) => {
481482
res
482-
.say(req.__("Sorry, I couldn't queue that song."))
483-
.reprompt(req.__("What would you like to do?"));
483+
.say(res.__("Sorry, I couldn't queue that song."))
484+
.reprompt(res.__("What would you like to do?"));
484485
});
485486
})
486487
.catch((err) => {
487488
res
488-
.say(req.__("Sorry, I couldn't queue that song."))
489-
.reprompt(req.__("What would you like to do?"));
489+
.say(res.__("Sorry, I couldn't queue that song."))
490+
.reprompt(res.__("What would you like to do?"));
490491
});
491492
}
492493
);
@@ -526,29 +527,29 @@ app.intent('DeviceTransferIntent', {
526527
// Handle sending as JSON
527528
json: true
528529
}).then((_r) => {
529-
res.say(req.__("Transferring to {{deviceName}}", {deviceName: device.name }));
530+
res.say(res.__("Transferring to {{deviceName}}", {deviceName: device.name }));
530531
}).catch((err) => {
531-
if (err.statusCode === 403) res.say(req.__("Make sure your Spotify account is premium"));
532+
if (err.statusCode === 403) res.say(res.__("Make sure your Spotify account is premium"));
532533
if (err.statusCode === 404) {
533-
res.say(req.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
534-
res.card(connectDeviceCard(req));
534+
res.say(res.__("I couldn't find any connect devices, check your Alexa app for information on connecting a device"));
535+
res.card(connectDeviceCard(res));
535536
}
536537
});
537538
}
538539
else {
539540
// If device not found
540-
res.say(req.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
541-
.say(req.__("Try asking me to list devices first"));
541+
res.say(res.__("I couldn't find a device named {{DEVICE}}.", { DEVICE }))
542+
.say(res.__("Try asking me to list devices first"));
542543
// Keep session open
543544
res.shouldEndSession(false);
544545
}
545546
});
546547
}
547548
else {
548549
// No slot value
549-
res.say(req.__("I couldn't work out which device to transfer to."))
550-
.say(req.__("Try asking me to list devices first"))
551-
.reprompt(req.__("What would you like to do?"));
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?"));
552553
// Keep session open
553554
res.shouldEndSession(false);
554555
}
@@ -577,16 +578,16 @@ app.intent('GetTrackIntent', {
577578
})
578579
.then(function (body) {
579580
if (body.is_playing) {
580-
res.say(req.__("This is {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
581+
res.say(res.__("This is {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
581582
}
582583
else {
583584
if (body.item.name) {
584585
// If not playing but last track known
585-
res.say(req.__("That was {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
586+
res.say(res.__("That was {{name}} by {{artist}}", { name: body.item.name, artist: body.item.artists[0].name }));
586587
}
587588
else {
588589
// If unknown
589-
res.say(req.__("Nothing is playing"));
590+
res.say(res.__("Nothing is playing"));
590591
}
591592
}
592593
})

0 commit comments

Comments
 (0)