Class

eZ\Publish\Core\Helper\TranslationHelper

class TranslationHelper

Helper class for translation.

Methods

__construct(ConfigResolverInterface $configResolver, ContentService $contentService, array $siteAccessesByLanguage, LoggerInterface $logger = null)

string getTranslatedContentName(Content $content, string $forcedLanguage = null)

Returns content name, translated.

string getTranslatedContentNameByContentInfo(ContentInfo $contentInfo, string $forcedLanguage = null)

Returns content name, translated, from a ContentInfo object.

Field|null getTranslatedField(Content $content, string $fieldDefIdentifier, string $forcedLanguage = null)

Returns Field object in the appropriate language for a given content.

string|null getTranslatedFieldDefinitionProperty(ContentType $contentType, string $fieldDefIdentifier, string $property = 'name', string $forcedLanguage = null)

Returns Field definition name in the appropriate language for a given content.

string|null getTranslatedByProperty(ValueObject $object, string $property, string $forcedLanguage = null)

Gets translated property generic helper.

string|null getTranslatedByMethod(ValueObject $object, string $method, string $forcedLanguage = null)

Gets translated method generic helper.

string|null getTranslationSiteAccess(string $languageCode)

Returns a SiteAccess name for translation in $languageCode.

array getAvailableLanguages()

Returns the list of all available languages, including the ones configured in related SiteAccesses.

Details

at line 48
public __construct(ConfigResolverInterface $configResolver, ContentService $contentService, array $siteAccessesByLanguage, LoggerInterface $logger = null)

Parameters

ConfigResolverInterface $configResolver
ContentService $contentService
array $siteAccessesByLanguage
LoggerInterface $logger

at line 65
public string getTranslatedContentName(Content $content, string $forcedLanguage = null)

Returns content name, translated.

By default this method uses prioritized languages, unless $forcedLanguage is provided.

Parameters

Content $content
string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)

Return Value

string

at line 105
public string getTranslatedContentNameByContentInfo(ContentInfo $contentInfo, string $forcedLanguage = null)

Returns content name, translated, from a ContentInfo object.

By default this method uses prioritized languages, unless $forcedLanguage is provided.

Parameters

ContentInfo $contentInfo
string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)

Return Value

string

at line 128
public Field|null getTranslatedField(Content $content, string $fieldDefIdentifier, string $forcedLanguage = null)

Returns Field object in the appropriate language for a given content.

By default, this method will return the field in current language if translation is present. If not, main language will be used. If $forcedLanguage is provided, will return the field in this language, if translation is present.

Parameters

Content $content
string $fieldDefIdentifier Field definition identifier.
string $forcedLanguage Locale we want the field translation in (e.g. "fre-FR"). Null by default (takes current locale)

Return Value

Field|null

at line 154
public string|null getTranslatedFieldDefinitionProperty(ContentType $contentType, string $fieldDefIdentifier, string $property = 'name', string $forcedLanguage = null)

Returns Field definition name in the appropriate language for a given content.

By default, this method will return the field definition name in current language if translation is present. If not, main language will be used. If $forcedLanguage is provided, will return the field definition name in this language, if translation is present.

Parameters

ContentType $contentType
string $fieldDefIdentifier Field Definition identifier
string $property Specifies if 'name' or 'description' should be used
string $forcedLanguage Locale we want the field definition name translated in in (e.g. "fre-FR"). Null by default (takes current locale)

Return Value

string|null

Exceptions

InvalidArgumentException

at line 198
public string|null getTranslatedByProperty(ValueObject $object, string $property, string $forcedLanguage = null)

Gets translated property generic helper.

For generic use, expects array property as-is on value object, typically $object->$property[$language]

Languages will consist of either forced language or current languages list, in addition helper will check if for mainLanguage property and append that to languages if alwaysAvailable property is true or non-existing.

Parameters

ValueObject $object Can be any kid of Value object which directly holds the translated property
string $property Property name, example 'names', 'descriptions'
string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)

Return Value

string|null

Exceptions

InvalidArgumentException

at line 237
public string|null getTranslatedByMethod(ValueObject $object, string $method, string $forcedLanguage = null)

Gets translated method generic helper.

For generic use, expects method exposing translated property as-is on value object, typically $object->$method($language)

Languages will consist of either forced language or current languages list, in addition helper will append null to list of languages so method may fallback to main/initial language if supported by domain.

Parameters

ValueObject $object Can be any kind of Value object which directly holds the methods that provides translated value.
string $method Method name, example 'getName', 'description'
string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)

Return Value

string|null

Exceptions

InvalidArgumentException

at line 261
public string|null getTranslationSiteAccess(string $languageCode)

Returns a SiteAccess name for translation in $languageCode.

This is used for LanguageSwitcher feature (generate links for current content in a different language if available). Will use configured translation_siteaccesses if any. Otherwise will use related siteaccesses (e.g. same repository, same rootLocationId).

Will return null if no translation SiteAccess can be found.

Parameters

string $languageCode Translation language code.

Return Value

string|null

at line 285
public array getAvailableLanguages()

Returns the list of all available languages, including the ones configured in related SiteAccesses.

Return Value

array