Localization in Extbase
Warning
The information on this page might be outdated!
Translating labels with the LocalizationUtility
Multilingual websites are widespread nowadays, which means that the
web-available texts have to be localized. Extbase provides the helper class
\TYPO3\ for the translation of the labels. Besides,
there is the Fluid ViewHelper <f:, with the help of whom you can use that
functionality in templates.
The localization class has only one public static method called translate, which
does all the translation. The method can be called like this:
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
$someString = LocalizationUtility::translate($key, $extensionName, $arguments);
$key- The identifier to be translated. If the format LLL:path:key is given, then this
identifier is used, and the parameter
$extensionis ignored. Otherwise, the fileName Resources/from the given extension is loaded, and the resulting text for the given key in the current language returned.Private/ Language/ locallang. xlf $extensionName - The extension name. It can be fetched from the request.
$arguments-
It allows you to specify an array of arguments. In the
Localization, these arguments will be passed to the functionUtility vsprintf. So you can insert dynamic values in every translation. You can find the possible wildcard specifiers under https://www.php.net/manual/function.sprintf.php#refsect1-function.sprintf-parameters.Example language file with inserted wildcards
EXT:my_extension/Resources/Private/Language/locallang.xlf<?xml version="1.0" encoding="UTF-8"?> <xliff version="1.0" xmlns="urn:oasis:names:tc:xliff:document:1.1"> <file source-language="en" datatype="plaintext" original="messages" date="..." product-name="..."> <header/> <body> <trans-unit id="count_posts"> <source>You have %d posts with %d comments written.</source> </trans-unit> <trans-unit id="greeting"> <source>Hello %s!</source> </trans-unit> </body> </file> </xliff>Called translations with arguments to fill data in wildcards
EXT:my_extension/Classes/Controller/SomeController.phpuse TYPO3\CMS\Extbase\Utility\LocalizationUtility; $someString = LocalizationUtility::translate('count_posts', 'BlogExample', [$countPosts, $countComments]) $anotherString = LocalizationUtility::translate('greeting', 'BlogExample', [$userName])Copied!
Localization of Extbase models
Identifiers in localized models
Domain models have a main identifier
uid and an additional property
_localized.
Depending on whether the overlay type
language aspect is enabled (
Language or
Language) or disabled (
Language),
the identifier contains different values.
When the overlay language aspect is enabled, then the
uid
property contains the
uid value of the default language record and
the
uid of the translated record is kept in the
_localized.
| Context | Record in language 0 | Translated record |
|---|---|---|
| Database | uid:2 | uid:11, l10n_parent:2 |
| Domain object values with Overlay language aspect enabled | uid:2, _localizedUid:2 | uid:2, _localizedUid:11 |
| Domain object values with Overlay language aspect disabled | uid:2, _localizedUid:2 | uid:11, _localizedUid:11 |
Hint
If your project uses
typo3/cms-workspaces
there is yet another
additional property,
_versioned. Refer to the
Workspaces documentation for details on
workspace overlays.