Aplus Framework Docs

Language
in package

Class Language.

Tags
see
https://www.sitepoint.com/localization-demystified-understanding-php-intl/
see
https://unicode-org.github.io/icu-docs/#/icu4c/classMessageFormat.html

Table of Contents

$currentLocale  : string
The current locale.
$debugCollector  : LanguageCollector
$defaultLocale  : string
The default locale.
$directories  : array<string|int, mixed>
List of directories to find for files.
$fallbackLevel  : FallbackLevel
The locale fallback level.
$findedLocales  : array<string|int, mixed>
List with locales of already scanned directories.
$languages  : array<string|int, mixed>
Language lines.
$supportedLocales  : array<string|int, mixed>
Supported locales. Any other will be ignored.
__construct()  : mixed
Language constructor.
addDirectory()  : static
addLines()  : static
Adds custom lines for a specific locale.
currency()  : string
Gets a currency value formatted in a given locale.
date()  : string
Gets a formatted date in a given locale.
formatMessage()  : string
getCurrentLocale()  : string
Gets the current locale.
getCurrentLocaleDirection()  : string
Gets the current locale directionality.
getDefaultLocale()  : string
Gets the default locale.
getDirectories()  : array<int, string>
Gets the list of directories where language files can be finded.
getFallbackLevel()  : FallbackLevel
Gets the Fallback Level.
getLines()  : array<string, array<string, array<string, string>>>
Gets the list of available locales, lines and texts.
getLocaleDirection()  : string
Gets text directionality based on locale.
getSupportedLocales()  : array<int, string>
Gets the list of Supported Locales.
hasLine()  : bool
Checks if Language has a line.
lang()  : string|null
Renders a language file line with dot notation format.
ordinal()  : string
Gets an ordinal number in a given locale.
render()  : string
Renders a language file line.
resetLines()  : static
setCurrentLocale()  : static
Sets the current locale.
setDebugCollector()  : static
setDefaultLocale()  : static
Sets the default locale.
setDirectories()  : static
Sets a list of directories where language files can be found.
setFallbackLevel()  : static
Sets the Fallback Level.
setSupportedLocales()  : static
Sets a list of Supported Locales.
addFindedLocale()  : static
Adds a locale to the list of already scanned directories.
findFilenames()  : array<int, string>
Find for absolute file paths from where language lines can be loaded.
findLines()  : static
Find and add lines.
getFallbackLine()  : array<int, string|null>
Gets a text line and locale according the Fallback Level.
getFileLines()  : array<int, string>
getLine()  : string|null
Gets a language line text.
getRenderedLine()  : array<string, string>
isFindedLocale()  : bool
Tells if a locale already was found in the directories.
reindex()  : void

Properties

$currentLocale

The current locale.

protected string $currentLocale

$defaultLocale

The default locale.

protected string $defaultLocale

$directories

List of directories to find for files.

protected array<string|int, mixed> $directories = []

$fallbackLevel

The locale fallback level.

protected FallbackLevel $fallbackLevel = \Framework\Language\FallbackLevel::default

$findedLocales

List with locales of already scanned directories.

protected array<string|int, mixed> $findedLocales = []

$languages

Language lines.

protected array<string|int, mixed> $languages = []

List of "locale" => "file" => "line" => "text"

$supportedLocales

Supported locales. Any other will be ignored.

protected array<string|int, mixed> $supportedLocales = []

The default locale is always supported.

Methods

__construct()

Language constructor.

public __construct([string $locale = 'en' ][, array<int, string> $directories = [] ]) : mixed
Parameters
$locale : string = 'en'

The default (and current) locale code

$directories : array<int, string> = []

List of directory paths to find for language files

Return values
mixed

addDirectory()

public addDirectory(string $directory) : static
Parameters
$directory : string
Return values
static

addLines()

Adds custom lines for a specific locale.

public addLines(string $locale, string $file, array<string|int, string> $lines) : static

Useful to set lines from a database or any parsed file.

NOTE: This function will always replace the old lines, as given from files.

Parameters
$locale : string

The locale code

$file : string

The file name

$lines : array<string|int, string>

An array of "line" => "text"

Return values
static

currency()

Gets a currency value formatted in a given locale.

public currency(float $value, string $currency[, string|null $locale = null ]) : string
Parameters
$value : float

The money value

$currency : string

The Currency code. i.e. USD, BRL, JPY

$locale : string|null = null

A custom locale or null to use the current

Tags
see
https://en.wikipedia.org/wiki/ISO_4217#Active_codes
Return values
string

date()

Gets a formatted date in a given locale.

public date(int $time[, string|null $style = null ][, string|null $locale = null ]) : string
Parameters
$time : int

An Unix timestamp

$style : string|null = null

One of: short, medium, long or full. Leave null to use short

$locale : string|null = null

A custom locale or null to use the current

Tags
throws
InvalidArgumentException

for invalid style format

Return values
string

formatMessage()

public formatMessage(string $text[, array<string|int, mixed> $args = [] ][, string|null $locale = null ]) : string
Parameters
$text : string
$args : array<string|int, mixed> = []
$locale : string|null = null
Return values
string

getCurrentLocale()

Gets the current locale.

public getCurrentLocale() : string
Return values
string

