dreamworkAPI
Ctrl K
GitHub
libraryDocs / fluent

fluent

librarysharedmenusource

A port of Project Fluent, the localization system, based on @fluent/bundle.

lua
local resource = fluent.Resource( [[
-brand = Dreamwork
welcome = Welcome to { -brand }, { $name }!
files = { $count ->
    [one] { $count } file
   *[other] { $count } files
}
]] )

local bundle = fluent.Bundle( "en-US" )
bundle:addResource( resource )

print( bundle:format( "welcome", { name = "Player" } ) ) -- Welcome to Dreamwork, Player!
print( bundle:format( "files", { count = 1500 } ) ) -- 1,500 files

Classes & types

Fields

NameTypeDescription
localestable<string, fluent.Locale>The registered locales by their normalized tags.
builtinstable<string, fluent.Function>The built-in functions available to every bundle, NUMBER and DATETIME.

Functions

fluent.formatDateTimeFormats the Unix timestamp like Intl.DateTimeFormat, see fluent.DateTimeOptions.
fluent.formatNumberFormats the number like Intl.NumberFormat.
fluent.getLocaleReturns the formatting data of the locale.
fluent.isTypeChecks if the value is a Fluent type ( fluent.None, fluent.Number, fluent.DateTime or a custom one ).
fluent.normalizeTagNormalizes the BCP 47 locale tag, en_us -> en-US, zh-hant-tw -> zh-Hant-TW.
fluent.pluralCategoryReturns the plural category of the number like Intl.PluralRules.
fluent.setLocaleRegisters the formatting data of the locale, missing fields are taken from the parent locale.

#fluent.formatDateTime

sharedmenunodiscardsource
function fluent.formatDateTime(timestamp: number, options?: fluent.DateTimeOptions, locale?: ( fluent.Locale | string )?): string

Formats the Unix timestamp like Intl.DateTimeFormat, see fluent.DateTimeOptions.

lua
print( fluent.formatDateTime( 905989690, { dateStyle = "long", timeZone = "UTC" }, "en" ) ) -- September 16, 1998
print( fluent.formatDateTime( 905989690, { dateStyle = "short", timeStyle = "short", timeZone = "UTC" }, "ru" ) ) -- 16.09.1998, 23:48
Parameters
NameTypeDescription
timestampnumberThe Unix timestamp in seconds.
optionsoptionalfluent.DateTimeOptionsThe formatting options.
localeoptional( fluent.Locale | string )?The locale or its tag, the root locale by default.
Returns
TypeNameDescription
stringstrThe formatted date and time.

#fluent.formatNumber

sharedmenunodiscardsource
function fluent.formatNumber(value: number, options?: fluent.NumberOptions, locale?: ( fluent.Locale | string )?): string

Formats the number like Intl.NumberFormat.

lua
print( fluent.formatNumber( 1234.5, { minimumFractionDigits = 2 }, "en" ) ) -- 1,234.50
print( fluent.formatNumber( 0.25, { style = "percent" }, "ru" ) ) -- 25 %
print( fluent.formatNumber( 9.99, { style = "currency", currency = "EUR" }, "de" ) ) -- 9,99 €
Parameters
NameTypeDescription
valuenumberThe number to format.
optionsoptionalfluent.NumberOptionsThe formatting options.
localeoptional( fluent.Locale | string )?The locale or its tag, the root locale by default.
Returns
TypeNameDescription
stringstrThe formatted number.

#fluent.getLocale

sharedmenunodiscardsource
function fluent.getLocale(tag: string): fluent.Locale

Returns the formatting data of the locale.

Subtags are removed one by one until a registered locale is found, pt-BR -> pt, the data of unknown languages uses ISO dates and the other plural category only.

Parameters
NameTypeDescription
tagstringThe BCP 47 locale tag, e.g. "pt-BR", "zh-Hant-TW".
Returns
TypeNameDescription
fluent.LocalelocaleThe locale data.

#fluent.isType

sharedmenunodiscardsource
function fluent.isType(value: any): boolean

Checks if the value is a Fluent type ( fluent.None, fluent.Number, fluent.DateTime or a custom one ).

Parameters
NameTypeDescription
valueanyThe value to check.
Returns
TypeNameDescription
booleanis_typetrue if the value inherits fluent.Type.

#fluent.normalizeTag

sharedmenunodiscardsource
function fluent.normalizeTag(tag: string): string

Normalizes the BCP 47 locale tag, en_us -> en-US, zh-hant-tw -> zh-Hant-TW.

Parameters
NameTypeDescription
tagstringThe locale tag.
Returns
TypeNameDescription
stringnormalized_tagThe normalized tag.

#fluent.pluralCategory

sharedmenunodiscardsource
function fluent.pluralCategory(value: number, options?: fluent.NumberOptions, locale?: ( fluent.Locale | string )?): fluent.Category

Returns the plural category of the number like Intl.PluralRules.

The fraction and significant digits options affect the category, 1.0 is other in English.

Parameters
NameTypeDescription
valuenumberThe number.
optionsoptionalfluent.NumberOptionsThe digits options and the rule type.
localeoptional( fluent.Locale | string )?The locale or its tag, the root locale by default.
Returns
TypeNameDescription
fluent.CategorycategoryThe plural category.

#fluent.setLocale

sharedmenusource
function fluent.setLocale(tag: string, data: table, parent?: string): fluent.Locale

Registers the formatting data of the locale, missing fields are taken from the parent locale.

lua
fluent.setLocale( "uk", {
    months = { "січня", "лютого", ... },
    date = { short = "{dd}.{MM}.{y}", long = "{d} {MMMM} {y} р." }
}, "ru" )
Parameters
NameTypeDescription
tagstringThe BCP 47 locale tag.
datatableThe locale data, see fluent.Locale, becomes the locale object.
parentoptionalstringThe tag of the parent locale, the root locale if nil.
Returns
TypeNameDescription
fluent.LocalelocaleThe registered locale.
Generated from the dreamwork meta branch.