Localization


Overview


Actionhero uses the i18n module to localize responses to clients.


Locale Files


  • When running Actionhero with config.i18n.updateFiles = true, you will see Actionhero generate a 'locales' folder at the top level of your project which will contain translations of all strings in your project with are passed though the new localization system. This includes all uses of i18n.localize and connection.localize.
  • We use mustache-style localization
  • From here, it is an easy matter to change the strings, per locale, to how you would like them presented back in your application. The next time you restart the server, the values you've updated in your locale strings file will be used.
  • disable config.i18n.updateFiles if you do not want this behavior.

Connection Locale


Since every Actionhero implementation is unique, we do not ship with a "guess" about how to determine a given connection's locale. Perhaps you have an HTTP server and you can trust your client's accept-language headers. Or perhaps you run your API under a number of different host names and you can presume locale based on them. Whatever the case, you need to create a async method in an initializer which will be called when each connection connects to return its locale.

For example, I may have an initializer in my project like this:

const { Initializer } = require("actionhero");

export class DetermineConnectionLocale extends Initializer {
  constructor() {
    super();
    this.name = "determineConnectionLocale";
  }

  initialize() {
    api.customLocalization = {
      lookup: connection => {
        let locale = "en";

        if (connection.type === "web") {
          const host = connection.rawConnection.req.headers.host;
          if (host === "usa.site.com") {
            locale = "en-US";
          }
          if (host === "uk.site.com") {
            locale = "en-GB";
          }
          if (host === "es.site.com") {
            locale = "es-ES";
          }
          if (host === "mx.site.com") {
            locale = "es-MX";
          }
        }

        return locale;
      }
    };
  }
}

To tell i18n to use this method with a new connection, set config.i18n.determineConnectionLocale = 'api.customLocalization.lookup'. Now you can localize responses in actions, based on which hostname a connection uses.

import { Action } from "actionhero";

export class RandomNumber extends Action {
  constructor() {
    super();
    this.name = "randomNumber";
    this.description =
      "I am an API method which will generate a random number, returning both the number and a localized string";
    this.outputExample = {
      number: 0.234,
      localizedResponse: "Your random number is 0.234"
    };
  }

  async run({ connection, response }) {
    const number = Math.random();
    const localizedResponse = connection.localize([
      "Your random number is {{number}}",
      { number: number }
    ]);
    response.message = localizedResponse;
    response.number = number;
  }
}

Connection Methods


  • connection.localize(string) or connection.localize([string-with-interpolation, values])
    • Allows you to interpolate a string based on the connection's current locale. For example, say in an action you wanted to respond with {CountExample} In your locale files, you would define the count was {{count}} in every language you cared about, and not need to modify the action itself at all.

Other Strings


  • To localize strings that are not used in methods mentioned above you can use api.i18n.localize(string, options).
    • Allows you to interpolate a string.
    • Just as the other localize methods above, the input string will be in your locale files for you to change it anytime you want.
    • The second options optional argument (default value is api.i18n) allows you to configure i18n. Note that you will use this argument only in very few special cases, It is recommended to edit the global api.config.i18n settings to suit your localization needs.

    Solutions

    Actionhero was built from the ground up to include all the features you expect from a modern API framework.

    Open Source


    The Actionhero server is open source, under the Apache-2 license


    Actionhero runs on Linux, OS X, and Windows


    You always have access to the Actionhero team via Slack and Github



    Premium Training & Review


    We provide support for corporate & nonprofit customers starting at a flat rate of $150/hr. Our services include:


    • Remote training for your team
    • Code Reviews
    • Best Practices Audits
    • Custom plugin & Feature Development

    We have packages appropriate for all company sizes. Contact us to learn more.


    Premium Training & Review


    We provide support for corporate & nonprofit customers starting at a flat rate of $150/hr. Our services include:


    • Remote training for your team
    • Code Reviews
    • Best Practices Workshops
    • Custom plugin & Feature Development

    We have packages appropriate for all company sizes. Contact us to learn more.


    Enterprise


    For larger customers in need of a support contract, we offer an enterprise plan including everything in the Premium plan plus:


    • 24/7 access to core members of the Actionhero Team
    • Emergency response packages
    • Deployment support
    • ...and custom development against Actionhero’s core as needed.