Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_translations_for_buttons' into 'release/2.3.0'
Browse files Browse the repository at this point in the history
Fix translations for buttons

See merge request metamodels/core!311
  • Loading branch information
zonky2 committed Aug 5, 2024
2 parents bc9362d + 8d6def0 commit 554c786
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 92 deletions.
1 change: 1 addition & 0 deletions .github/workflows/diagnostics.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: MetaModels core

on:
pull_request:
push:
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
"email": "[email protected]",
"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",
Expand Down
80 changes: 32 additions & 48 deletions src/CoreBundle/Contao/Hooks/LoadDataContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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] = [];
}
Expand All @@ -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
{
Expand All @@ -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()"',
Expand All @@ -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
Expand All @@ -242,58 +248,24 @@ 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
*/
private function buildChildOperationButton(
string $idParameter,
string $itemId,
string $href,
string $label,
string $name,
string $transPrefix,
string $icon,
string $attributes,
string $table
Expand All @@ -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(
'<a href="%1$s" title="%2$s"%3$s>%4$s</a> ',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<source>Is default</source>
</trans-unit>
<trans-unit id="isdefault.description" resname="isdefault.description">
<source>Determines that this input screen shall be used as default for the parenting MetaModel.</source>
<source>Determines that this grouping and sorting shall be used as default for the MetaModel.</source>
</trans-unit>
<trans-unit id="rendermode.label" resname="rendermode.label">
<source>Render mode</source>
Expand Down
110 changes: 71 additions & 39 deletions src/CoreBundle/Translator/MetaModelTranslationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
*
Expand Down Expand Up @@ -209,27 +223,45 @@ private function handleInputScreen(
$inputScreen,
$catalog,
$domain,
[],
);
return;
}

if ('ctable' === $inputScreen['meta']['rendertype']) {
$this->handleChildInputScreen(
$inputScreen['meta']['ptable'],
$domain,
$locale,
$mainLanguage,
$inputScreen,
$catalog,
);
}
}

/** @param array<string, string> $parameters The parameters. */
private function setTranslationLabelAndDescription(
string $domain,
string $locale,
string $mainLanguage,
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);
Expand Down

0 comments on commit 554c786

Please sign in to comment.