StackpressGitHub

API reference

Language

Language manages locale selection and phrase translation. Stackpress uses it with request/session data and saves locale changes back through the response.

Import

import { Language } from 'stackpress/language';

Static Properties

PropertyTypeDescription
localesstring[]Configured locale keys.
keystringRequest/session key used for locale storage.

Static Methods

Language.configure(key, languages)

Configures the locale storage key and language map.

Returns the Language class.

Language.language(name)

Finds one configured language by locale key.

Returns the language entry with label and translations, or null.

Language.load(req, defaults?)

Creates a Language instance from request data/session data.

Returns a Language instance.

Instance Properties

PropertyTypeDescription
labelstringHuman-readable label for the active locale.
localestringActive locale. Can be assigned to change locale.
translationsRecord<string, string>Translation map for the active locale.

Instance Methods

save(res)

Saves the current locale to the response/session surface.

Returns the language instance.

update(locale, res)

Changes locale and saves it to the response.

Returns the language instance.

translate(phrase, ...variables)

Translates a phrase and interpolates scalar variables.

Returns the translated string, or the input phrase when no translation is available.

Example

const language = Language.load(req, 'en_US');
const label = language.translate('Sign In');
language.update('en_US', res);

This example loads the active language from the request, translates one phrase, and writes the selected locale back to the response.