Translate ViewHelper <f:translate>

Translate a key from locallang. The files are loaded from the folder Resources/Private/Language/.

Examples

Translate key

<f:translate key="key1" />
Copied!

Value of key key1 in the current website language. Alternatively id can be used instead of key:

<f:translate id="key1" />
Copied!

This will output the same as above. If both id and key are set, id will take precedence.

Keep HTML tags

<f:format.raw><f:translate key="htmlKey" /></f:format.raw>
Copied!

Value of key htmlKey in the current website language, no htmlspecialchars() applied.

Translate key from custom locallang file

<f:translate key="key1" extensionName="MyExt"/>
Copied!

or

<f:translate key="LLL:EXT:myext/Resources/Private/Language/locallang.xlf:key1" />
Copied!

Value of key key1 in the current website language.

Inline notation with arguments and default value

{f:translate(key: 'someKey', arguments: {0: 'dog', 1: 'fox'}, default: 'default value')}
Copied!

Value of key someKey in the current website language with the given arguments (dog and fox) assigned for the specified %s conversions, using PHP sprintf() notation in the language file:

<trans-unit id="someKey" resname="someKey">
    <source>Some text about a %s and a %s.</source>
</trans-unit>
Copied!

The output will be Some text about a dog and a fox.

If the key someKey is not found in the language file, the output is default value.

As in PHP's sprintf() you can order placeholders (Second %2$s, first %1$s) or use specific types like A padded number: %'.09d, returning 000000123 for a number passed as 123. See the sprintf PHP Documentation for more information on possible formatting.

Inline notation with extension name

{f:translate(key: 'someKey', extensionName: 'SomeExtensionName')}
Copied!

Value of key someKey in the current website language. The locallang file of extension "some_extension_name" will be used.

Source code

Go to the source code of this ViewHelper: TranslateViewHelper.php (GitHub).

Arguments

The following arguments are available for <f:translate>:

Name Type Default
string
string
string
mixed
string
string
mixed
key
Type
string
Required

true

Translation Key

id
Type
string
Required

true

Translation ID. Same as key.

default
Type
string
Required

true

If the given locallang key could not be found, this value is used. If this argument is not set, child nodes will be used to render the default

arguments
Type
mixed
Required

true

Arguments to be replaced in the resulting string

extensionName
Type
string
Required

true

UpperCamelCased extension key (for example BlogExample)

languageKey
Type
string
Required

true

Language key ("da" for example) or "default" to use. Also a Locale object is possible. If empty, use current locale from the request.

alternativeLanguageKeys
Type
mixed
Required

true

Alternative language keys if no translation does exist. Ignored in non-extbase context. Deprecated, will be removed in TYPO3 v13.0