ตรรกะการดำเนินการตามคำสั่งซื้อจะใช้สตริงภาษาที่ได้รับในทุกคำขอเพื่อ ตอบสนองผู้ใช้ คู่มือนี้จะแสดงวิธีใช้บริการของบุคคลที่สาม ไลบรารีการแปลภายใน Cloud Function สำหรับ Firebase เพื่อแสดงผล คำตอบที่แปลแล้ว
ไลบรารีการแปล
ต่อไปนี้คือคลังที่มีประโยชน์บางส่วนซึ่งคุณควรพิจารณาเพื่อช่วย สร้างคำตอบที่ปรับแต่งให้เหมาะกับภาษา:
- จุดประสงค์ทั่วไป: โหนด I18n (ของเรา ตัวอย่างข้อมูลโค้ดใช้ไลบรารีนี้)
- จุดประสงค์ทั่วไป: format.js
- เขตเวลา/การแปลเวลา: moment.js (ของเรา ตัวอย่างข้อมูลโค้ดใช้ไลบรารีนี้)
- เงิน/สกุลเงิน: numeral.js
สร้างคำตอบที่แปลแล้ว
ส่วนนี้จะแสดงวิธีสร้างไฟล์แหล่งข้อมูลสตริงที่แปลแล้วที่ มีสตริงที่แปลแล้วและวิธีใช้ไฟล์ทรัพยากรเหล่านี้ในระบบคลาวด์ ฟังก์ชันสำหรับ Fulfillment ของ Firebase
วิธีสร้างคำตอบที่มีการแปลภาษา
- สร้างในไดเรกทอรีเดียวกันกับไฟล์ 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 และโมเมนต์เป็น ทรัพยากร Dependency:- { ... "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ให้ประกาศทรัพยากร Dependency สำหรับโหนด i18n และโมเมนต์ ไลบรารี:- const i18n = require('i18n'); const moment = require('moment'); 
- ในไฟล์ - index.jsให้กำหนดค่าโหนด i18n ด้วยภาษาที่รองรับดังนี้- 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()พร้อมสตริงที่แปลแล้ว ส่งคืนโดย i18n ข้อมูลโค้ดนี้ยังมีฟังก์ชันที่ใช้ช่วงเวลา เพื่อแสดงวันที่ที่แปลแล้ว:- 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);
