Actionhero uses the i18n module to localize responses to clients.
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
.config.i18n.updateFiles
if you do not want this behavior.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 }) {
const number = Math.random();
const localizedResponse = connection.localize([
"Your random number is {{number}}",
{ number: number },
]);
return { message: localizedResponse, number };
}
}
connection.localize(string)
or connection.localize([string-with-interpolation, values])
{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.api.i18n.localize(string, options)
.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 config.i18n
settings to suit your localization needs.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
We provide support for corporate & nonprofit customers starting at a flat rate of $150/hr. Our services include:
We have packages appropriate for all company sizes. Contact us to learn more.
We provide support for corporate & nonprofit customers starting at a flat rate of $150/hr. Our services include:
We have packages appropriate for all company sizes. Contact us to learn more.
For larger customers in need of a support contract, we offer an enterprise plan including everything in the Premium plan plus: