Adding multi language support.

This commit is contained in:
Rodrigo Rodriguez 2018-09-13 18:21:22 -03:00
parent 18200e8ea1
commit d3487387d6
8 changed files with 54 additions and 10 deletions

View file

@ -32,12 +32,11 @@
"use strict" "use strict"
const WaitUntil = require("wait-until")
import { GBCoreService } from "../services/GBCoreService"
import { IGBDialog } from "botlib" import { IGBDialog } from "botlib"
import { GBConversationalService } from "../services/GBConversationalService"
import { GBMinInstance } from "botlib" import { GBMinInstance } from "botlib"
import { BotAdapter } from "botbuilder" import { BotAdapter } from "botbuilder"
const localize = require("localize")(__dirname)
const _T = localize.translate
export class WelcomeDialog extends IGBDialog { export class WelcomeDialog extends IGBDialog {
/** /**
@ -53,13 +52,14 @@ export class WelcomeDialog extends IGBDialog {
async (dc, args) => { async (dc, args) => {
const user = min.userState.get(dc.context) const user = min.userState.get(dc.context)
localize.setLocale(dc.context.activity.locale.split("-")[0])
if (!user.once) { if (!user.once) {
user.once = true user.once = true
var a = new Date() var a = new Date()
const date = a.getHours() const date = a.getHours()
var msg = var msg =
date < 12 ? "bom dia" : date < 18 ? "boa tarde" : "boa noite" date < 12 ? _T("good morning") : date < 18 ? localize.translate("good evening") : _T("good night")
let messages = [`Oi, ${msg}.`, `Oi!`, `Olá, ${msg}`, `Olá!`] let messages = [`Oi, ${msg}.`, `Oi!`, `Olá, ${msg}`, `Olá!`]
await dc.context.sendActivity(messages[0]) await dc.context.sendActivity(messages[0])

View file

@ -32,8 +32,6 @@
"use strict" "use strict"
import { GBConversationalService } from "./../services/GBConversationalService"
import { GBCoreService } from "../services/GBCoreService"
import { IGBDialog } from "botlib" import { IGBDialog } from "botlib"
import { GBMinInstance } from "botlib" import { GBMinInstance } from "botlib"
import { BotAdapter } from "botbuilder" import { BotAdapter } from "botbuilder"
@ -54,7 +52,7 @@ export class WhoAmIDialog extends IGBDialog {
await dc.context.sendActivity(`${min.instance.description}`) await dc.context.sendActivity(`${min.instance.description}`)
if (min.instance.whoAmIVideo) { if (min.instance.whoAmIVideo) {
await dc.context.sendActivity(`Vou te mostrar um vídeo. Por favor, aguarde...`) await dc.context.sendActivity("show_video")
await min.conversationalService.sendEvent(dc, "play", { await min.conversationalService.sendEvent(dc, "play", {
playerType: "video", playerType: "video",
data: min.instance.whoAmIVideo.trim() data: min.instance.whoAmIVideo.trim()

View file

@ -0,0 +1,14 @@
{
"I will show you a video, please wait...": {
"pt": "Vou te mostrar um vídeo. Por favor, aguarde..."
},
"good morning": {
"pt": "bom dia"
},
"good evening": {
"pt": "boa tarde"
},
"good night": {
"pt": "boa noite"
}
}

View file

@ -1,4 +1,3 @@
import { IGBPackage } from "botlib"
/*****************************************************************************\ /*****************************************************************************\
| ( )_ _ | | ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ | | _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
@ -46,6 +45,7 @@ import { IGBCoreService, IGBInstance } from "botlib"
import { GBConfigService } from "./GBConfigService" import { GBConfigService } from "./GBConfigService"
import { GBError } from "botlib" import { GBError } from "botlib"
import { GuaribasPackage } from "../models/GBModel" import { GuaribasPackage } from "../models/GBModel"
import { IGBPackage } from "botlib"
/** Deployer service for bots, themes, ai and more. */ /** Deployer service for bots, themes, ai and more. */
export class GBDeployer { export class GBDeployer {

View file

@ -97,6 +97,7 @@ export class GBMinService {
server: any, server: any,
appPackages: Array<IGBPackage> appPackages: Array<IGBPackage>
): Promise<GBMinInstance> { ): Promise<GBMinInstance> {
// Serves default UI on root address '/'. // Serves default UI on root address '/'.
let uiPackage = "default.gbui" let uiPackage = "default.gbui"
@ -110,6 +111,7 @@ export class GBMinService {
let instances = await this.core.loadInstances() let instances = await this.core.loadInstances()
Promise.all( Promise.all(
instances.map(async instance => { instances.map(async instance => {
// Gets the authorization key for each instance from Bot Service. // Gets the authorization key for each instance from Bot Service.
let webchatToken = await this.getWebchatToken(instance) let webchatToken = await this.getWebchatToken(instance)
@ -119,6 +121,7 @@ export class GBMinService {
server.get("/instances/:botId", (req, res) => { server.get("/instances/:botId", (req, res) => {
(async () => { (async () => {
// Returns the instance object to clients requesting bot info. // Returns the instance object to clients requesting bot info.
let botId = req.params.botId let botId = req.params.botId
@ -269,6 +272,7 @@ export class GBMinService {
return adapter.processActivity(req, res, async context => { return adapter.processActivity(req, res, async context => {
const state = conversationState.get(context) const state = conversationState.get(context)
const dc = min.dialogs.createContext(context, state) const dc = min.dialogs.createContext(context, state)
dc.context.activity.locale = "pt-BR"
const user = min.userState.get(dc.context) const user = min.userState.get(dc.context)
if (!user.loaded) { if (!user.loaded) {
@ -291,26 +295,44 @@ export class GBMinService {
context.activity.type === "conversationUpdate" && context.activity.type === "conversationUpdate" &&
context.activity.membersAdded.length > 0 context.activity.membersAdded.length > 0
) { ) {
let member = context.activity.membersAdded[0] let member = context.activity.membersAdded[0]
if (member.name === "GeneralBots") { if (member.name === "GeneralBots") {
logger.info(`Bot added to conversation, starting chat...`) logger.info(`Bot added to conversation, starting chat...`)
appPackages.forEach(e => { appPackages.forEach(e => {
e.onNewSession(min, dc) e.onNewSession(min, dc)
}) })
// Starts root dialog.
await dc.begin("/") await dc.begin("/")
} else { } else {
logger.info(`Member added to conversation: ${member.name}`) logger.info(`Member added to conversation: ${member.name}`)
} }
// Processes messages.
} else if (context.activity.type === "message") { } else if (context.activity.type === "message") {
// Check to see if anyone replied. If not then start echo dialog
// Checks for /admin request.
if (context.activity.text === "admin") { if (context.activity.text === "admin") {
await dc.begin("/admin") await dc.begin("/admin")
// Checks for /menu JSON signature.
} else if (context.activity.text.startsWith("{\"title\"")) { } else if (context.activity.text.startsWith("{\"title\"")) {
await dc.begin("/menu", {data:JSON.parse(context.activity.text)}) await dc.begin("/menu", {data:JSON.parse(context.activity.text)})
// Otherwise, continue to the active dialog in the stack.
} else { } else {
await dc.continue() await dc.continue()
} }
// Processes events.
} else if (context.activity.type === "event") { } else if (context.activity.type === "event") {
// Empties dialog stack before going to the target. // Empties dialog stack before going to the target.

View file

@ -0,0 +1,5 @@
export const messages =
{
show_video: "I will show you a video, please wait..."
}
;

View file

@ -0,0 +1,4 @@
export const messages =
{
show_video: "Vou te mostrar um vídeo. Por favor, aguarde..."
}

View file

@ -47,6 +47,7 @@
"express-promise-router": "^3.0.3", "express-promise-router": "^3.0.3",
"fs-extra": "^7.0.0", "fs-extra": "^7.0.0",
"fs-walk": "^0.0.2", "fs-walk": "^0.0.2",
"localize": "^0.4.7",
"marked": "^0.5.0", "marked": "^0.5.0",
"pragmatismo-io-framework": "^1.0.15", "pragmatismo-io-framework": "^1.0.15",
"reflect-metadata": "^0.1.12", "reflect-metadata": "^0.1.12",