Amazon AlexaAWS

alexa-sdkでの多言語対応とalexa-conversationでのテスト方法

この記事は一人Alexa Skills Kit for Node.js Advent Calendar 2017の6日目の記事です。 alexa-sdkはi18nextでの多言語化に対応しています。 該当コード:http […]

広告ここから
広告ここまで

この記事は一人Alexa Skills Kit for Node.js Advent Calendar 2017の6日目の記事です。

alexa-sdkはi18nextでの多言語化に対応しています。

該当コード:https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/alexa.js#L5

Hello Worldを英独に対応させる

翻訳文章のセットアップ

以下の様な書き方で翻訳テキストをセットアップします。

const languageStrings = {
  'en-US': {
    'translation': {
      'SAY_HELLO_MESSAGE': 'Hello World!'
    }
  },
  'de-DE': {
    'translation': {
      'SAY_HELLO_MESSAGE': 'Hallo Welt!'
    }
  }
}
module.exports = languageStrings 

翻訳文章の読み込み

alexa.resourcesに先ほど作成したオブジェクトを読み込ませればOKです。

const Alexa = require('alexa-sdk')
const handlers = require('./handlres')
const languageStrings = require('./lang')

module.exports.handler = function (event, context, callback) {
  const alexa = Alexa.handler(event, context)
  alexa.resources = languageStrings
  alexa.registerHandlers(handlers)
  alexa.execute()
}

alexa-conversationでの多言語テスト

alexa-conversationでも多言語のテストが可能です。

conversationの引数にlocale = 'de-DEと入れることで、ドイツ語のレスポンスに切り替わっていることが確認できます。

const conversation = require('alexa-conversation')
const app = require('../../index.js')

const opts = {
  appId: 'your-app-id',
  app: app,
  handler: app.handler
}

opts.name = 'Say Hello world in English'
conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('Hello World!')
  .end()

opts.locale = 'de-DE'
conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('Hallo Welt!')
  .end()

で、日本語は?

日本語のkeyはja-JPです。
なので以下の様に値を追加してやることで、日独英に対応したHello Worldスキルを作ることができます。

const languageStrings = {
  'en-US': {
    'translation': {
      'SAY_HELLO_MESSAGE': 'Hello World!'
    }
  },
  'de-DE': {
    'translation': {
      'SAY_HELLO_MESSAGE': 'Hallo Welt!'
    }
  },
  'ja-JP': {
    'translation': {
      'SAY_HELLO_MESSAGE': 'こんにちは!'
    }
  }
}

テストも同様です。

const conversation = require('alexa-conversation')
const app = require('../../index.js')

const opts = {
  appId: 'your-app-id',
  app: app,
  handler: app.handler
}

opts.name = 'Say Hello world in English'
conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('Hello World!')
  .end()

opts.name = 'Say Hello world in German'
opts.locale = 'de-DE'
conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('Hallo Welt!')
  .end()

opts.name = 'Say Hello world in Japanese'
opts.locale = 'ja-JP'
conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('こんにちは!')
  .end()

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts