joomla/language
Joomla Framework Language package for internationalization and translations. Provides interfaces and utilities to manage language strings, load translation files, and integrate multilingual support in PHP 8.1+ applications (requires joomla/string).
The LanguageFactory class is the Language package's service provider and is used to retrieve objects provided by the Language
package.
There are two ways to instantiate a LanguageFactory object. The class can be instantiated directly or a shared instance may
be retrieved from a Joomla\DI\Container.
use Joomla\Language\LanguageFactory;
$languageFactory = new LanguageFactory;
Note: In order to use this method, your project must utilise Joomla's Dependency Injection package.
use Joomla\DI\Container;
use Joomla\Language\Service\LanguageFactoryProvider;
$container = new Container;
$container->registerServiceProvider(new LanguageFactoryProvider);
$languageFactory = $container->get('Joomla\Language\LanguageFactory');
The LanguageFactory object may be further configured if a config object is registered to the DI Container. The config
object must be an instance of Joomla\Registry\Registry.
use Joomla\DI\Container;
use Joomla\Language\Service\LanguageFactoryProvider;
use Joomla\Registry\Registry;
$config = new Registry;
$config->set('language.basedir', '/var/www/jfw-application');
$config->set('language.default', 'en-GB');
$container = new Container;
$container->set('config', $config);
$container->registerServiceProvider(new LanguageFactoryProvider);
$languageFactory = $container->get('Joomla\Language\LanguageFactory');
The getLanguage method is used to retrieve a Language instance. The factory caches a Language instance for each language
that has been requested.
/*
* [@param](https://github.com/param) string $lang The language to use.
* [@param](https://github.com/param) string $path The base path to the language folder. This is required if creating a new instance.
* [@param](https://github.com/param) boolean $debug The debug mode.
*/
public function getLanguage($lang = null, $path = null, $debug = false)
If the $lang or $path parameters are not set, the default values stored in the factory instance will be used instead.
The following example demonstrates basic usage of the LanguageFactory class to retrieve a Language instance. This assumes
a Factory instance has been stored to a DI container as demonstrated above.
$languageFactory = $container->get('Joomla\Language\LanguageFactory');
$language = $languageFactory->getLangauge();
The getLocalise method is used to retrieve a LocaliseInterface instance.
/*
* [@param](https://github.com/param) string $lang Language to check.
* [@param](https://github.com/param) string $basePath Base path to the language folder.
*/
public function getLocalise($lang, $basePath = null)
If the $basePath parameter is not set, the default language path stored in the factory instance will be used instead.
The following example demonstrates basic usage of the LanguageFactory class to retrieve a LocaliseInterface instance.
This assumes a Factory instance has been stored to a DI container as demonstrated above.
$languageFactory = $container->get('Joomla\Language\LanguageFactory');
$language = $languageFactory->getLocalise('en-GB');
Note that if a class is found at the lookup path and does not implement the LocaliseInterface a RuntimeException is thrown.
If a LocaliseInterface object is not found in the lookup path, a En_GBLocalise object will be returned as a default implementation.
How can I help you explore Laravel packages today?