Fulfillment

Your fulfillment logic can use the locale string it receives in every request to cater responses to users. This guide shows you how to use some third-party localization libraries within a Cloud Function for Firebase to return localized responses.

Localization libraries

Here are some helpful libraries to consider to help you generate customized responses for specific locales:

  • General purpose: I18n-node (our example code snippets use this library)
  • General purpose: format.js
  • Timezone/time localization: moment.js (our example code snippets use this library)
  • Money/currency: numeral.js

Create localized responses

This section shows you how to create localized string resource files that contain localized strings and how to use these resource files in your Cloud Function for Firebase fulfillment.

To create localized responses:

  1. In the same directory as your package.json and index.js files, create a locales directory for your localized string files. We'll refer to this directory as <project-dir>/functions/locales.
  2. Create a resource file that contains localized strings for every locale that you want to support. For example, if you want to support en-US, en-GB, and de-DE locales with localized welcome and date messages, those files might look like this:

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

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

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

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

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

    {
       "WELCOME_BASIC": "Hallo und wilkommen!",
       "DATE": "Das Datum ist %s"
    }
    
  3. In your package.json file, declare the i18n-node and moment libraries as dependencies:

    {
     ...
     "dependencies": {
       "actions-on-google": "^1.0.0",
       "firebase-admin": "^4.2.1",
       "firebase-functions": "^0.5.7",
       "i18n": "^0.8.3",
       "moment": "^2.18.1"
     }
    }
    
  4. In your index.js file, declare the dependencies for the i18n-node and moment libraries:

    const i18n = require('i18n');
    const moment = require('moment');
    
  5. In your index.js file, configure the i18n-node with your supported locales:

    i18n.configure({
      locales: ['en-US', 'en-GB', 'de-DE'],
      directory: __dirname + '/locales',
      defaultLocale: 'en-US'
    });
    
  6. Set the locale for the libraries using the getUserLocale() client library function.

    i18n.setLocale(app.getUserLocale());
    moment.locale(app.getUserLocale());
    
  7. To return a localized response, call ask() with a localized string returned by i18n. This snippet also contains a function that uses moment to return a localized date:

    function welcome (app) {
      app.ask(i18n.__('WELCOME_BASIC'));
    }
    
    function date (app) {
      app.ask(i18n.__('DATE', moment().format('LL')));
    }
    

Here's a complete index.js file as an example:

'use strict';

process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').DialogflowApp;
const i18n = require('i18n');
const moment = require('moment');
const functions = require('firebase-functions');

exports.myAssistantApp = functions.https.onRequest((request, response) => {
 const app = new App({request, response});
 console.log('Request headers: ' + JSON.stringify(request.headers));
 console.log('Request body: ' + JSON.stringify(request.body));

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

 function welcome (app) {
   app.ask(i18n.__('WELCOME_BASIC'));
 }

 function date (app) {
   app.ask(i18n.__('DATE', moment().format('LL')));
 }

 let actionMap = new Map();
 actionMap.set('input.welcome', welcome);
 actionMap.set('date', date);

 app.handleRequest(actionMap);
});