Download the PHP package limoncello-php/l10n without Composer
On this page you can find all versions of the php package limoncello-php/l10n. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download limoncello-php/l10n
More information about limoncello-php/l10n
Files in limoncello-php/l10n
Package l10n
Short Description Limoncello framework localization support.
License Apache-2.0
Homepage https://github.com/limoncello-php/framework/tree/master/components/L10n
Informations about the package l10n
Summary
This is localization component for Limoncello Framework.
The component helps to translate an application into different languages. In particular, it provides an ability to manage localized files easily and extract localized strings for a given locale with a fallback to default one.
Message Localization
For a given resource storage data (later on this) the usage looks like
Where
- first parameter
de_AT
is a locale code. - second parameter
ErrorMessages
is messages' namespace name. Using a namespace allows message isolation across namespaces. Typically namespace is a resource file name without.php
file extension. - third parameter
id_or_message
is a message identity. A message identity should be unique across a namespace.
It first finds the closest locale (de_AT
, de
or a default one such as en
if no locale is found) and a corresponding message.
The result would contain the found message with their locale. For example
If resources for requested locale de_AT
existed the result could look like
Resource Storage
In the example above resource storage data is an array that contains localized strings. The reason this object is introduced is a performance optimization. Looking for multiple files in a file system and analyzing their data is time and resource consuming. The storage data replaces many resource files with a single optimized for search array which could be cached.
For instance, there is a resource folder structure
Where Messages.php
is a plain PHP array file
and
Then, a storage array could be created as
Method getStorageData
has a default locale code as a first parameter.
Finally, complete example
Message Translation
In addition to Resource Storage, the package provides a helpful wrapper over MessageFormatter.
Method translateMessage
has signature translateMessage(string $locale, string $namespace, string $key, array $args = []): string
. The method can accept formatting parameters via $args
parameter. For advanced formatting samples see MessageFormatter::formatMessage.
There is another wrapper for Translator
called Formatter
which hides locale code and namespace. It could be used in an environment where locale for current session is defined for a logged in user. So the code basically asks for FormatterInterface
for current locale and specified namespace.
Recommended Usage
- Have a resource folder with locales needed for your application.
- For the resource folder and default locale create a storage data with
FileBundleEncoder
and cache it. - On page load read the storage data from the cache and load it into
Translator
withBundleStorage
. - Use the translator for message formatting.