Skip to content

Commit 388a79c

Browse files
committed
Merge branch 'hotfix/translation_loader' into 'feature/2.3.0'
Changes for translation loader See merge request metamodels/attribute_checkbox!43
2 parents 3d197e8 + f08d264 commit 388a79c

File tree

10 files changed

+221
-47
lines changed

10 files changed

+221
-47
lines changed

.tx/config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[main]
22
host = https://app.transifex.com
33

4-
[o:metamodels:p:attribute-checkbox:r:metamodels_default]
5-
file_filter = src/Resources/translations/metamodels_default.<lang>.xlf
6-
source_file = src/Resources/translations/metamodels_default.en.xlf
4+
[o:metamodels:p:attribute-checkbox:r:metamodel_checkbox]
5+
file_filter = src/Resources/translations/metamodel_checkbox.<lang>.xlf
6+
source_file = src/Resources/translations/metamodel_checkbox.en.xlf
77
type = XLIFF
88
minimum_perc = 0
9-
resource_name = metamodels_default
9+
resource_name = metamodel_checkbox
1010
replace_edited_strings = false
1111
keep_translations = false
1212

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"contao/core-bundle": "^4.13.0 <5.0",
3838
"metamodels/core": "^2.3.0@dev",
3939
"symfony/dependency-injection": "^5.4",
40-
"symfony/http-kernel": "^5.4"
40+
"symfony/http-kernel": "^5.4",
41+
"symfony/translation": "^5.4"
4142
},
4243
"require-dev": {
4344
"contao/manager-plugin": "^2.1",

src/Attribute/Checkbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getFilterOptions($idList, $usedOnly, &$arrCount = null)
123123
$mapped = [];
124124
foreach ($values as $value) {
125125
$mapped[(string) $value] =
126-
$translator->trans('value_' . ($value ?: '0'), [], 'metamodels_default');
126+
$translator->trans('value_' . ($value ?: '0'), [], 'metamodel_checkbox');
127127
}
128128

129129
return $mapped;

src/EventListener/BuildMetaModelOperationsListener.php

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace MetaModels\AttributeCheckboxBundle\EventListener;
2525

2626
use Contao\FilesModel;
27-
use Contao\System;
2827
use ContaoCommunityAlliance\DcGeneral\Contao\DataDefinition\Definition\Contao2BackendViewDefinition;
2928
use ContaoCommunityAlliance\DcGeneral\Contao\DataDefinition\Definition\Contao2BackendViewDefinitionInterface;
3029
use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator;
@@ -36,7 +35,6 @@
3635
use MetaModels\AttributeCheckboxBundle\Attribute\Checkbox;
3736
use MetaModels\CoreBundle\Assets\IconBuilder;
3837
use MetaModels\DcGeneral\Events\MetaModel\BuildMetaModelOperationsEvent;
39-
use Symfony\Contracts\Translation\TranslatorInterface;
4038

4139
/**
4240
* This class creates the default instances for property conditions when generating input screens.
@@ -78,40 +76,31 @@ public function __construct(RequestScopeDeterminator $scopeMatcher, IconBuilder
7876
* @param array $propertyData The property date from the input screen property.
7977
*
8078
* @return ToggleCommandInterface
81-
*
82-
* @SuppressWarnings(PHPMD.Superglobals)
83-
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
84-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8579
*/
86-
private function buildCommand(Checkbox $attribute, array $propertyData)
80+
private function buildCommand(Checkbox $attribute, array $propertyData, string $prefix)
8781
{
88-
if ($attribute->get('check_listview') === 1) {
82+
if ($attribute->get('check_listview') === '1') {
8983
$commandName = 'listviewtoggle_' . $attribute->getColName();
9084
} else {
9185
$commandName = 'publishtoggle_' . $attribute->getColName();
9286
}
9387

94-
$translator = System::getContainer()->get('translator');
95-
assert($translator instanceof TranslatorInterface);
96-
9788
$toggle = new ToggleCommand();
9889
$toggle->setName($commandName);
99-
$toggle->setLabel($translator->trans('toggle.label', [], 'metamodels_default'));
100-
$toggle->setDescription(
101-
$translator->trans('toggle.description', ['name' => $attribute->getName()], 'metamodels_default')
102-
);
103-
104-
$extra = $toggle->getExtra();
105-
$extra['icon'] = 'visible.svg';
106-
$objIconEnabled = FilesModel::findByUuid($attribute->get('check_listviewicon'));
107-
$objIconDisabled = FilesModel::findByUuid($attribute->get('check_listviewicondisabled'));
108-
109-
if ($attribute->get('check_listview') === '1' && null !== $objIconEnabled && $objIconEnabled->path) {
110-
$extra['icon'] = $this->iconBuilder->getBackendIcon($objIconEnabled->path);
111-
}
112-
113-
if ($attribute->get('check_listview') === '1' && null !== $objIconDisabled && $objIconDisabled->path) {
114-
$extra['icon_disabled'] = $this->iconBuilder->getBackendIcon($objIconDisabled->path);
90+
$toggle->setLabel($prefix . $commandName . '.label');
91+
$toggle->setDescription($prefix . $commandName . '.description');
92+
93+
$extra = $toggle->getExtra();
94+
$extra['icon'] = 'visible.svg';
95+
if ($attribute->get('check_listview') === '1') {
96+
$objIconEnabled = FilesModel::findByUuid($attribute->get('check_listviewicon'));
97+
$objIconDisabled = FilesModel::findByUuid($attribute->get('check_listviewicondisabled'));
98+
if (null !== $objIconEnabled && $objIconEnabled->path) {
99+
$extra['icon'] = $this->iconBuilder->getBackendIcon($objIconEnabled->path);
100+
}
101+
if (null !== $objIconDisabled && $objIconDisabled->path) {
102+
$extra['icon_disabled'] = $this->iconBuilder->getBackendIcon($objIconDisabled->path);
103+
}
115104
}
116105

117106
$toggle->setToggleProperty($attribute->getColName());
@@ -162,13 +151,15 @@ public function handle(BuildMetaModelOperationsEvent $event)
162151
if (!$this->scopeMatcher->currentScopeIsBackend()) {
163152
return;
164153
}
165-
$allProps = $event->getScreen()['properties'];
166-
$properties = \array_map(
154+
$inputScreen = $event->getScreen();
155+
$allProps = $inputScreen['properties'];
156+
$properties = \array_map(
167157
static function (array $property) {
168158
return ($property['col_name'] ?? null);
169159
},
170160
$allProps
171161
);
162+
$prefix = 'inputscreen.' . $inputScreen['meta']['id'] . '.';
172163
foreach ($event->getMetaModel()->getAttributes() as $attribute) {
173164
if (!$this->wantToAdd($attribute, $properties)) {
174165
continue;
@@ -181,7 +172,7 @@ static function (array $property) {
181172
}
182173
}
183174
/** @var Checkbox $attribute */
184-
$toggle = $this->buildCommand($attribute, $info);
175+
$toggle = $this->buildCommand($attribute, $info, $prefix);
185176
$container = $event->getContainer();
186177
$view = $this->createBackendViewDefinition($container);
187178

src/Resources/config/services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ services:
1919
MetaModels\AttributeCheckboxBundle\Schema\DoctrineSchemaGenerator:
2020
tags:
2121
- { name: 'metamodels.schema-generator.doctrine' }
22+
23+
MetaModels\AttributeCheckboxBundle\Translator\TranslationLoader:
24+
arguments:
25+
$baseTranslator: '@contao.translation.translator'
26+
$builder: '@metamodels.view_combination.input_screen_information_builder'
27+
tags: [ { name: metamodels.translation-loader } ]

src/Resources/translations/metamodels_default.de.xlf renamed to src/Resources/translations/metamodel_checkbox.de.xlf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" ?><xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
2-
<file source-language="en" datatype="plaintext" original="src/Resources/translations/metamodels_default.en.xlf" target-language="de">
2+
<file source-language="en" datatype="plaintext" original="src/Resources/translations/metamodel_checkbox.en.xlf" target-language="de">
33
<body>
44
<trans-unit id="toggle.label" resname="toggle.label">
55
<source>Toggle</source>
66
<target>Wechseln</target>
77
</trans-unit>
88
<trans-unit id="toggle.description" resname="toggle.description">
9-
<source>Toggle the state of attribute %id% for this item</source>
10-
<target>Wechselt den Status des Attributs %id% für diesen Datensatz.</target>
9+
<source>Toggle the state of attribute &quot;%name%&quot; for item %id%</source>
10+
<target>Wechselt den Status des Attributs &quot;%name%&quot; für Datensatz %id%</target>
1111
</trans-unit>
1212
<trans-unit id="checkbox.value_0" resname="checkbox.value_0">
1313
<source>Inactive</source>

src/Resources/translations/metamodels_default.en.xlf renamed to src/Resources/translations/metamodel_checkbox.en.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
3-
<file source-language="en" datatype="plaintext" original="src/Resources/translations/metamodels_default.en.xlf">
3+
<file source-language="en" datatype="plaintext" original="src/Resources/translations/metamodel_checkbox.en.xlf">
44
<body>
55
<trans-unit id="toggle.label" resname="toggle.label">
66
<source>Toggle</source>
77
</trans-unit>
88
<trans-unit id="toggle.description" resname="toggle.description">
9-
<source>Toggle the state of attribute %id% for this item</source>
9+
<source>Toggle the state of attribute "%name%" for item %id%</source>
1010
</trans-unit>
1111
<trans-unit id="checkbox.value_0" resname="checkbox.value_0">
1212
<source>Inactive</source>

src/Resources/translations/tl_metamodel_filtersetting.de.xlf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<file source-language="en" datatype="plaintext" original="src/Resources/translations/tl_metamodel_filtersetting.en.xlf" target-language="de">
33
<body>
44
<trans-unit id="typenames.checkbox_published" resname="typenames.checkbox_published">
5-
<source>Published state</source>
6-
<target>Veröffentlichungsstatus</target>
5+
<source>Checkbox state</source>
6+
<target>Checkbox-Status</target>
77
</trans-unit>
88
<trans-unit id="check_ignorepublished.label" resname="check_ignorepublished.label">
99
<source>Allow parameter override</source>
@@ -18,8 +18,8 @@
1818
<target>Filter nicht in Frontendvorschau nutzen</target>
1919
</trans-unit>
2020
<trans-unit id="check_allowpreview.description" resname="check_allowpreview.description">
21-
<source>If you check this, this filter will not get applied when an user is in &quot;Front end preview&quot; and has the option &quot;show unpublished items&quot; active.</source>
22-
<target>Falls ausgewählt, wird dieser Filter nicht benutzt, wenn ein Benutzer die Frontend-Vorschau nutzt. Die Option &quot;Unveröffentlichte Elemente anzeigen&quot; wird nicht ignoriert.</target>
21+
<source>If you check this, this filter will not get applied when a user is in &quot;Front end preview&quot; and has the option &quot;show unpublished items&quot; active.</source>
22+
<target>Falls ausgewählt, wird dieser Filter übergangen, wenn ein Benutzer die Frontend-Vorschau nutzt. Die Option &quot;Unveröffentlichte Elemente anzeigen&quot; wird ignoriert.</target>
2323
</trans-unit>
2424
<trans-unit id="ignore_published.label" resname="ignore_published.label">
2525
<source>Ignore checkbox field %name%</source>

src/Resources/translations/tl_metamodel_filtersetting.en.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<file source-language="en" datatype="plaintext" original="src/Resources/translations/tl_metamodel_filtersetting.en.xlf">
44
<body>
55
<trans-unit id="typenames.checkbox_published" resname="typenames.checkbox_published">
6-
<source>Published state</source>
6+
<source>Checkbox state</source>
77
</trans-unit>
88
<trans-unit id="check_ignorepublished.label" resname="check_ignorepublished.label">
99
<source>Allow parameter override</source>
@@ -15,7 +15,7 @@
1515
<source>Ignore filter in preview mode</source>
1616
</trans-unit>
1717
<trans-unit id="check_allowpreview.description" resname="check_allowpreview.description">
18-
<source>If you check this, this filter will not get applied when an user is in &quot;Front end preview&quot; and has the option &quot;show unpublished items&quot; active.</source>
18+
<source>If you check this, this filter will not get applied when a user is in &quot;Front end preview&quot; and has the option &quot;show unpublished items&quot; active.</source>
1919
</trans-unit>
2020
<trans-unit id="ignore_published.label" resname="ignore_published.label">
2121
<source>Ignore checkbox field %name%</source>
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
/**
4+
* This file is part of MetaModels/attribute_checkbox.
5+
*
6+
* (c) 2012-2024 The MetaModels team.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* This project is provided in good faith and hope to be usable by anyone.
12+
*
13+
* @package MetaModels/attribute_checkbox
14+
* @author Ingolf Steinhardt <info@e-spin.de>
15+
* @copyright 2012-2024 The MetaModels team.
16+
* @license https://github.com/MetaModels/attribute_checkbox/blob/master/LICENSE LGPL-3.0-or-later
17+
* @filesource
18+
*/
19+
20+
declare(strict_types=1);
21+
22+
namespace MetaModels\AttributeCheckboxBundle\Translator;
23+
24+
use MetaModels\Attribute\IAttribute;
25+
use MetaModels\AttributeCheckboxBundle\Attribute\Checkbox;
26+
use MetaModels\CoreBundle\Translator\LoaderInterface;
27+
use MetaModels\IMetaModel;
28+
use MetaModels\ITranslatedMetaModel;
29+
use MetaModels\ViewCombination\InputScreenInformationBuilder;
30+
use Symfony\Component\Translation\MessageCatalogue;
31+
use Symfony\Component\Translation\TranslatorBagInterface;
32+
33+
use function array_map;
34+
use function in_array;
35+
use function is_array;
36+
37+
final class TranslationLoader implements LoaderInterface
38+
{
39+
public function __construct(
40+
private readonly TranslatorBagInterface $baseTranslator,
41+
private readonly InputScreenInformationBuilder $builder,
42+
) {
43+
}
44+
45+
public function load(IMetaModel $metaModel, string $locale): MessageCatalogue
46+
{
47+
/**
48+
* @psalm-suppress DeprecatedMethod
49+
* @psalm-suppress TooManyArguments
50+
*/
51+
if ($metaModel instanceof ITranslatedMetaModel) {
52+
$metaModel->selectLanguage($locale);
53+
$mainLanguage = $metaModel->getMainLanguage();
54+
} elseif ($metaModel->isTranslated(false)) {
55+
$mainLanguage = $metaModel->getFallbackLanguage() ?? 'en';
56+
} else {
57+
// Untranslated MetaModel.
58+
$mainLanguage = 'en';
59+
}
60+
$domain = $metaModel->getTableName();
61+
62+
$catalog = new MessageCatalogue($locale);
63+
64+
foreach ($this->builder->fetchAllInputScreensForTable($domain) as $inputScreen) {
65+
foreach (
66+
$this->loadInputScreen($metaModel, $inputScreen, $locale, $mainLanguage, $domain)
67+
->all($domain) as $key => $value
68+
) {
69+
$catalog->set($key, $value, $domain);
70+
}
71+
}
72+
73+
return $catalog;
74+
}
75+
76+
private function loadInputScreen(
77+
IMetaModel $metaModel,
78+
array $inputScreen,
79+
string $locale,
80+
string $mainLanguage,
81+
string $domain
82+
): MessageCatalogue {
83+
$allProps = $inputScreen['properties'];
84+
$properties = array_map(
85+
static fn(array $property): ?string => ($property['col_name'] ?? null),
86+
$allProps
87+
);
88+
$catalog = new MessageCatalogue($locale);
89+
90+
// Load tl_metamodel_item catalogue.
91+
$base = $this->baseTranslator->getCatalogue($locale);
92+
93+
$prefix = 'inputscreen.' . $inputScreen['meta']['id'] . '.';
94+
foreach ($metaModel->getAttributes() as $attribute) {
95+
if (!$this->wantToAdd($attribute, $properties)) {
96+
continue;
97+
}
98+
99+
if ($attribute->get('check_listview') === '1') {
100+
$commandName = 'listviewtoggle_' . $attribute->getColName();
101+
} else {
102+
$commandName = 'publishtoggle_' . $attribute->getColName();
103+
}
104+
105+
$values = ['%name%' => $this->getName($attribute, $locale, $mainLanguage)];
106+
107+
$catalog->set(
108+
$prefix . $commandName . '.label',
109+
strtr($base->get('toggle.label', 'metamodel_checkbox'), $values),
110+
$domain
111+
);
112+
$catalog->set(
113+
$prefix . $commandName . '.description',
114+
strtr($base->get('toggle.description', 'metamodel_checkbox'), $values),
115+
$domain
116+
);
117+
}
118+
119+
return $catalog;
120+
}
121+
122+
/**
123+
* Test if we want to add an operation for the attribute.
124+
*
125+
* @param IAttribute $attribute The attribute to test.
126+
* @param array $properties The property names in the input screen.
127+
*
128+
* @return bool
129+
*/
130+
private function wantToAdd(IAttribute $attribute, array $properties): bool
131+
{
132+
return ($attribute instanceof Checkbox)
133+
&& (($attribute->get('check_publish') === '1') || ($attribute->get('check_listview') === '1'))
134+
&& (in_array($attribute->getColName(), $properties, true));
135+
}
136+
137+
private function getName(IAttribute $attribute, string $locale, string $mainLanguage): string
138+
{
139+
$colName = $attribute->getColName();
140+
$name = $attribute->get('name') ?? $colName;
141+
142+
return $this->extractLangString($name, $locale, $mainLanguage) ?? $colName;
143+
}
144+
145+
/**
146+
* @param string|array<string, string> $value The value.
147+
* @param string $locale The locale.
148+
* @param string $mainLocale The fallback language.
149+
*
150+
* @return string|null
151+
*
152+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
153+
*/
154+
private function extractLangString(string|array $value, string $locale, string $mainLocale): ?string
155+
{
156+
if (!is_array($value)) {
157+
return $value;
158+
}
159+
160+
$fallback = null;
161+
foreach ($value as $langCode => $string) {
162+
if ($locale === $langCode && '' !== $string) {
163+
return $string;
164+
}
165+
if ($mainLocale === $langCode && '' !== $string) {
166+
$fallback = $string;
167+
}
168+
}
169+
170+
if ('en' === $locale && null === $fallback && (null !== $default = ($value['default'] ?? $value[''] ?? null))) {
171+
return $default;
172+
}
173+
174+
return $fallback;
175+
}
176+
}

0 commit comments

Comments
 (0)