LanguageService
This class is used to translate strings in plain PHP. For examples
see Localization in PHP. A
Language should not
be created directly, therefore its constructor is internal. Create a
Language with the
LanguageServiceFactory.
In the backend context a
Language is stored in the global
variable
$GLOBALS.
In the frontend a
Language can
be accessed via the contentObject:
<?php
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
final class ExampleController
{
private ServerRequestInterface $request;
public function __construct(
private readonly LanguageServiceFactory $LanguageServiceFactory,
) {}
public function processAction(
string $content,
array $configurations,
ServerRequestInterface $request,
): string {
$this->request = $request;
// ...
$content .= $this->getTranslatedLabel(
'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:labels.exampleLabel',
);
// ...
return $content;
}
private function getTranslatedLabel(string $key): string
{
$language =
$this->request->getAttribute('language')
?? $this->request->getAttribute('site')->getDefaultLanguage();
$languageService = $this->LanguageServiceFactory
->createFromSiteLanguage($language);
return $languageService->sL($key);
}
}
In CLI context $GLOBALS is also not available and has to be
instantiated manually.
- class LanguageService
-
- Fully qualified name
-
\TYPO3\
CMS\ Core\ Localization\ Language Service
Main API to fetch labels from XLF (label files) based on the current system language of TYPO3. It is able to resolve references to files + their pointers to the proper language. If you see something about "LLL", this class does the trick for you. It is not related to language handling of content, but rather of labels for plugins.
Usually this is injected into $GLOBALS['LANG'] when in backend or CLI context, and populated by the current backend user. Do not rely on $GLOBAL['LANG'] in frontend, as it is only available under certain circumstances!
As TYPO3 internally does not match the proper ISO locale standard, the "locale" here is actually a list of supported language keys, (see Locales class), whereas "English" is always the fallback ("default language").
Further usages on setting up your own LanguageService in BE:
$languageService = GeneralUtility::makeInstance(LanguageServiceFactory::class) ->createFromUserPreferences($GLOBALS['BE_USER']);Copied!- sL ( ?string $input)
-
Main and most often used method.
Resolve strings like these:
'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0' 'LLL:core.messages:labels.depth_0' 'core.messages:labels.depth_0' // LLL: prefix is optionalCopied!This looks up the given .xlf file path or translation domain in the 'core' extension for label labels.depth_0
The LLL: prefix is optional. If the input contains a colon (:), it will be treated as a label reference. If no colon is found, the input string is returned as-is (constant non-localizable label).
Only the plain string contents of a language key, like "Record title: %s" are returned. Placeholder interpolation must be performed separately, for example via
sprintf, like() Localizationdoes internally (which should only be used in Extbase context)Utility:: translate () Example: Label is defined in
EXT:as:my_ ext/ Resources/ Private/ Language/ locallang. xlf <trans-unit id="downloaded_times"> <source>downloaded %d times from %s locations</source> </trans-unit>Copied!The following code example assumes
$this->requestto hold the current request object. There are several ways to create the LanguageService using the Factory, depending on the context. Please adjust this example to your use case:$language = $this->request->getAttribute('language'); $languageService = GeneralUtility::makeInstance(LanguageServiceFactory::class) ->createFromSiteLanguage($language); $label = sprintf( $languageService->sL( 'LLL:EXT:my_ext/Resources/Private/Language/locallang.xlf:downloaded_times' ), 27, 'several' );Copied!This will result in
$labelto contain'downloaded 27 times from several locations'.- param $input
-
Label key/reference
- Returns
-
string