From 8d6def03b0da482749a8cd93a379d186cbec083c Mon Sep 17 00:00:00 2001 From: Ingolf Steinardt Date: Mon, 5 Aug 2024 22:09:42 +0200 Subject: [PATCH] Fix translations for buttons --- .github/workflows/diagnostics.yml | 1 + composer.json | 3 +- .../Contao/Hooks/LoadDataContainer.php | 80 +++++-------- .../DefinitionBuilder/CommandBuilder.php | 6 +- .../tl_metamodel_dca_sortgroup.en.xlf | 2 +- .../Translator/MetaModelTranslationLoader.php | 110 +++++++++++------- 6 files changed, 110 insertions(+), 92 deletions(-) diff --git a/.github/workflows/diagnostics.yml b/.github/workflows/diagnostics.yml index dd22e9133..0fbc0d314 100644 --- a/.github/workflows/diagnostics.yml +++ b/.github/workflows/diagnostics.yml @@ -1,4 +1,5 @@ name: MetaModels core + on: pull_request: push: diff --git a/composer.json b/composer.json index 3ba031772..dd5fb4331 100644 --- a/composer.json +++ b/composer.json @@ -26,13 +26,12 @@ "email": "mail@metamodel.me", "issues": "https://github.com/MetaModels/core/issues", "wiki": "https://de.contaowiki.org/MetaModels", - "irc": "irc://irc.freenode.org/contao.mm", "source": "https://github.com/MetaModels/core" }, "require": { "php": "^8.1", "ext-dom": "*", - "contao-community-alliance/dc-general": "^2.3.16", + "contao-community-alliance/dc-general": "^2.3.17", "contao-community-alliance/events-contao-bindings": "^4.13.1", "contao-community-alliance/meta-palettes": "^2.0.10", "contao-community-alliance/translator": "^2.4.2", diff --git a/src/CoreBundle/Contao/Hooks/LoadDataContainer.php b/src/CoreBundle/Contao/Hooks/LoadDataContainer.php index 64df899e6..f196b791e 100644 --- a/src/CoreBundle/Contao/Hooks/LoadDataContainer.php +++ b/src/CoreBundle/Contao/Hooks/LoadDataContainer.php @@ -32,6 +32,7 @@ use MetaModels\IFactory; use MetaModels\IMetaModel; use MetaModels\ViewCombination\ViewCombination; +use Symfony\Contracts\Translation\TranslatorInterface; /** * This class handles loading of the virtual data containers. @@ -137,6 +138,8 @@ private function handleMetaModelTable(string $tableName): void return; } + $this->controller->loadDataContainer('tl_metamodel_item'); + if (!isset($GLOBALS['TL_DCA'][$tableName])) { $GLOBALS['TL_DCA'][$tableName] = []; } @@ -156,6 +159,7 @@ private function handleMetaModelTable(string $tableName): void * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ private function handleNonMetaModelTable(string $tableName): void { @@ -176,16 +180,16 @@ private function handleNonMetaModelTable(string $tableName): void $parentDCA = &$GLOBALS['TL_DCA'][$tableName]; - $this->controller->loadLanguageFile('default'); foreach ($map[$tableName] as $metaModelTable => $inputScreen) { $metaModel = $this->factory->getMetaModel($metaModelTable); assert($metaModel instanceof IMetaModel); - $caption = $this->buildCaption($metaModel, $inputScreen); + $translationPrefix = 'metamodel_edit_as_child.' + . $metaModel->getTableName() + . '.' . $inputScreen['meta']['id']; $operationName = 'edit_' . $metaModel->getTableName(); $parentDCA['list']['operations'][$operationName] = [ - 'label' => &$caption, 'href' => 'table=' . $metaModelTable, 'icon' => $this->iconBuilder->getBackendIcon($inputScreen['meta']['backendicon']), 'attributes' => 'onclick="Backend.getScrollOffset()"', @@ -211,13 +215,15 @@ function ( string $icon, string $attributes, string $table - ) use ($idParameter): string { + ) use ( + $idParameter, + $translationPrefix + ): string { return $this->buildChildOperationButton( $idParameter, $row['id'], $href, - $label, - $name, + $translationPrefix, $icon, $attributes, $table @@ -242,49 +248,16 @@ private function buildMap(): array return $map; } - /** - * Build the caption for a table. - * - * @param IMetaModel $metaModel The MetaModel to build the caption for. - * @param array $inputScreen The input screen information. - * - * @return array - * - * @SuppressWarnings(PHPMD.Superglobals) - * @SuppressWarnings(PHPMD.CamelCaseVariableName) - */ - private function buildCaption(IMetaModel $metaModel, array $inputScreen): array - { - $caption = [ - \sprintf($GLOBALS['TL_LANG']['MSC']['metamodel_edit_as_child']['label'], $metaModel->getName()), - '' - ]; - - // @deprecated usage of TL_LANGUAGE - remove for Contao 5.0. - $currentLanguage = LocaleUtil::formatAsLocale($GLOBALS['TL_LANGUAGE']); - foreach ($inputScreen['label'] as $langCode => $label) { - if ($label !== '' && $langCode === $currentLanguage) { - $caption = [ - $label, - $inputScreen['description'][$langCode] - ]; - } - } - - return $caption; - } - /** * This method exists only for being compatible when MetaModels are being used as child table from DC_Table context. * * @param string $idParameter The id parameter in use. - * @param string $itemId The current data row. - * @param string $href The href to be appended. - * @param string $label The operation label. - * @param string $name The operation name. - * @param string $icon The icon path. - * @param string $attributes The button attributes. - * @param string $table The table name. + * @param string $itemId The current data row. + * @param string $href The href to be appended. + * @param string $transPrefix The operation button label translation prefix. + * @param string $icon The icon path. + * @param string $attributes The button attributes. + * @param string $table The table name. * * @return string */ @@ -292,8 +265,7 @@ private function buildChildOperationButton( string $idParameter, string $itemId, string $href, - string $label, - string $name, + string $transPrefix, string $icon, string $attributes, string $table @@ -311,7 +283,19 @@ private function buildChildOperationButton( $url = \preg_replace('#(&)id=(?:&)?#', '$1', $url); } - $title = \sprintf($label ?: $name, $itemId); + $translator = System::getContainer()->get('translator'); + assert($translator instanceof TranslatorInterface); + + $label = $translator->trans( + $transPrefix . '.label', + ['%id%' => $itemId], + $table + ); + $title = $translator->trans( + $transPrefix . '.description', + ['%id%' => $itemId], + $table + ); return \sprintf( '%4$s ', diff --git a/src/CoreBundle/EventListener/DcGeneral/DefinitionBuilder/CommandBuilder.php b/src/CoreBundle/EventListener/DcGeneral/DefinitionBuilder/CommandBuilder.php index 628736d88..2dbddfa27 100644 --- a/src/CoreBundle/EventListener/DcGeneral/DefinitionBuilder/CommandBuilder.php +++ b/src/CoreBundle/EventListener/DcGeneral/DefinitionBuilder/CommandBuilder.php @@ -233,14 +233,16 @@ private function parseModelOperations(Contao2BackendViewDefinitionInterface $vie // Check if we have some children. foreach ($this->viewCombination->getChildrenOf($this->container->getName()) as $tableName => $screen) { + $screenId = (string) ($screen['meta']['id'] ?? ''); + assert('' !== $screenId); $this->createCommand( $collection, 'edit_' . $tableName, ['table' => $tableName], $this->iconBuilder->getBackendIcon($screen['meta']['backendicon']), [ - 'label' => 'metamodel_edit_as_child.' . $tableName . '.label', - 'description' => 'metamodel_edit_as_child.' . $tableName . '.description', + 'label' => 'metamodel_edit_as_child.' . $tableName . '.' . $screenId . '.label', + 'description' => 'metamodel_edit_as_child.' . $tableName . '.' . $screenId . '.description', 'idparam' => 'pid' ] ); diff --git a/src/CoreBundle/Resources/translations/tl_metamodel_dca_sortgroup.en.xlf b/src/CoreBundle/Resources/translations/tl_metamodel_dca_sortgroup.en.xlf index 77d4b05fe..dab68c5c4 100644 --- a/src/CoreBundle/Resources/translations/tl_metamodel_dca_sortgroup.en.xlf +++ b/src/CoreBundle/Resources/translations/tl_metamodel_dca_sortgroup.en.xlf @@ -76,7 +76,7 @@ Is default - Determines that this input screen shall be used as default for the parenting MetaModel. + Determines that this grouping and sorting shall be used as default for the MetaModel. Render mode diff --git a/src/CoreBundle/Translator/MetaModelTranslationLoader.php b/src/CoreBundle/Translator/MetaModelTranslationLoader.php index 7e3256731..488f775b8 100644 --- a/src/CoreBundle/Translator/MetaModelTranslationLoader.php +++ b/src/CoreBundle/Translator/MetaModelTranslationLoader.php @@ -124,43 +124,10 @@ public function load($resource, string $locale, string $domain = 'messages'): Me } // Check if we have some children - add child button translations then. - foreach ($this->viewCombination->getChildrenOf($domain) as $tableName => $inputScreen) { - $subMetaModel = $this->factory->getMetaModel($tableName); - if (null === $subMetaModel) { - continue; - } - $translationKey = 'metamodel_edit_as_child.' . $subMetaModel->getTableName(); - - if ( - 'metamodel_edit_as_child.label' !== - $baseValue = $catalog->get('metamodel_edit_as_child.label', $domain) - ) { - $catalog->set( - $translationKey . '.label', - strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]), - $domain, - ); + foreach (\array_keys($this->viewCombination->getChildrenOf($domain)) as $tableName) { + foreach ($this->builder->fetchAllInputScreensForTable($tableName) as $inputScreen) { + $this->handleChildInputScreen($domain, $tableName, $locale, $mainLanguage, $inputScreen, $catalog); } - if ( - 'metamodel_edit_as_child.description' !== - $baseValue = $catalog->get('metamodel_edit_as_child.description', $domain) - ) { - $catalog->set( - $translationKey . '.description', - strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]), - $domain, - ); - } - - $this->setTranslationLabelAndDescription( - $domain, - $locale, - $mainLanguage, - $translationKey, - $inputScreen, - $catalog, - $tableName, - ); } foreach ($this->loaders as $loader) { @@ -172,6 +139,53 @@ public function load($resource, string $locale, string $domain = 'messages'): Me return $catalog; } + private function handleChildInputScreen( + string $domain, + string $tableName, + string $locale, + string $mainLanguage, + array $inputScreen, + MessageCatalogue $catalog + ): void { + $subMetaModel = $this->factory->getMetaModel($tableName); + if (null === $subMetaModel) { + return; + } + $translationKey = 'metamodel_edit_as_child.' . $tableName . '.' . $inputScreen['meta']['id']; + + if ( + 'metamodel_edit_as_child.label' !== + $baseValue = $catalog->get('metamodel_edit_as_child.label', $domain) + ) { + $catalog->set( + $translationKey . '.label', + strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]), + $domain, + ); + } + if ( + 'metamodel_edit_as_child.description' !== + $baseValue = $catalog->get('metamodel_edit_as_child.description', $domain) + ) { + $catalog->set( + $translationKey . '.description', + strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]), + $domain, + ); + } + + $this->setTranslationLabelAndDescription( + $domain, + $locale, + $mainLanguage, + $translationKey, + $inputScreen, + $catalog, + $tableName, + ['%child_name%' => $subMetaModel->getName()] + ); + } + /** * Handle input screen. * @@ -209,10 +223,24 @@ private function handleInputScreen( $inputScreen, $catalog, $domain, + [], + ); + return; + } + + if ('ctable' === $inputScreen['meta']['rendertype']) { + $this->handleChildInputScreen( + $inputScreen['meta']['ptable'], + $domain, + $locale, + $mainLanguage, + $inputScreen, + $catalog, ); } } + /** @param array $parameters The parameters. */ private function setTranslationLabelAndDescription( string $domain, string $locale, @@ -220,16 +248,20 @@ private function setTranslationLabelAndDescription( string $prefix, array $inputScreen, MessageCatalogue $catalog, - string $headlineDomain + string $headlineDomain, + array $parameters, ): void { $headlineKey = 'backend-module.' . $inputScreen['meta']['id'] . '.headline'; - $catalog->set($prefix . '.description', '', $domain); if ('' !== $value = $this->extractLangString($inputScreen['description'], $locale, $mainLanguage) ?? '') { + $value = strtr($value, $parameters); $catalog->set($prefix . '.description', $value, $domain); - $catalog->set($headlineKey, $value, $headlineDomain); + if ($headlineKey === $catalog->get($headlineKey, $headlineDomain)) { + $catalog->set($headlineKey, $value, $headlineDomain); + } } if ('' !== $value = $this->extractLangString($inputScreen['label'], $locale, $mainLanguage) ?? '') { + $value = strtr($value, $parameters); $catalog->set($prefix . '.label', $value, $domain); if ($headlineKey === $catalog->get($headlineKey, $headlineDomain)) { $catalog->set($headlineKey, $value, $headlineDomain);