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.
public getTranslatedByMethod ( eZ\Publish\API\Repository\Values\ValueObject $object, string $method, string $forcedLanguage = null ) : string | null | ||
$object | eZ\Publish\API\Repository\Values\ValueObject | Can be any kind of Value object which directly holds the methods that provides translated value. |
$method | string | Method name, example 'getName', 'description' |
$forcedLanguage | string | Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
Résultat | string | null |
/** * Returns tag keyword for provided tag ID or tag object. * * @param mixed|\Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag * * @return string */ public function getTagKeyword($tag) { if (!$tag instanceof Tag) { try { $tag = $this->tagsService->loadTag($tag); } catch (NotFoundException $e) { return ''; } } return $this->translationHelper->getTranslatedByMethod($tag, 'getKeyword'); }
/** * Gets translated property generic helper. * * For generic use, expects property in singular form. For instance if 'name' is provided it will first look for * getName( $lang ) method, then property called ->names[$lang], in either case look for correct translation. * * Languages will consist of either forced language or current SiteAccess languages list, in addition for property * lookup helper will look for mainLanguage property and use it if either alwaysAvailable property is true or non- * existing. * * @param \eZ\Publish\API\Repository\Values\ValueObject $object Can be any kid of Value object which directly holds the translated data * @param string $property Property name, example 'name', 'description' * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If $property does not exists as plural or as method * * @return string|null */ public function getTranslatedProperty(ValueObject $object, $property, $forcedLanguage = null) { $pluralProperty = $property . 's'; if (method_exists($object, 'get' . $property)) { return $this->translationHelper->getTranslatedByMethod($object, 'get' . $property, $forcedLanguage); } elseif (property_exists($object, $pluralProperty) && is_array($object->{$pluralProperty})) { return $this->translationHelper->getTranslatedByProperty($object, $pluralProperty, $forcedLanguage); } throw new InvalidArgumentValue('$property', $property, get_class($object)); }