Skip to content

Class: I18nMiddleware ​

The entrypoint middleware choosing the language of each request. Add it to the middlewares of buildEntrypoints(). For each request, it reads the languages the request asks for (I18nMiddleware.requested), negotiates them with the supported languages of the I18n component (I18n.negotiate, the default language when none matches), and makes the result:

  • the request's context.locale;
  • the language of the Translator the request's objects inject;
  • the language of the validation errors of the request (its input, @Accept/@Return, the DTO.validate calls of its code);
  • the language of the exceptions it throws: a lakutata Exception whose errno has a message errors.<errno> (errors.E_ORDER_NOT_FOUND) gets that message, formatted with the exception's template parameters.

The languages are read, preferred first: for HTTP, the lang query parameter, the lang cookie, then Accept-Language; for Socket.IO, the same from the connection's handshake; for the HTTP service adapter and the JSON-RPC adapter over HTTP or WebSocket, the same from the HTTP request (for WebSocket, the request opening the connection); for the command line and the other service transports, the process's environment (environmentLocales) then the process's language; for the schedules, none. It uses the component registered as i18n; a subclass changes the component, the names of the query parameter and of the cookie, or the languages read (requested()).

Example ​

typescript
import {type BaseContext} from 'lakutata/com/entrypoint'
import {Inject} from 'lakutata/decorator/di'
import {I18nMiddleware, type I18n} from 'lakutata/com/i18n'

//BuildEntrypoints({middlewares: [LanguageMiddleware], ...}) in place of I18nMiddleware
export class LanguageMiddleware extends I18nMiddleware {
    //The component registered as "lang"
    @Inject('lang')
    protected readonly i18n: I18n

    //?locale=zh-CN
    protected readonly queryParameter: string = 'locale'

    protected readonly environmentVariable: string = 'SHOP_LANG'

    //The language saved by the signed-in user first
    protected requested(context: BaseContext): string[] {
        const saved: unknown = context.data.userLanguage
        return [...(typeof saved === 'string' ? [saved] : []), ...super.requested(context)]
    }
}

Extends ​

Constructors ​

Constructor ​

new I18nMiddleware(cradleProxy): I18nMiddleware

Called by the container only: an object is created through a container (see the class description), never with new.

Parameters ​

cradleProxy ​

Record<string | symbol, any>

The container's resolution proxy.

Returns ​

I18nMiddleware

Inherited from ​

Middleware.constructor

Properties ​

constructor ​

constructor: Function

The initial value of Object.prototype.constructor is the standard built-in Object constructor.

Inherited from ​

Middleware.constructor

Accessors ​

$id ​

Get Signature ​

get $id(): string | symbol

The name the object was registered under (its key in the module options), or a symbol of its class for an object registered by its class or built without registration.

Returns ​

string | symbol

Inherited from ​

Middleware.$id


$symbol ​

Get Signature ​

get $symbol(): symbol

A symbol unique to the instance (made from its $uuid), created on first read.

Returns ​

symbol

Inherited from ​

Middleware.$symbol


$uuid ​

Get Signature ​

get $uuid(): string

A random UUID identifying the instance.

Returns ​

string

Inherited from ​

Middleware.$uuid


className ​

Get Signature ​

get static className(): string

The name of the class.

Returns ​

string

Inherited from ​

Middleware.className


className ​

Get Signature ​

get className(): string

The name of the object's class.

Returns ​

string

Inherited from ​

Middleware.className

Methods ​

[asyncDispose]() ​

[asyncDispose](): Promise<void>

Dispose the object at the end of an await using block, as BaseObject.dispose.

Returns ​

Promise<void>

Resolves once the object is destroyed.

Inherited from ​

Middleware.[asyncDispose]


acceptsOption() ​

static acceptsOption(name, value): boolean

Whether the class accepts an option which is not one of its @Configurable properties (an option it reads with getConfigurableRecords()): the modules check the options of the objects they declare and warn of the options the classes do not accept. Override it in the classes reading options without configurable properties.

Parameters ​

name ​

string

The option name.

value ​

unknown

The option value.

Returns ​

boolean

true when the option is accepted; false by default.

Inherited from ​

Middleware.acceptsOption


assign() ​

Call Signature ​

static assign<T, U>(target, source): T & U

Copy the values of all of the enumerable own properties from one or more source objects to a target object. Returns the target object.

Type Parameters ​
T ​

T extends object

U ​

U

Parameters ​
target ​

T

The target object to copy to.

source ​

U

The source object from which to copy properties.

Returns ​

T & U

Inherited from ​

Middleware.assign

Call Signature ​

static assign<T, U, V>(target, source1, source2): T & U & V