getCurrentLocaleDirection()

Gets the current locale directionality.

public getCurrentLocaleDirection() : string
Return values
string

'ltr' for Left-To-Right ot 'rtl' for Right-To-Left

getDefaultLocale()

Gets the default locale.

public getDefaultLocale() : string
Return values
string

getDirectories()

Gets the list of directories where language files can be finded.

public getDirectories() : array<int, string>
Return values
array<int, string>

getFallbackLevel()

Gets the Fallback Level.

public getFallbackLevel() : FallbackLevel
Return values
FallbackLevel

getLines()

Gets the list of available locales, lines and texts.

public getLines() : array<string, array<string, array<string, string>>>
Return values
array<string, array<string, array<string, string>>>

getSupportedLocales()

Gets the list of Supported Locales.

public getSupportedLocales() : array<int, string>
Return values
array<int, string>

hasLine()

Checks if Language has a line.

public hasLine(string $file, string $line[, string|null $locale = null ]) : bool
Parameters
$file : string

The file

$line : string

The file line

$locale : string|null = null

A custom locale or null to use the current

Return values
bool

True if the line is found, otherwise false

lang()

Renders a language file line with dot notation format.

public lang(string $line[, array<string|int, mixed> $args = [] ][, string|null $locale = null ]) : string|null

E.g. home.hello matches home for file and hello for line.

Parameters
$line : string

The dot notation file line

$args : array<string|int, mixed> = []

The arguments to be used in the formatted text

$locale : string|null = null

A custom locale or null to use the current

Return values
string|null

The rendered text or null if not found

ordinal()

Gets an ordinal number in a given locale.

public ordinal(int $number[, string|null $locale = null ]) : string
Parameters
$number : int

The number to be converted to ordinal

$locale : string|null = null

A custom locale or null to use the current

Return values
string

render()

Renders a language file line.

public render(string $file, string $line[, array<string|int, mixed> $args = [] ][, string|null $locale = null ]) : string
Parameters
$file : string

The file

$line : string

The file line

$args : array<string|int, mixed> = []

The arguments to be used in the formatted text

$locale : string|null = null

A custom locale or null to use the current

Return values
string

The rendered text or file.line expression

resetLines()

public resetLines() : static
Return values
static

setCurrentLocale()

Sets the current locale.

public setCurrentLocale(string $locale) : static
Parameters
$locale : string

The current locale. This automatically is set as one of Supported Locales.

Return values
static

setDefaultLocale()

Sets the default locale.

public setDefaultLocale(string $locale) : static
Parameters
$locale : string

The default locale. This automatically is set as one of Supported Locales.

Return values
static

setDirectories()

Sets a list of directories where language files can be found.

public setDirectories(array<string|int, string> $directories) : static
Parameters
$directories : array<string|int, string>

a list of valid directory paths

Tags
throws
InvalidArgumentException

if a directory path is inaccessible

Return values
static

setFallbackLevel()

Sets the Fallback Level.

public setFallbackLevel(FallbackLevel|int $level) : static
Parameters
$level : FallbackLevel|int
Return values
static

setSupportedLocales()

Sets a list of Supported Locales.

public setSupportedLocales(array<string|int, string> $locales) : static

NOTE: the default locale always is supported. But the current can be exclude if this function is called after Language::setCurrentLocale().

Parameters
$locales : array<string|int, string>

the supported locales

Return values
static

addFindedLocale()

Adds a locale to the list of already scanned directories.

protected addFindedLocale(string $locale) : static
Parameters
$locale : string
Return values
static

findFilenames()

Find for absolute file paths from where language lines can be loaded.

protected findFilenames(string $locale, string $file) : array<int, string>
Parameters
$locale : string

The required locale

$file : string

The required file

Return values
array<int, string>

a list of valid filenames

findLines()

Find and add lines.

protected findLines(string $locale, string $file) : static

This method can be overridden to find lines in custom storage, such as in a database table.

Parameters
$locale : string
$file : string
Return values
static

getFallbackLine()

Gets a text line and locale according the Fallback Level.

protected getFallbackLine(string $locale, string $file, string $line) : array<int, string|null>
Parameters
$locale : string

The locale to get his fallback line

$file : string

The file

$line : string

The line

Return values
array<int, string|null>

Two numeric keys containg the used locale and text

getFileLines()

protected getFileLines(string $filename) : array<int, string>
Parameters
$filename : string
Return values
array<int, string>

getLine()

Gets a language line text.

protected getLine(string $locale, string $file, string $line) : string|null
Parameters
$locale : string

The required locale

$file : string

The required file

$line : string

The required line

Return values
string|null

The text of the line or null if the line is not found

getRenderedLine()

protected getRenderedLine(string $file, string $line[, array<string|int, mixed> $args = [] ][, string|null $locale = null ]) : array<string, string>
Parameters
$file : string
$line : string
$args : array<string|int, mixed> = []
$locale : string|null = null
Return values
array<string, string>

isFindedLocale()

Tells if a locale already was found in the directories.

protected isFindedLocale(string $locale) : bool
Parameters
$locale : string

The locale

Tags
see
Language::getLine()
Return values
bool

reindex()

protected reindex() : void
Return values
void

Search results