처리 로직은 모든 요청에서 수신하는 언어 문자열을 사용하여 사용자에게 응답할 수 있습니다. 이 가이드에서는 Google Cloud Platform을 사용해 Firebase용 Cloud 함수 내에서 현지화 라이브러리가 반환 응답을 현지화하는 데 도움이 됩니다
현지화 라이브러리
다음과 같은 유용한 라이브러리를 제공합니다. 특정 언어에 대한 맞춤형 응답 생성:
- 범용: I18n-node(예시 코드 스니펫에서 이 라이브러리를 사용함)
- 범용: format.js
- 시간대/시간 현지화: moment.js (Google 예시 코드 스니펫에서 이 라이브러리를 사용)
- 금액/통화: numeral.js
현지화된 대답 만들기
이 섹션에서는 현지화된 문자열과 Cloud에서 이러한 리소스 파일을 사용하는 방법을 알아보세요. Firebase fulfillment의 함수입니다.
현지화된 대답을 만들려면 다음 단계를 따르세요.
package.json
및index.js
파일과 동일한 디렉터리에 현지화된 문자열 파일의locales
디렉터리를 만듭니다. 이 내용은 디렉터리를<project-dir>/functions/locales
로 지정합니다.영어를 사용하는 모든 언어로 현지화된 문자열이 포함된 선택할 수 있습니다 예를 들어
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" }
package.json
파일에서 i18n-node 및 시점 라이브러리를 다음과 같이 선언합니다. dependencies:{ ... "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" } }
index.js
파일에서 i18n-node 및 순간의 종속 항목을 선언합니다. 라이브러리:const i18n = require('i18n'); const moment = require('moment');
index.js
파일에서 지원되는 언어로 i18n-node를 구성합니다.i18n.configure({ locales: ['en-US', 'en-GB', 'de-DE'], directory: __dirname + '/locales', defaultLocale: 'en-US' });
클라이언트 라이브러리의
conv.user.locale
를 사용하여 라이브러리의 언어를 설정합니다. 속성app.middleware((conv) => { i18n.setLocale(conv.user.locale); moment.locale(conv.user.locale); });
현지화된 응답을 반환하려면 현지화된 문자열로
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);