Skip to content

Commit 554c786

Browse files
committed
Merge branch 'hotfix/fix_translations_for_buttons' into 'release/2.3.0'
Fix translations for buttons See merge request metamodels/core!311
2 parents bc9362d + 8d6def0 commit 554c786

File tree

6 files changed

+110
-92
lines changed

6 files changed

+110
-92
lines changed

.github/workflows/diagnostics.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: MetaModels core
2+
23
on:
34
pull_request:
45
push:

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
"email": "[email protected]",
2727
"issues": "https://github.com/MetaModels/core/issues",
2828
"wiki": "https://de.contaowiki.org/MetaModels",
29-
"irc": "irc://irc.freenode.org/contao.mm",
3029
"source": "https://github.com/MetaModels/core"
3130
},
3231
"require": {
3332
"php": "^8.1",
3433
"ext-dom": "*",
35-
"contao-community-alliance/dc-general": "^2.3.16",
34+
"contao-community-alliance/dc-general": "^2.3.17",
3635
"contao-community-alliance/events-contao-bindings": "^4.13.1",
3736
"contao-community-alliance/meta-palettes": "^2.0.10",
3837
"contao-community-alliance/translator": "^2.4.2",

src/CoreBundle/Contao/Hooks/LoadDataContainer.php

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use MetaModels\IFactory;
3333
use MetaModels\IMetaModel;
3434
use MetaModels\ViewCombination\ViewCombination;
35+
use Symfony\Contracts\Translation\TranslatorInterface;
3536

3637
/**
3738
* This class handles loading of the virtual data containers.
@@ -137,6 +138,8 @@ private function handleMetaModelTable(string $tableName): void
137138
return;
138139
}
139140

141+
$this->controller->loadDataContainer('tl_metamodel_item');
142+
140143
if (!isset($GLOBALS['TL_DCA'][$tableName])) {
141144
$GLOBALS['TL_DCA'][$tableName] = [];
142145
}
@@ -156,6 +159,7 @@ private function handleMetaModelTable(string $tableName): void
156159
*
157160
* @SuppressWarnings(PHPMD.Superglobals)
158161
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
162+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
159163
*/
160164
private function handleNonMetaModelTable(string $tableName): void
161165
{
@@ -176,16 +180,16 @@ private function handleNonMetaModelTable(string $tableName): void
176180

177181
$parentDCA = &$GLOBALS['TL_DCA'][$tableName];
178182

179-
$this->controller->loadLanguageFile('default');
180183
foreach ($map[$tableName] as $metaModelTable => $inputScreen) {
181184
$metaModel = $this->factory->getMetaModel($metaModelTable);
182185
assert($metaModel instanceof IMetaModel);
183186

184-
$caption = $this->buildCaption($metaModel, $inputScreen);
187+
$translationPrefix = 'metamodel_edit_as_child.'
188+
. $metaModel->getTableName()
189+
. '.' . $inputScreen['meta']['id'];
185190

186191
$operationName = 'edit_' . $metaModel->getTableName();
187192
$parentDCA['list']['operations'][$operationName] = [
188-
'label' => &$caption,
189193
'href' => 'table=' . $metaModelTable,
190194
'icon' => $this->iconBuilder->getBackendIcon($inputScreen['meta']['backendicon']),
191195
'attributes' => 'onclick="Backend.getScrollOffset()"',
@@ -211,13 +215,15 @@ function (
211215
string $icon,
212216
string $attributes,
213217
string $table
214-
) use ($idParameter): string {
218+
) use (
219+
$idParameter,
220+
$translationPrefix
221+
): string {
215222
return $this->buildChildOperationButton(
216223
$idParameter,
217224
$row['id'],
218225
$href,
219-
$label,
220-
$name,
226+
$translationPrefix,
221227
$icon,
222228
$attributes,
223229
$table
@@ -242,58 +248,24 @@ private function buildMap(): array
242248
return $map;
243249
}
244250

245-
/**
246-
* Build the caption for a table.
247-
*
248-
* @param IMetaModel $metaModel The MetaModel to build the caption for.
249-
* @param array $inputScreen The input screen information.
250-
*
251-
* @return array
252-
*
253-
* @SuppressWarnings(PHPMD.Superglobals)
254-
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
255-
*/
256-
private function buildCaption(IMetaModel $metaModel, array $inputScreen): array
257-
{
258-
$caption = [
259-
\sprintf($GLOBALS['TL_LANG']['MSC']['metamodel_edit_as_child']['label'], $metaModel->getName()),
260-
''
261-
];
262-
263-
// @deprecated usage of TL_LANGUAGE - remove for Contao 5.0.
264-
$currentLanguage = LocaleUtil::formatAsLocale($GLOBALS['TL_LANGUAGE']);
265-
foreach ($inputScreen['label'] as $langCode => $label) {
266-
if ($label !== '' && $langCode === $currentLanguage) {
267-
$caption = [
268-
$label,
269-
$inputScreen['description'][$langCode]
270-
];
271-
}
272-
}
273-
274-
return $caption;
275-
}
276-
277251
/**
278252
* This method exists only for being compatible when MetaModels are being used as child table from DC_Table context.
279253
*
280254
* @param string $idParameter The id parameter in use.
281-
* @param string $itemId The current data row.
282-
* @param string $href The href to be appended.
283-
* @param string $label The operation label.
284-
* @param string $name The operation name.
285-
* @param string $icon The icon path.
286-
* @param string $attributes The button attributes.
287-
* @param string $table The table name.
255+
* @param string $itemId The current data row.
256+
* @param string $href The href to be appended.
257+
* @param string $transPrefix The operation button label translation prefix.
258+
* @param string $icon The icon path.
259+
* @param string $attributes The button attributes.
260+
* @param string $table The table name.
288261
*
289262
* @return string
290263
*/
291264
private function buildChildOperationButton(
292265
string $idParameter,
293266
string $itemId,
294267
string $href,
295-
string $label,
296-
string $name,
268+
string $transPrefix,
297269
string $icon,
298270
string $attributes,
299271
string $table
@@ -311,7 +283,19 @@ private function buildChildOperationButton(
311283
$url = \preg_replace('#(&)id=(?:&)?#', '$1', $url);
312284
}
313285

314-
$title = \sprintf($label ?: $name, $itemId);
286+
$translator = System::getContainer()->get('translator');
287+
assert($translator instanceof TranslatorInterface);
288+
289+
$label = $translator->trans(
290+
$transPrefix . '.label',
291+
['%id%' => $itemId],
292+
$table
293+
);
294+
$title = $translator->trans(
295+
$transPrefix . '.description',
296+
['%id%' => $itemId],
297+
$table
298+
);
315299

316300
return \sprintf(
317301
'<a href="%1$s" title="%2$s"%3$s>%4$s</a> ',

src/CoreBundle/EventListener/DcGeneral/DefinitionBuilder/CommandBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,16 @@ private function parseModelOperations(Contao2BackendViewDefinitionInterface $vie
233233

234234
// Check if we have some children.
235235
foreach ($this->viewCombination->getChildrenOf($this->container->getName()) as $tableName => $screen) {
236+
$screenId = (string) ($screen['meta']['id'] ?? '');
237+
assert('' !== $screenId);
236238
$this->createCommand(
237239
$collection,
238240
'edit_' . $tableName,
239241
['table' => $tableName],
240242
$this->iconBuilder->getBackendIcon($screen['meta']['backendicon']),
241243
[
242-
'label' => 'metamodel_edit_as_child.' . $tableName . '.label',
243-
'description' => 'metamodel_edit_as_child.' . $tableName . '.description',
244+
'label' => 'metamodel_edit_as_child.' . $tableName . '.' . $screenId . '.label',
245+
'description' => 'metamodel_edit_as_child.' . $tableName . '.' . $screenId . '.description',
244246
'idparam' => 'pid'
245247
]
246248
);

src/CoreBundle/Resources/translations/tl_metamodel_dca_sortgroup.en.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<source>Is default</source>
7777
</trans-unit>
7878
<trans-unit id="isdefault.description" resname="isdefault.description">
79-
<source>Determines that this input screen shall be used as default for the parenting MetaModel.</source>
79+
<source>Determines that this grouping and sorting shall be used as default for the MetaModel.</source>
8080
</trans-unit>
8181
<trans-unit id="rendermode.label" resname="rendermode.label">
8282
<source>Render mode</source>

src/CoreBundle/Translator/MetaModelTranslationLoader.php

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -124,43 +124,10 @@ public function load($resource, string $locale, string $domain = 'messages'): Me
124124
}
125125

126126
// Check if we have some children - add child button translations then.
127-
foreach ($this->viewCombination->getChildrenOf($domain) as $tableName => $inputScreen) {
128-
$subMetaModel = $this->factory->getMetaModel($tableName);
129-
if (null === $subMetaModel) {
130-
continue;
131-
}
132-
$translationKey = 'metamodel_edit_as_child.' . $subMetaModel->getTableName();
133-
134-
if (
135-
'metamodel_edit_as_child.label' !==
136-
$baseValue = $catalog->get('metamodel_edit_as_child.label', $domain)
137-
) {
138-
$catalog->set(
139-
$translationKey . '.label',
140-
strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]),
141-
$domain,
142-
);
127+
foreach (\array_keys($this->viewCombination->getChildrenOf($domain)) as $tableName) {
128+
foreach ($this->builder->fetchAllInputScreensForTable($tableName) as $inputScreen) {
129+
$this->handleChildInputScreen($domain, $tableName, $locale, $mainLanguage, $inputScreen, $catalog);
143130
}
144-
if (
145-
'metamodel_edit_as_child.description' !==
146-
$baseValue = $catalog->get('metamodel_edit_as_child.description', $domain)
147-
) {
148-
$catalog->set(
149-
$translationKey . '.description',
150-
strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]),
151-
$domain,
152-
);
153-
}
154-
155-
$this->setTranslationLabelAndDescription(
156-
$domain,
157-
$locale,
158-
$mainLanguage,
159-
$translationKey,
160-
$inputScreen,
161-
$catalog,
162-
$tableName,
163-
);
164131
}
165132

166133
foreach ($this->loaders as $loader) {
@@ -172,6 +139,53 @@ public function load($resource, string $locale, string $domain = 'messages'): Me
172139
return $catalog;
173140
}
174141

142+
private function handleChildInputScreen(
143+
string $domain,
144+
string $tableName,
145+
string $locale,
146+
string $mainLanguage,
147+
array $inputScreen,
148+
MessageCatalogue $catalog
149+
): void {
150+
$subMetaModel = $this->factory->getMetaModel($tableName);
151+
if (null === $subMetaModel) {
152+
return;
153+
}
154+
$translationKey = 'metamodel_edit_as_child.' . $tableName . '.' . $inputScreen['meta']['id'];
155+
156+
if (
157+
'metamodel_edit_as_child.label' !==
158+
$baseValue = $catalog->get('metamodel_edit_as_child.label', $domain)
159+
) {
160+
$catalog->set(
161+
$translationKey . '.label',
162+
strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]),
163+
$domain,
164+
);
165+
}
166+
if (
167+
'metamodel_edit_as_child.description' !==
168+
$baseValue = $catalog->get('metamodel_edit_as_child.description', $domain)
169+
) {
170+
$catalog->set(
171+
$translationKey . '.description',
172+
strtr($baseValue, ['%child_name%' => $subMetaModel->getName()]),
173+
$domain,
174+
);
175+
}
176+
177+
$this->setTranslationLabelAndDescription(
178+
$domain,
179+
$locale,
180+
$mainLanguage,
181+
$translationKey,
182+
$inputScreen,
183+
$catalog,
184+
$tableName,
185+
['%child_name%' => $subMetaModel->getName()]
186+
);
187+
}
188+
175189
/**
176190
* Handle input screen.
177191
*
@@ -209,27 +223,45 @@ private function handleInputScreen(
209223
$inputScreen,
210224
$catalog,
211225
$domain,
226+
[],
227+
);
228+
return;
229+
}
230+
231+
if ('ctable' === $inputScreen['meta']['rendertype']) {
232+
$this->handleChildInputScreen(
233+
$inputScreen['meta']['ptable'],
234+
$domain,
235+
$locale,
236+
$mainLanguage,
237+
$inputScreen,
238+
$catalog,
212239
);
213240
}
214241
}
215242

243+
/** @param array<string, string> $parameters The parameters. */
216244
private function setTranslationLabelAndDescription(
217245
string $domain,
218246
string $locale,
219247
string $mainLanguage,
220248
string $prefix,
221249
array $inputScreen,
222250
MessageCatalogue $catalog,
223-
string $headlineDomain
251+
string $headlineDomain,
252+
array $parameters,
224253
): void {
225254
$headlineKey = 'backend-module.' . $inputScreen['meta']['id'] . '.headline';
226-
$catalog->set($prefix . '.description', '', $domain);
227255
if ('' !== $value = $this->extractLangString($inputScreen['description'], $locale, $mainLanguage) ?? '') {
256+
$value = strtr($value, $parameters);
228257
$catalog->set($prefix . '.description', $value, $domain);
229258

230-
$catalog->set($headlineKey, $value, $headlineDomain);
259+
if ($headlineKey === $catalog->get($headlineKey, $headlineDomain)) {
260+
$catalog->set($headlineKey, $value, $headlineDomain);
261+
}
231262
}
232263
if ('' !== $value = $this->extractLangString($inputScreen['label'], $locale, $mainLanguage) ?? '') {
264+
$value = strtr($value, $parameters);
233265
$catalog->set($prefix . '.label', $value, $domain);
234266
if ($headlineKey === $catalog->get($headlineKey, $headlineDomain)) {
235267
$catalog->set($headlineKey, $value, $headlineDomain);

0 commit comments

Comments
 (0)