dreamworkAPI
Ctrl K
GitHub
classDocs / FluentBundle

FluentBundle

classsharedmenusource
Object → FluentBundle

The collection of messages of one locale, formats them with variables.

lua
local bundle = fluent.Bundle( "ru" )
bundle:addResource( file_content )

local errors = {}
print( bundle:format( "items", { count = 3 }, errors ) ) -- 3 предмета

Constructor

FluentBundle(locales: ( string | string[] ), options: fluent.BundleOptions?): FluentBundle

Fields

NameTypeDescription
localesstring[]The locale tags, the first one is used for formatting.
localefluent.LocaleThe formatting data of the first locale.
messagestable<string, fluent.Message>The messages by their identifiers.
termstable<string, fluent.Message>The terms by their identifiers, with -.
functionstable<string, fluent.Function>The functions available to messages.
use_isolatingbooleanIf true, placeables are wrapped in Unicode bidi isolation marks.
transform( fun( text: string ): string ) | nilThe function applied to every text element.

Methods

addResourceAdds the messages and terms of the resource to the bundle.
formatFormats the message value, or its attribute if the identifier is message.attribute.
formatPatternFormats the pattern of a message or its attribute.
getMessageReturns the message, its value and attributes patterns are formatted with FluentBundle:formatPattern.
hasMessageChecks if the bundle has the message, terms are not messages.
setFunctionAdds or replaces the function available to messages.

#FluentBundle:addResource

sharedmenusource
function FluentBundle:addResource(resource: FluentResource | string, allow_overrides?: boolean): string[]

Adds the messages and terms of the resource to the bundle.

Parameters
NameTypeDescription
resourceFluentResource | stringThe resource or the FTL source to parse.
allow_overridesoptionalbooleanIf true, existing messages and terms are replaced, otherwise they are kept and reported.
Returns
TypeNameDescription
string[]errorsThe override errors, syntax errors of a parsed source are in FluentResource.errors.

#FluentBundle:format

sharedmenunodiscardsource
function FluentBundle:format(id: string, args?: table<string, fluent.Argument>, errors?: Error[]): string | nil

Formats the message value, or its attribute if the identifier is message.attribute.

Parameters
NameTypeDescription
idstringThe message identifier, e.g. "login-input" or "login-input.placeholder".
argsoptionaltable<string, fluent.Argument>The variables.
errorsoptionalError[]The list collecting the errors, see FluentBundle:formatPattern.
Returns
TypeNameDescription
string | nilstrThe formatted string, nil if the message or its attribute does not exist.

#FluentBundle:formatPattern

sharedmenunodiscardsource
function FluentBundle:formatPattern(pattern: fluent.Pattern, args?: table<string, fluent.Argument>, errors?: Error[]): string

Formats the pattern of a message or its attribute.

If errors is given, errors are collected into it and the best possible string is returned, e.g. {$name} for a missing variable, otherwise the first error is thrown.

lua
local message = bundle:getMessage( "hello" )
if message ~= nil and message.value ~= nil then
    local errors = {}
    print( bundle:formatPattern( message.value, { name = "World" }, errors ) )
end
Parameters
NameTypeDescription
patternfluent.PatternThe pattern to format.
argsoptionaltable<string, fluent.Argument>The variables.
errorsoptionalError[]The list collecting the errors.
Returns
TypeNameDescription
stringstrThe formatted string.

#FluentBundle:getMessage

sharedmenunodiscardsource
function FluentBundle:getMessage(id: string): fluent.Message | nil

Returns the message, its value and attributes patterns are formatted with FluentBundle:formatPattern.

Parameters
NameTypeDescription
idstringThe message identifier.
Returns
TypeNameDescription
fluent.Message | nilmessageThe message, nil if it does not exist.

#FluentBundle:hasMessage

sharedmenunodiscardsource
function FluentBundle:hasMessage(id: string): boolean

Checks if the bundle has the message, terms are not messages.

Parameters
NameTypeDescription
idstringThe message identifier.
Returns
TypeNameDescription
booleanhas_messagetrue if the message exists.

#FluentBundle:setFunction

sharedmenusource
function FluentBundle:setFunction(name: string, fn: fluent.Function | nil)

Adds or replaces the function available to messages.

lua
bundle:setFunction( "UPPER", function( positional )
    return string.upper( tostring( positional[ 1 ] ) )
end )
Parameters
NameTypeDescription
namestringThe function name, must be upper-case to be callable from FTL.
fnfluent.Function | nilThe function, nil removes it.
Generated from the dreamwork meta branch.