/** * Returns translation key (placeholder) by entity class name, field name and property code * examples (for default scope which is 'entity'): * [vendor].[bundle].[entity].[field].[config property] * oro.user.group.name.label * * if [entity] == [bundle] -> skip it * oro.user.first_name.label * * if NO fieldName -> add prefix 'entity_' * oro.user.entity_label * oro.user.group.entity_label * examples (for other scopes, for instance 'test'): * [vendor].[bundle].[entity].[field].[scope]_[config property] * oro.user.group.name.test_label * * if [entity] == [bundle] -> skip it * oro.user.first_name.test_label * * if NO fieldName -> add prefix 'entity_' * oro.user.entity_test_label * oro.user.group.entity_test_label * * @param string $scope * @param string $propertyName property key: label, description, plural_label, etc. * @param string $className * @param string $fieldName * * @return string * * @throws \InvalidArgumentException */ public static function getTranslationKey($scope, $propertyName, $className, $fieldName = null) { if (empty($scope)) { throw new \InvalidArgumentException('$scope must not be empty'); } if (empty($propertyName)) { throw new \InvalidArgumentException('$propertyName must not be empty'); } if (empty($className)) { throw new \InvalidArgumentException('$className must not be empty'); } // handle 'entity' scope separately if ($scope === 'entity') { return EntityLabelBuilder::getTranslationKey($propertyName, $className, $fieldName); } $parts = EntityLabelBuilder::explodeClassName($className); $propertyName = Inflector::tableize($scope) . '_' . $propertyName; if ($fieldName) { $parts[] = Inflector::tableize($fieldName); $parts[] = $propertyName; } else { $parts[] = 'entity_' . $propertyName; } return implode('.', $parts); }
/** * {@inheritdoc} * * Parameters: * groupName * entityClass */ public function getLabel(array $parameters) { $label = self::DEFAULT_GROUP === $parameters['groupName'] ? self::DEFAULT_LABEL : sprintf(self::DEFAULT_GROUP_LABEL, $parameters['groupName']); $result = $this->translator->trans($label); if (!empty($parameters['entityClass']) && false !== strpos($result, self::ENTITY_NAME_PLACEHOLDER)) { $entityNameLabel = EntityLabelBuilder::getEntityLabelTranslationKey($parameters['entityClass']); $result = str_replace(self::ENTITY_NAME_PLACEHOLDER, $this->translator->trans($entityNameLabel), $result); } return $result; }
/** * @dataProvider getTranslationKeyProvider */ public function testGetTranslationKey($expected, $propertyName, $className, $fieldName) { $result = EntityLabelBuilder::getTranslationKey($propertyName, $className, $fieldName); $this->assertEquals($expected, $result); }