Language class

Framework\Language\Language

Class Language.

Defined in libraries/language/src/Language.php

Properties #

$currentLocale
The current locale.
$defaultLocale
The default locale.
$directories
List of directories to find for files.
$fallbackLevel
The locale fallback level.
$findedLocales
List with locales of already scanned directories.
$languages
Language lines.
$supportedLocales
Supported locales. Any other will be ignored.
$debugCollector
-

$currentLocale

protected string $currentLocale

The current locale.

$defaultLocale

protected string $defaultLocale

The default locale.

$directories

protected array $directories = array ( )

List of directories to find for files.

$fallbackLevel

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

The locale fallback level.

$findedLocales

protected array $findedLocales = array ( )

List with locales of already scanned directories.

$languages

protected array $languages = array ( )

Language lines.

$supportedLocales

protected array $supportedLocales = array ( )

Supported locales. Any other will be ignored.

$debugCollector

protected \Framework\Language\Debug\LanguageCollector $debugCollector

Methods #

__construct()
Language constructor.
addDirectory()
-
addLines()
Adds custom lines for a specific locale.
currency()
Gets a currency value formatted in a given locale.
date()
Gets a formatted date in a given locale.
formatMessage()
-
getCurrentLocale()
Gets the current locale.
getCurrentLocaleDirection()
Gets the current locale directionality.
getDefaultLocale()
Gets the default locale.
getDirectories()
Gets the list of directories where language files can be finded.
getFallbackLevel()
Gets the Fallback Level.
getLines()
Gets the list of available locales, lines and texts.
getLocaleDirection()
Gets text directionality based on locale.
getSupportedLocales()
Gets the list of Supported Locales.
hasLine()
Checks if Language has a line.
lang()
Renders a language file line with dot notation format.
ordinal()
Gets an ordinal number in a given locale.
render()
Renders a language file line.
resetLines()
-
setCurrentLocale()
Sets the current locale.
setDebugCollector()
-
setDefaultLocale()
Sets the default locale.
setDirectories()
Sets a list of directories where language files can be found.
setFallbackLevel()
Sets the Fallback Level.
setSupportedLocales()
Sets a list of Supported Locales.
addFindedLocale()
Adds a locale to the list of already scanned directories.
findFilenames()
Find for absolute file paths from where language lines can be loaded.
findLines()
Find and add lines.
getFallbackLine()
Gets a text line and locale according the Fallback Level.
getFileLines()
-
getLine()
Gets a language line text.
getRenderedLine()
-
isFindedLocale()
Tells if a locale already was found in the directories.
reindex()
-

__construct()

public function __construct(string $locale = 'en', array $directories = array ( ))

Language constructor.

Parameters
  • string $locale - The default (and current) locale code
  • array $directories - List of directory paths to find for language files

addDirectory()

public function addDirectory(string $directory): static
Parameters
  • string $directory
Returns static

addLines()

public function addLines(string $locale, string $file, array $lines): static

Adds custom lines for a specific locale.

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
  • string $locale - The locale code
  • string $file - The file name
  • array $lines - An array of "line" => "text"
Returns static

currency()

public function currency(float $value, string $currency, ?string $locale = NULL): string

Gets a currency value formatted in a given locale.

Parameters
  • float $value - The money value
  • string $currency - The Currency code. i.e. USD, BRL, JPY
  • ?string $locale - A custom locale or null to use the current
Returns string

date()

public function date(int $time, ?string $style = NULL, ?string $locale = NULL): string

Gets a formatted date in a given locale.

Parameters
  • int $time - An Unix timestamp
  • ?string $style - One of: short, medium, long or full. Leave null to use short
  • ?string $locale - A custom locale or null to use the current
Returns string
throws
InvalidArgumentException for invalid style format

formatMessage()

public function formatMessage(string $text, array $args = array ( ), ?string $locale = NULL): string
Parameters
  • string $text
  • array $args
  • ?string $locale
Returns string

getCurrentLocale()

public function getCurrentLocale(): string

Gets the current locale.

Returns string

getCurrentLocaleDirection()

public function getCurrentLocaleDirection(): string

Gets the current locale directionality.

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

getDefaultLocale()

public function getDefaultLocale(): string

Gets the default locale.

Returns string

getDirectories()

public function getDirectories(): array

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

Returns array

getFallbackLevel()

public function getFallbackLevel(): \Framework\Language\FallbackLevel

