Skip to content

Commit fe4c725

Browse files
committed
Updated localization demo.
1 parent 3919691 commit fe4c725

File tree

6 files changed

+105
-76
lines changed

6 files changed

+105
-76
lines changed
Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/*-----------------------------------------------------------------------------
2-
This Bot demonstrates how the default localizer can be used to localize prompts.
2+
This Bot demonstrates basic localization support for a bot. It shows how to:
3+
4+
* Configure the bots default language and localization file path.
5+
* Prompt the user for their preferred language.
6+
* Localize all of a bots prompts across multiple languages.
37
48
# RUN THE BOT:
59
@@ -9,67 +13,82 @@ This Bot demonstrates how the default localizer can be used to localize prompts.
913
-----------------------------------------------------------------------------*/
1014

1115
var builder = require('../../core/');
12-
var fs = require("fs");
1316

1417
var connector = new builder.ConsoleConnector().listen();
15-
var bot = new builder.UniversalBot(connector, {localizerSettings: { botLocalePath: "./locale", defaultLocale: "en" }});
16-
17-
// Locale detection middleware
18-
bot.use({
19-
botbuilder: function (session, next) {
20-
// Use proper method to detect locale -- typing es triggers the es locale to be set
21-
if (/^es/i.test(session.message.text)) {
22-
// Set the locale for the session once its detected
23-
session.preferredLocale("es", (err) => {
24-
next();
25-
});
26-
27-
// Use proper method to detect locale -- typing us triggers the en-us locale to be set
28-
} else if (/^us/i.test(session.message.text)) {
29-
// Set the locale for the session once its detected
30-
session.preferredLocale("en-us", (err) => {
31-
next();
32-
});
33-
} else {
34-
// By not setting the preferred locale, we will fallback to the default (en in this case)
35-
next();
36-
}
18+
var bot = new builder.UniversalBot(connector, {
19+
localizerSettings: {
20+
botLocalePath: "./locale",
21+
defaultLocale: "en"
3722
}
3823
});
3924

40-
41-
bot.dialog('/', [
42-
function (session, args, next) {
43-
// This key is present in all the locale/*/index.json files, so whichever locale is set on the session should
44-
// dictate what's output to the user
45-
session.send("Hello World");
46-
47-
// This key is only in the locale/en/index.json file, so en-us will fallback to this because its a child of en.
48-
// es will also fallback to this, but only because our default locale is en.
49-
session.send("Hello World2");
50-
51-
// This key is not present in any of the locale/*/index.json files, so we will just end up showing 'Hello World3'
52-
session.send("Hello World3");
53-
54-
// This key has multiple values associated with it, delimited by a semi-colon
55-
// To display a random value, get the entire localized string then split it on the semi-colon
56-
// and select a random index from the array
57-
var valuesStr = session.localizer.gettext(session.preferredLocale(), "nums");
58-
var valuesArray = valuesStr.split(";");
59-
session.send(valuesArray[Math.floor(Math.random() * valuesArray.length)]);
60-
61-
// This key has multiple values as well, but stored as an array, so a random element will be returned each time
25+
bot.dialog("/", [
26+
function (session) {
6227
session.send("greeting");
28+
session.send("instructions");
29+
session.beginDialog('/localePicker');
30+
},
31+
function (session) {
32+
builder.Prompts.text(session, "text_prompt");
33+
},
34+
function (session, results) {
35+
session.send("input_response", results.response);
36+
builder.Prompts.number(session, "number_prompt");
37+
},
38+
function (session, results) {
39+
session.send("input_response", results.response);
40+
builder.Prompts.choice(session, "listStyle_prompt", "auto|inline|list|button|none");
41+
},
42+
function (session, results) {
43+
// You can use the localizer manually to load a localized list of options.
44+
var style = builder.ListStyle[results.response.entity];
45+
var options = session.localizer.gettext(session.preferredLocale(), "choice_options");
46+
builder.Prompts.choice(session, "choice_prompt", options, { listStyle: style });
47+
},
48+
function (session, results) {
49+
session.send("choice_response", results.response.entity);
50+
builder.Prompts.confirm(session, "confirm_prompt");
51+
},
52+
function (session, results) {
53+
// You can use the localizer manually to load prompts from another namespace.
54+
var choice = results.response ? 'confirm_yes' : 'confirm_no';
55+
session.send("choice_response", session.localizer.gettext(session.preferredLocale(), choice, 'BotBuilder'));
56+
builder.Prompts.time(session, "time_prompt");
57+
},
58+
function (session, results) {
59+
session.send("time_response", JSON.stringify(results.response));
60+
session.endDialog("demo_finished");
61+
}
62+
])
6363

64-
// Supply the localized key to the prompt.
65-
// Note that our locale/en/botbuilder.json file overrides the system's default prompt when a number is not supplied
66-
builder.Prompts.choice(session, "listr", "y|n|idk")
64+
bot.dialog('/localePicker', [
65+
function (session) {
66+
// Prompt the user to select their preferred locale
67+
builder.Prompts.choice(session, "locale_prompt", 'English|Español|Italiano');
6768
},
6869
function (session, results) {
69-
if (results.response) {
70-
session.send("Thanks");
71-
} else {
72-
session.send("Sorry!");
70+
// Update preferred locale
71+
var locale;
72+
switch (results.response.entity) {
73+
case 'English':
74+
locale = 'en';
75+
break;
76+
case 'Español':
77+
locale = 'es';
78+
break;
79+
case 'Italiano':
80+
locale = 'it';
81+
break;
7382
}
83+
session.preferredLocale(locale, function (err) {
84+
if (!err) {
85+
// Locale files loaded
86+
session.endDialog('locale_updated');
87+
} else {
88+
// Problem loading the selected locale
89+
session.error(err);
90+
}
91+
});
7492
}
7593
]);
94+

Node/examples/basics-localization/locale/en-us/index.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

Node/examples/basics-localization/locale/en/BotBuilder.json

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
{
2-
"Hello World": "Hello World en",
3-
"Hello World2": "Hello World 2 en",
4-
"listr": "Do you like node?",
5-
"thanks": "Thanks",
6-
"y": "oh yeah!",
7-
"n": "no way!",
8-
"idk": "i dunno",
9-
"greeting": ["hey", "hello", "hi"],
10-
"nums": "one;two;three"
2+
"greeting": ["Welcome to the localization sample bot.", "Hey there. I'm the localization sample bot.", "Greetings... I'm the localization sample bot."],
3+
"instructions": "This demo will first ask you to select a preferred language and then walk through using all of the built-in prompts in that language.",
4+
"locale_prompt": "What's your preferred language?",
5+
"locale_updated": "Your preferred language is now english.",
6+
"text_prompt": "Prompts.text(): enter some text and it will be repeated back to you.",
7+
"number_prompt": "Prompts.number(): enter a number. Try something not a number to see the built-in re-prompt text.",
8+
"listStyle_prompt": "Prompts.choice() supports a range of ListStyles. Which would you like to use?",
9+
"choice_prompt": "Prompts.choice(): choose an option.",
10+
"choice_options": "Option A|Option B|Option C",
11+
"confirm_prompt": "Prompts.confirm(): enter 'yes' or 'no'.",
12+
"time_prompt": "Prompts.time(): the built-in time prompt only accepts english. Enter a time like 'monday at 7am' and I'll show you the JSON returned.",
13+
"input_response": "You entered '%s'",
14+
"choice_response": "You chose '%s'",
15+
"time_response": "JSON for time entered: %s",
16+
"demo_finished": ["That's the end of the demo.", "Thank you. That's the end."]
1117
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
{
2-
"Hello World": "hola mundo es",
3-
"listr": "te gusta el node?",
4-
"thanks": "gracias!",
5-
"y": "claro!",
6-
"n": "nunca",
7-
"idk": "no se",
8-
"greeting": ["hola", "buenos", "buen dia"],
9-
"nums": "uno;dos;tres"
2+
"locale_updated": "Your preferred language is now español."
103
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"greeting": ["Benvenuto. Sono il bot di esempio di localizzazione.", "Ciao. Sono il bot di esempio di localizzazione."],
3+
"instructions": "Questo esempio ti chiederà di selezionare la tua lingua preferita e poi ti mostrerà come usare i messaggi predefiniti in quella lingua. ",
4+
"locale_prompt": "Qual è la tua lingua preferita?",
5+
"locale_updated": "La tua lingua preferita è: italiano.",
6+
"text_prompt": "Prompts.text(): qualsiasi testo inserito verrà ripetuto.",
7+
"number_prompt": "Prompts.number(): inserisci un numero. Prova ad inserire un carattere non numerico per vedere il messaggio di errore predefinito.",
8+
"listStyle_prompt": "Prompts.choice() supporta vari ListStyles. Quale vorresti usare?",
9+
"choice_prompt": "Prompts.choice(): scegli un’opzione.",
10+
"choice_options": "Opzione A|Opzione B|Opzione C",
11+
"confirm_prompt": "Prompts.confirm(): rispondi 'sì' o 'no'.",
12+
"time_prompt": "Prompts.time(): il messaggio di richiesta della data/tempo funziona solo in inglese. Inserisci una data/tempo come 'monday at 7am' e ti mostrerò il corrispondente risultato JSON.",
13+
"input_response": "Hai inserito '%s'",
14+
"choice_response": "Hai scelto '%s'",
15+
"time_response": "Risultato JSON per la data/tempo inserita: %s",
16+
"demo_finished": ["Questa è la conclusione dell’esempio di localizzazione.", "Grazie. Si conclude quì l’esempio di localizzazione."]
17+
}

0 commit comments

Comments
 (0)