Copy the values of all of the enumerable own properties from one or more source objects to a target object. Returns the target object.

Type Parameters ​
T ​

T extends object

U ​

U

V ​

V

Parameters ​
target ​

T

The target object to copy to.

source1 ​

U

The first source object from which to copy properties.

source2 ​

V

The second source object from which to copy properties.

Returns ​

T & U & V

Inherited from ​

Middleware.assign

Call Signature ​

static assign<T, U, V, W>(target, source1, source2, source3): T & U & V & W

Copy the values of all of the enumerable own properties from one or more source objects to a target object. Returns the target object.

Type Parameters ​
T ​

T extends object

U ​

U

V ​

V

W ​

W

Parameters ​
target ​

T

The target object to copy to.

source1 ​

U

The first source object from which to copy properties.

source2 ​

V

The second source object from which to copy properties.

source3 ​

W

The third source object from which to copy properties.

Returns ​

T & U & V & W

Inherited from ​

Middleware.assign

Call Signature ​

static assign(target, ...sources): any

Copy the values of all of the enumerable own properties from one or more source objects to a target object. Returns the target object.

Parameters ​
target ​

object

The target object to copy to.

sources ​

...any[]

One or more source objects from which to copy properties

Returns ​

any

Inherited from ​

Middleware.assign


create() ​

Call Signature ​

static create(o): any

Creates an object that has the specified prototype or that has null prototype.

Parameters ​
o ​

object | null

Object to use as a prototype. May be null.

Returns ​

any

Inherited from ​

Middleware.create

Call Signature ​

static create(o, properties): any

Creates an object that has the specified prototype, and that optionally contains specified properties.

Parameters ​
o ​

object | null

Object to use as a prototype. May be null

properties ​

PropertyDescriptorMap & ThisType<any>

JavaScript object that contains one or more property descriptors.

Returns ​

any

Inherited from ​

Middleware.create


defineProperties() ​

static defineProperties<T>(o, properties): T

Adds one or more properties to an object, and/or modifies attributes of existing properties.

Type Parameters ​

T ​

T

Parameters ​

o ​

T

Object on which to add or modify the properties. This can be a native JavaScript object or a DOM object.

properties ​

PropertyDescriptorMap & ThisType<any>

JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property.

Returns ​

T

Inherited from ​

Middleware.defineProperties


defineProperty() ​

static defineProperty<T>(o, p, attributes): T

Adds a property to an object, or modifies attributes of an existing property.

Type Parameters ​

T ​

T

Parameters ​

o ​

T

Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.

p ​

PropertyKey

The property name.

attributes ​

PropertyDescriptor & ThisType<any>

Descriptor for the property. It can be for a data property or an accessor property.

Returns ​

T

Inherited from ​

Middleware.defineProperty


dispose() ​

dispose(): Promise<void>

Destroy the object: its beforeDestroy(), the objects it created, then its destroy(). Calling it more than once destroys once. A disposed singleton or scoped object is dropped from its container, which creates a new one on the next request. Using a destroyed object (getObject(), buildObject()) throws an ObjectDestroyedException.

Returns ​

Promise<void>

Resolves once the object is destroyed.

Throws ​

The error of the object's own beforeDestroy() or destroy() (an AggregateError when several fail); the errors of the objects it created are reported through the application's logger instead.

Inherited from ​

Middleware.dispose


entries() ​

Call Signature ​

static entries<T>(o): [string, T][]

Returns an array of key/values of the enumerable own properties of an object

Type Parameters ​
T ​

T

Parameters ​
o ​

{[s: string]: T; } | ArrayLike<T>

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

[string, T][]

Inherited from ​

Middleware.entries

Call Signature ​

static entries(o): [string, any][]

Returns an array of key/values of the enumerable own properties of an object

Parameters ​
o ​

{ }

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

[string, any][]

Inherited from ​

Middleware.entries


freeze() ​

Call Signature ​

static freeze<T>(f): T

Prevents the modification of existing property attributes and values, and prevents the addition of new properties.

Type Parameters ​
T ​

T extends Function

Parameters ​
f ​

T

Object on which to lock the attributes.

Returns ​

T

Inherited from ​

Middleware.freeze

Call Signature ​

static freeze<T, U>(o): Readonly<T>

Prevents the modification of existing property attributes and values, and prevents the addition of new properties.

Type Parameters ​
T ​

T extends object

U ​

U extends string | number | bigint | boolean | symbol

Parameters ​
o ​

T

Object on which to lock the attributes.

Returns ​

Readonly<T>

Inherited from ​

Middleware.freeze

Call Signature ​

static freeze<T>(o): Readonly<T>

Prevents the modification of existing property attributes and values, and prevents the addition of new properties.