Gets the Fallback Level.

Returns \Framework\Language\FallbackLevel

getLines()

public function getLines(): array

Gets the list of available locales, lines and texts.

Returns array

getLocaleDirection()

public static function getLocaleDirection(string $locale): string

Gets text directionality based on locale.

Parameters
  • string $locale - The locale code
Returns string - 'ltr' for Left-To-Right ot 'rtl' for Right-To-Left

getSupportedLocales()

public function getSupportedLocales(): array

Gets the list of Supported Locales.

Returns array

hasLine()

public function hasLine(string $file, string $line, ?string $locale = NULL): bool

Checks if Language has a line.

Parameters
  • string $file - The file
  • string $line - The file line
  • ?string $locale - A custom locale or null to use the current
Returns bool - True if the line is found, otherwise false

lang()

public function lang(string $line, array $args = array ( ), ?string $locale = NULL): ?string

Renders a language file line with dot notation format.

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

Parameters
  • string $line - The dot notation file line
  • array $args - The arguments to be used in the formatted text
  • ?string $locale - A custom locale or null to use the current
Returns ?string - The rendered text or null if not found

ordinal()

public function ordinal(int $number, ?string $locale = NULL): string

Gets an ordinal number in a given locale.

Parameters
  • int $number - The number to be converted to ordinal
  • ?string $locale - A custom locale or null to use the current
Returns string

render()

public function render(string $file, string $line, array $args = array ( ), ?string $locale = NULL): string

Renders a language file line.

Parameters
  • string $file - The file
  • string $line - The file line
  • array $args - The arguments to be used in the formatted text
  • ?string $locale - A custom locale or null to use the current
Returns string - The rendered text or file.line expression

resetLines()

public function resetLines(): static
Returns static

setCurrentLocale()

public function setCurrentLocale(string $locale): static

Sets the current locale.

Parameters
  • string $locale - The current locale. This automatically is set as one of Supported Locales.
Returns static

setDebugCollector()

public function setDebugCollector(\Framework\Language\Debug\LanguageCollector $debugCollector): static
Parameters
  • \Framework\Language\Debug\LanguageCollector $debugCollector
Returns static

setDefaultLocale()

public function setDefaultLocale(string $locale): static

Sets the default locale.

Parameters
  • string $locale - The default locale. This automatically is set as one of Supported Locales.
Returns static

setDirectories()

public function setDirectories(array $directories): static

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

Parameters
  • array $directories - a list of valid directory paths
Returns static
throws
InvalidArgumentException if a directory path is inaccessible

setFallbackLevel()

public function setFallbackLevel(\Framework\Language\FallbackLevel|int $level): static

Sets the Fallback Level.

Parameters
  • \Framework\Language\FallbackLevel|int $level
Returns static

setSupportedLocales()

public function setSupportedLocales(array $locales): static

Sets a list of Supported Locales.

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

Parameters
  • array $locales - the supported locales
Returns static

addFindedLocale()

protected function addFindedLocale(string $locale): static

Adds a locale to the list of already scanned directories.

Parameters
  • string $locale
Returns static

findFilenames()

protected function findFilenames(string $locale, string $file): array

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

Parameters
  • string $locale - The required locale
  • string $file - The required file
Returns array - a list of valid filenames

findLines()

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

Find and add lines.

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

Parameters
  • string $locale
  • string $file
Returns static

getFallbackLine()

protected function getFallbackLine(string $locale, string $file, string $line): array

Gets a text line and locale according the Fallback Level.

Parameters
  • string $locale - The locale to get his fallback line
  • string $file - The file
  • string $line - The line
Returns array - Two numeric keys containg the used locale and text

getFileLines()

protected function getFileLines(string $filename): array
Parameters
  • string $filename
Returns array

getLine()

protected function getLine(string $locale, string $file, string $line): ?string

Gets a language line text.

Parameters
  • string $locale - The required locale
  • string $file - The required file
  • string $line - The required line
Returns ?string - The text of the line or null if the line is not found

getRenderedLine()

protected function getRenderedLine(string $file, string $line, array $args = array ( ), ?string $locale = NULL): array
Parameters
  • string $file
  • string $line
  • array $args
  • ?string $locale
Returns array

isFindedLocale()

protected function isFindedLocale(string $locale): bool

Tells if a locale already was found in the directories.

Parameters
  • string $locale - The locale
Returns bool

reindex()

protected function reindex(): void
Returns void