translator = $translator; } public function transAction($action) { return $this->trans('action.' . $action); } public function transMenu($menu) { return $this->trans('menu.' . $menu); } public function transFlashMessage($name, $params = []) { return $this->trans('flash_message.' . $name, $params); } public function transField($fieldName, $entityClass): string { return $this->transEntityThenDefault( $this->buildTransIdField($fieldName, $entityClass), $this->buildTransIdField($fieldName, $entityClass, true) ); } public function transChoices(array $choices, string $entityName,string $field): array { $newChoices = []; foreach ($choices as $key => $choice) { $newChoices[$this->transField($field.'Choices.'.$choice, $entityName)] = $choice; } return $newChoices; } public function transHelp($fieldName, $entityClass): string { $fieldName = $fieldName . '_help'; return $this->transEntityThenDefault( $this->buildTransIdField($fieldName, $entityClass), $this->buildTransIdField($fieldName, $entityClass, true), true ); } public function transPanel($panelName, $entityClass): string { return $this->transEntityThenDefault( $this->buildTransIdPanel($panelName, $entityClass), $this->buildTransIdPanel($panelName, $entityClass, true) ); } public function transTitle($pageName, $entityClass = null, $params = []) { $entityName = $this->getEntityName($entityClass); $paramsTranslation = []; if ($entityName) { $baseIdEntityLabel = 'entity.' . $entityName; $paramsTranslation = [ '%label%' => $this->trans($baseIdEntityLabel . '.label'), '%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'), ]; } if (isset($params['id'])) { $paramsTranslation['%id%'] = $params['id']; } return $this->trans( 'title.' . $pageName, $paramsTranslation ); } private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false): string { $translation = $this->trans($idTranslationEntity); if ($translation == $idTranslationEntity) { $translation = $this->trans($idTranslationDefault); if ($translation == $idTranslationDefault) { if ($returnEmpty) { $translation = ''; } else { $translation = $idTranslationEntity; } } } return $translation; } private function buildTransIdField($fieldName, $entityClass, $default = false): string { return $this->buildTransIdEntitySection($fieldName, $entityClass, 'fields', $default); } private function buildTransIdPanel($panelName, $entityClass, $default = false): string { return $this->buildTransIdEntitySection($panelName, $entityClass, 'panels', $default); } private function buildTransIdEntitySection($name, $entityClass, $entitySection, $default): string { if ($default) { $entityName = 'default'; } else { $entityName = $this->getEntityName($entityClass); } return 'entity.' . $entityName . '.' . $entitySection . '.' . $name; } public function trans($id, $params = [], $domain = self::DOMAIN): string { return $this->translator->trans($id, $params, $domain); } private function getEntityName($entityClass): string { if (is_object($entityClass)) { $entityClass = get_class($entityClass); } if (is_string($entityClass)) { $entityNameExplode = explode('\\', $entityClass); return $entityNameExplode[count($entityNameExplode) - 1]; } return 'default'; } } ?>