Type Parameters ​
T ​

T

Parameters ​
o ​

T

Object on which to lock the attributes.

Returns ​

Readonly<T>

Inherited from ​

Middleware.freeze


fromEntries() ​

Call Signature ​

static fromEntries<T>(entries): object

Returns an object created by key-value entries for properties and methods

Type Parameters ​
T ​

T = any

Parameters ​
entries ​

Iterable<readonly [PropertyKey, T]>

An iterable object that contains key-value entries for properties and methods.

Returns ​

object

Inherited from ​

Middleware.fromEntries

Call Signature ​

static fromEntries(entries): any

Returns an object created by key-value entries for properties and methods

Parameters ​
entries ​

Iterable<readonly any[]>

An iterable object that contains key-value entries for properties and methods.

Returns ​

any

Inherited from ​

Middleware.fromEntries


getMethod() ​

getMethod(name, throwExceptionIfNotFound?): (...args) => any

Get a method bound to the object, to call it by name.

Parameters ​

name ​

string | symbol

The method name or symbol.

throwExceptionIfNotFound? ​

boolean

Throw when the method does not exist, instead of returning a function doing nothing; false by default.

Returns ​

The bound method, or a function doing nothing (returning undefined) when it does not exist.

(...args) => any

Throws ​

When the method does not exist and throwExceptionIfNotFound is true.

Inherited from ​

Middleware.getMethod


getOwnPropertyDescriptor() ​

static getOwnPropertyDescriptor(o, p): PropertyDescriptor | undefined

Gets the own property descriptor of the specified object. An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype.

Parameters ​

o ​

any

Object that contains the property.

p ​

PropertyKey

Name of the property.

Returns ​

PropertyDescriptor | undefined

Inherited from ​

Middleware.getOwnPropertyDescriptor


getOwnPropertyDescriptors() ​

static getOwnPropertyDescriptors<T>(o): { [P in string | number | symbol]: TypedPropertyDescriptor<T[P]> } & object

Returns an object containing all own property descriptors of an object

Type Parameters ​

T ​

T

Parameters ​

o ​

T

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

{ [P in string | number | symbol]: TypedPropertyDescriptor<T[P]> } & object

Inherited from ​

Middleware.getOwnPropertyDescriptors


getOwnPropertyNames() ​

static getOwnPropertyNames(o): string[]

Returns the names of the own properties of an object. The own properties of an object are those that are defined directly on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions.

Parameters ​

o ​

any

Object that contains the own properties.

Returns ​

string[]

Inherited from ​

Middleware.getOwnPropertyNames


getOwnPropertySymbols() ​

static getOwnPropertySymbols(o): symbol[]

Returns an array of all symbol properties found directly on object o.

Parameters ​

o ​

any

Object to retrieve the symbols from.

Returns ​

symbol[]

Inherited from ​

Middleware.getOwnPropertySymbols


getProperty() ​

getProperty<T>(propertyKey, defaultValue?): T

Read an own property of the object.

Type Parameters ​

T ​

T = any

Parameters ​

propertyKey ​

string

The property name.

defaultValue? ​

T

The value returned when the object has no own property of this name.

Returns ​

T

The property value, or the default value.

Inherited from ​

Middleware.getProperty


getPrototypeOf() ​

static getPrototypeOf(o): any

Returns the prototype of an object.

Parameters ​

o ​

any

The object that references the prototype.

Returns ​

any

Inherited from ​

Middleware.getPrototypeOf


handle() ​

handle(invocation, next): Promise<unknown>

Negotiate the request's language, set context.locale, create the request's Translator, then run the next middlewares and the action with the validations in that language, translating the exception they throw.

Parameters ​

invocation ​

ActionInvocation

The action invocation: its context and its container.

next ​

Next

Runs the rest of the chain.

Returns ​

Promise<unknown>

The result of the rest of the chain; an exception it throws is rethrown, its message translated by I18nMiddleware.translate.

Overrides ​

Middleware.handle


hasMethod() ​

hasMethod(name): boolean

Whether the object has a method of this name or symbol (a function of its class, not an own property).

Parameters ​

name ​

string | symbol

The method name or symbol.

Returns ​

boolean

true when the method exists.

Inherited from ​

Middleware.hasMethod


hasOwn() ​

static hasOwn(o, v): boolean

Determines whether an object has a property with the specified name.

Parameters ​

o ​

object

An object.

v ​

PropertyKey

A property name.

Returns ​

boolean

Inherited from ​

Middleware.hasOwn


hasOwnProperty() ​

hasOwnProperty(v): boolean

Determines whether an object has a property with the specified name.

Parameters ​

v ​

PropertyKey

A property name.

Returns ​

boolean

