처리 (Dialogflow)

처리 로직은 모든 요청에서 수신하는 언어 문자열을 사용하여 사용자에게 응답을 전달할 수 있습니다. 이 가이드에서는 Firebase용 Cloud 함수 내에서 일부 타사 현지화 라이브러리를 사용하여 현지화된 응답을 반환하는 방법을 설명합니다.

현지화 라이브러리

다음은 특정 언어에 맞게 맞춤설정된 응답을 생성하는 데 도움이 되는 유용한 라이브러리입니다.

  • 범용: I18n-node (코드 스니펫에서 이 라이브러리를 사용함)
  • 범용: format.js
  • 시간대/시간 현지화: moment.js (이 라이브러리를 사용하는 코드 스니펫 예)
  • 화폐/통화: numeral.js

현지화된 응답 만들기

이 섹션에서는 현지화된 문자열이 포함된 현지화된 문자열 리소스 파일을 만드는 방법과 Firebase용 Cloud 함수 처리에서 이러한 리소스 파일을 사용하는 방법을 설명합니다.

현지화된 응답을 만드는 방법은 다음과 같습니다.

  1. package.jsonindex.js 파일과 동일한 디렉터리에 현지화된 문자열 파일을 위한 locales 디렉터리를 만듭니다. 이 디렉터리를 <project-dir>/functions/locales라고 하겠습니다.
  2. 지원하려는 모든 언어의 현지화된 문자열이 포함된 리소스 파일을 만듭니다. 예를 들어 현지화된 시작 및 날짜 메시지로 en-US, en-GB, de-DE 언어를 지원하려는 경우 해당 파일은 다음과 같을 수 있습니다.

    <project-dir>/functions/locales/en-US.json

    {
       "WELCOME_BASIC": "Hello, welcome!",
       "DATE": "The date is %s"
    }
    

    <project-dir>/functions/locales/en-GB.json

    {
       "WELCOME_BASIC": "Hello, welcome!",
       "DATE": "The date is %s"
    }
    

    <project-dir>/functions/locales/de-DE.json

    {
       "WELCOME_BASIC": "Hallo und willkommen!",
       "DATE": "Das Datum ist %s"
    }
    
  3. package.json 파일에서 i18n-node 및 모멘트 라이브러리를 종속 항목으로 선언합니다.

    {
     ...
     "dependencies": {
       "actions-on-google": "^2.7.0",
       "firebase-admin": "^7.2.1",
       "firebase-functions": "^2.2.1",
       "i18n": "^0.8.3",
       "moment": "^2.22.1"
     }
    }
    
  4. index.js 파일에서 i18n-node 및 모멘트 라이브러리의 종속 항목을 선언합니다.

    const i18n = require('i18n');
    const moment = require('moment');

  5. index.js 파일에서 지원되는 언어로 i18n-node를 구성합니다.

    i18n.configure({
      locales: ['en-US', 'en-GB', 'de-DE'],
      directory: __dirname + '/locales',
      defaultLocale: 'en-US'
    });

  6. 클라이언트 라이브러리 속성에서 conv.user.locale를 사용하여 라이브러리의 언어를 설정합니다.

    app.middleware((conv) => {
      i18n.setLocale(conv.user.locale);
      moment.locale(conv.user.locale);
    });

  7. 현지화된 응답을 반환하려면 i18n에서 반환된 현지화된 문자열로 ask()를 호출합니다. 이 스니펫에는 순간을 사용하여 현지화된 날짜를 반환하는 함수도 포함되어 있습니다.

    app.intent('Default Welcome Intent', (conv) => { // must not be async for i18n
      conv.ask(i18n.__('WELCOME_BASIC'));
    });
    
    app.intent('date', (conv) => { // must not be async for i18n
      conv.ask(i18n.__('DATE', moment().format('LL')));
    });

다음은 전체 index.js 파일의 예입니다.

'use strict';
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const i18n = require('i18n');
const moment = require('moment');

i18n.configure({
  locales: ['en-US', 'en-GB', 'de-DE'],
  directory: __dirname + '/locales',
  defaultLocale: 'en-US'
});

const app = dialogflow({debug: true});

app.middleware((conv) => {
  i18n.setLocale(conv.user.locale);
  moment.locale(conv.user.locale);
});

app.intent('Default Welcome Intent', (conv) => { // must not be async for i18n
  conv.ask(i18n.__('WELCOME_BASIC'));
});

app.intent('date', (conv) => { // must not be async for i18n
  conv.ask(i18n.__('DATE', moment().format('LL')));
});

exports.demoAction = functions.https.onRequest(app);