Inherited from ​

Middleware.hasOwnProperty


hasProperty() ​

hasProperty(propertyKey): boolean

Whether the object has an own property (not a method or accessor of its class) of this name or symbol.

Parameters ​

propertyKey ​

string | symbol

The property name or symbol.

Returns ​

boolean

true when the property exists.

Inherited from ​

Middleware.hasProperty


is() ​

static is(value1, value2): boolean

Returns true if the values are the same value, false otherwise.

Parameters ​

value1 ​

any

The first value.

value2 ​

any

The second value.

Returns ​

boolean

Inherited from ​

Middleware.is


isExtensible() ​

static isExtensible(o): boolean

Returns a value that indicates whether new properties can be added to an object.

Parameters ​

o ​

any

Object to test.

Returns ​

boolean

Inherited from ​

Middleware.isExtensible


isFrozen() ​

static isFrozen(o): boolean

Returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.

Parameters ​

o ​

any

Object to test.

Returns ​

boolean

Inherited from ​

Middleware.isFrozen


isPrototypeOf() ​

isPrototypeOf(v): boolean

Determines whether an object exists in another object's prototype chain.

Parameters ​

v ​

Object

Another object whose prototype chain is to be checked.

Returns ​

boolean

Inherited from ​

Middleware.isPrototypeOf


isSealed() ​

static isSealed(o): boolean

Returns true if existing property attributes cannot be modified in an object and new properties cannot be added to the object.

Parameters ​

o ​

any

Object to test.

Returns ​

boolean

Inherited from ​

Middleware.isSealed


keys() ​

Call Signature ​

static keys(o): string[]

Returns the names of the enumerable string properties and methods of an object.

Parameters ​
o ​

object

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

string[]

Inherited from ​

Middleware.keys

Call Signature ​

static keys(o): string[]

Returns the names of the enumerable string properties and methods of an object.

Parameters ​
o ​

{ }

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

string[]

Inherited from ​

Middleware.keys


preventExtensions() ​

static preventExtensions<T>(o): T

Prevents the addition of new properties to an object.

Type Parameters ​

T ​

T

Parameters ​

o ​

T

Object to make non-extensible.

Returns ​

T

Inherited from ​

Middleware.preventExtensions


propertyIsEnumerable() ​

propertyIsEnumerable(v): boolean

Determines whether a specified property is enumerable.

Parameters ​

v ​

PropertyKey

A property name.

Returns ​

boolean

Inherited from ​

Middleware.propertyIsEnumerable


propertyNames() ​

propertyNames(): string[]

The names of the object's own properties (not its class's methods and accessors).

Returns ​

string[]

The names.

Inherited from ​

Middleware.propertyNames


propertySymbols() ​

propertySymbols(): symbol[]

The symbols of the object's own properties.

Returns ​

symbol[]

The symbols.

Inherited from ​

Middleware.propertySymbols


seal() ​

static seal<T>(o): T

Prevents the modification of attributes of existing properties, and prevents the addition of new properties.

Type Parameters ​

T ​

T

Parameters ​

o ​

T

Object on which to lock the attributes.

Returns ​

T

Inherited from ​

Middleware.seal


setProperty() ​

setProperty(propertyKey, value): void

Set a property of the object; the value is not validated, even for a @Configurable property.

Parameters ​

propertyKey ​

string

The property name.

value ​

any

The value.

Returns ​

void

Inherited from ​

Middleware.setProperty


setPrototypeOf() ​

static setPrototypeOf(o, proto): any

Sets the prototype of a specified object o to object proto or null. Returns the object o.

Parameters ​

o ​

any

The object to change its prototype.

proto ​

object | null

The value of the new prototype or null.

Returns ​

any

Inherited from ​

Middleware.setPrototypeOf


toLocaleString() ​

toLocaleString(): string

Returns a date converted to a string using the current locale.

Returns ​

string

Inherited from ​

Middleware.toLocaleString


toString() ​

toString(): string

Returns a string representation of an object.

Returns ​

string

Inherited from ​

Middleware.toString


valueOf() ​

valueOf(): Object

Returns the primitive value of the specified object.

Returns ​

Object

Inherited from ​

Middleware.valueOf


values() ​

Call Signature ​

static values<T>(o): T[]

Returns an array of values of the enumerable own properties of an object

Type Parameters ​
T ​

T

Parameters ​
o ​

{[s: string]: T; } | ArrayLike<T>

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

T[]

Inherited from ​

Middleware.values

Call Signature ​

static values(o): any[]

Returns an array of values of the enumerable own properties of an object

Parameters ​
o ​

{ }

Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Returns ​

any[]

Inherited from ​

Middleware.values

Released under the MIT License.