Skip to content

Commit 1c2b9a1

Browse files
committed
[TASK] Small improvements with conversion
1 parent 471141e commit 1c2b9a1

6 files changed

Lines changed: 116 additions & 71 deletions

File tree

Classes/Controller/FileController.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public function selectExtensionAction(ServerRequestInterface $request): Response
4545
{
4646
[$extensions] = $this->prepareExtensions($request, false);
4747

48-
$moduleTemplate = $this->initializeModuleTemplate($request);
48+
$moduleTemplate = $this->initializeModuleTemplate(
49+
$request,
50+
'LLL:EXT:ew_llxml2xliff/Resources/Private/Language/locallang.xlf:extension'
51+
);
4952
$moduleTemplate->assign('extensions', $extensions);
5053
return $moduleTemplate->renderResponse('File/SelectExtension');
5154
}
@@ -55,7 +58,10 @@ public function selectFileAction(ServerRequestInterface $request): ResponseInter
5558
[$extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
5659
[$files] = $this->prepareFiles($request, $selectedExtensionKey, false);
5760

58-
$moduleTemplate = $this->initializeModuleTemplate($request);
61+
$moduleTemplate = $this->initializeModuleTemplate(
62+
$request,
63+
'LLL:EXT:ew_llxml2xliff/Resources/Private/Language/locallang.xlf:file'
64+
);
5965
$moduleTemplate->assignMultiple([
6066
'extensions' => $extensions,
6167
'selectedExtension' => $selectedExtension,
@@ -70,7 +76,10 @@ public function confirmConversionAction(ServerRequestInterface $request): Respon
7076
[$extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
7177
[$files, $selectedFile, $selectedFileKey] = $this->prepareFiles($request, $selectedExtensionKey);
7278

73-
$moduleTemplate = $this->initializeModuleTemplate($request);
79+
$moduleTemplate = $this->initializeModuleTemplate(
80+
$request,
81+
'LLL:EXT:ew_llxml2xliff/Resources/Private/Language/locallang.xlf:confirm_selection'
82+
);
7483
$moduleTemplate->assignMultiple([
7584
'extensions' => $extensions,
7685
'selectedExtension' => $selectedExtension,
@@ -90,7 +99,10 @@ public function convertFileAction(ServerRequestInterface $request): ResponseInte
9099
$conversionResult = $this->extensionService
91100
->convertLanguageFile($selectedExtensionKey, $selectedFileKey, $files);
92101

93-
$moduleTemplate = $this->initializeModuleTemplate($request);
102+
$moduleTemplate = $this->initializeModuleTemplate(
103+
$request,
104+
'LLL:EXT:ew_llxml2xliff/Resources/Private/Language/locallang.xlf:finish'
105+
);
94106
$moduleTemplate->assignMultiple([
95107
'extensions' => $extensions,
96108
'selectedExtension' => $selectedExtension,
@@ -169,15 +181,16 @@ protected function isArgumentSetAndAvailable(ServerRequestInterface $request, ar
169181
return ($formFieldValue !== '' && isset($values[$formFieldValue])) ? $formFieldValue : null;
170182
}
171183

172-
protected function initializeModuleTemplate(ServerRequestInterface $request): ModuleTemplate
184+
protected function initializeModuleTemplate(ServerRequestInterface $request, string $context): ModuleTemplate
173185
{
174186
$this->pageRenderer->addCssFile('EXT:ew_llxml2xliff/Resources/Public/Css/form.css');
175187

176188
$moduleTemplate = $this->moduleTemplateFactory->create($request);
177189
$moduleTemplate->setTitle(
178190
$this->getLanguageService()->sL(
179191
'LLL:EXT:ew_llxml2xliff/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'
180-
)
192+
),
193+
$this->getLanguageService()->sL($context)
181194
);
182195

183196
$buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();

Classes/File/Converter.php

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
/**
2424
* Conversion of locallang*.[xml|php] files to locallang.xlf.
25-
*
26-
* @author Xavier Perseguers <xavier@typo3.org>
2725
*/
2826
class Converter
2927
{
@@ -156,7 +154,7 @@ protected function localizedFileRef(string $fileRef, string $lang): string
156154
*/
157155
protected function writeNewXliffFile(string $xmlFile, string $newFileName, string $langKey): string
158156
{
159-
$xml = $this->generateFileContent($xmlFile, $langKey);
157+
$xml = $this->generateFileContent($xmlFile, $langKey, $newFileName);
160158

161159
$result = '';
162160
if (!@file_exists($newFileName)) {
@@ -167,69 +165,37 @@ protected function writeNewXliffFile(string $xmlFile, string $newFileName, strin
167165
return $result;
168166
}
169167

170-
protected function generateFileContent(string $xmlFile, string $langKey): string
168+
protected function generateFileContent(string $xmlFile, string $langKey, string $newFileName): string
171169
{
172170
// Initialize variables:
173-
$xml = [];
174171
$LOCAL_LANG = $this->getCombinedTranslationFileContent($xmlFile);
172+
$originalFile = $this->getOriginalFileName($newFileName, $langKey);
175173

174+
$xml = [];
176175
$xml[] = '<?xml version="1.0" encoding="UTF-8"?>';
177176
$xml[] = '<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">';
178-
$xml[] = ' <file source-language="en"'
179-
. ($langKey !== 'default' ? ' target-language="' . $langKey . '"' : '')
180-
. ' datatype="plaintext" original="EXT:' . $this->extension
181-
. '/Resources/Private/Language/locallang.xlf" date="'
182-
. gmdate('Y-m-d\TH:i:s\Z')
177+
$xml[] = ' <file source-language="en"' . ($langKey !== 'default' ? ' target-language="' . $langKey . '"' : '')
178+
. ' datatype="plaintext" original="' . $originalFile . '" date="' . gmdate('Y-m-d\TH:i:s\Z')
183179
. '" product-name="' . $this->extension . '">';
184-
$xml[] = ' <header/>';
185-
$xml[] = ' <body>';
180+
$xml[] = ' <body>';
186181

187182
foreach ($LOCAL_LANG[$langKey] as $key => $data) {
188-
if (is_array($data)) {
189-
$target = $data[0]['target'] ?? '';
190-
$source = $data[0]['source'] ?? $target;
191-
} else {
192-
$source = $LOCAL_LANG['default'][$key];
193-
$target = $data;
194-
}
195-
196-
if (str_contains($source, chr(10))) {
197-
$preserve = 'xml:space="preserve"';
198-
} else {
199-
$preserve = '';
200-
}
201-
202-
if (empty($source)) {
203-
$source = '<source/>';
204-
} else {
205-
$source = '<source>' . htmlspecialchars($source) . '</source>';
206-
}
207-
208-
if (empty($target)) {
209-
$target = '<target/>';
210-
} else {
211-
$target = '<target>' . htmlspecialchars($target) . '</target>';
212-
}
213-
214-
if ($langKey === 'default') {
215-
$xml[] = ' <trans-unit id="' . $key . '" resname="' . $key . '" ' . $preserve . '>';
216-
$xml[] = ' ' . $source;
217-
} else {
218-
$xml[] = ' <trans-unit id="' . $key . '" resname="' . $key . '" ' . $preserve
219-
. ' approved="yes">';
220-
$xml[] = ' ' . $source;
221-
$xml[] = ' ' . $target;
222-
}
223-
$xml[] = ' </trans-unit>';
183+
$xml[] = (string)(new TransUnit($data, $key, $langKey, $LOCAL_LANG));
224184
}
225185

226-
$xml[] = ' </body>';
227-
$xml[] = ' </file>';
186+
$xml[] = ' </body>';
187+
$xml[] = ' </file>';
228188
$xml[] = '</xliff>';
229189

230190
return implode(LF, $xml);
231191
}
232192

193+
protected function getOriginalFileName(string $filename, string $langKey): string
194+
{
195+
$currentTargetName = str_replace($langKey . '.', '', pathinfo($filename, PATHINFO_FILENAME));
196+
return 'EXT:' . $this->extension . '/Resources/Private/Language/' . $currentTargetName . '.xlf';
197+
}
198+
233199
/**
234200
* Reads/Requires locallang files and returns raw $LOCAL_LANG array
235201
*
@@ -265,23 +231,20 @@ protected function getCombinedTranslationFileContent(string $languageFile): arra
265231

266232
/**
267233
* Converts an XML string to a PHP array.
268-
* This is the reverse function of array2xml()
234+
* This is the reverse function of GeneralUtility::array2xml()
269235
* This is a wrapper for xml2arrayProcess that adds a two-level cache
270236
*
271237
* @param string $string XML content to convert into an array
272-
* @param string $NSprefix The tag-prefix resolve, e.g. a namespace like "T3:"
238+
* @param string $namespacePrefix The tag-prefix resolve, e.g. a namespace like "T3:"
273239
* @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
274240
*
275241
* @return array|string If the parsing had errors, a string with the error message is returned.
276242
* Otherwise, an array with the content.
277243
*
278-
* @see array2xml(),xml2arrayProcess()
244+
* @see GeneralUtility::array2xml(),GeneralUtility::xml2arrayProcess()
279245
*/
280-
protected function xml2array(
281-
string $string,
282-
string $NSprefix = '',
283-
bool $reportDocTag = false
284-
): array|string {
285-
return GeneralUtility::xml2array($string, $NSprefix, $reportDocTag);
246+
protected function xml2array(string $string, string $namespacePrefix = '', bool $reportDocTag = false): array|string
247+
{
248+
return GeneralUtility::xml2array($string, $namespacePrefix, $reportDocTag);
286249
}
287250
}

Classes/File/TransUnit.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is developed by evoWeb.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*
12+
* For the full copyright and license information, please read the
13+
* LICENSE.txt file that was distributed with this source code.
14+
*/
15+
16+
namespace Evoweb\EwLlxml2xliff\File;
17+
18+
readonly class TransUnit
19+
{
20+
protected string $source;
21+
22+
protected string $target;
23+
24+
public function __construct(
25+
string|array $data,
26+
protected string $key,
27+
protected string $langKey,
28+
array $LOCAL_LANG
29+
) {
30+
if (is_array($data)) {
31+
$this->target = $data['target'] ?? '';
32+
$this->source = $data['source'] ?? $this->target;
33+
} else {
34+
$this->target = $data;
35+
$this->source = $LOCAL_LANG['default'][$key];
36+
}
37+
}
38+
39+
protected function getPreserved(): string
40+
{
41+
return str_contains($this->source, chr(10))
42+
? ' xml:space="preserve"'
43+
: '';
44+
}
45+
46+
protected function getApproved(): string
47+
{
48+
return $this->langKey !== 'default' ? ' approved="yes"' : '';
49+
}
50+
51+
protected function getSourceNode(): string
52+
{
53+
return empty($this->source)
54+
? '<source/>'
55+
: '<source>' . htmlspecialchars($this->source) . '</source>';
56+
}
57+
58+
protected function getTargetNode(): string
59+
{
60+
return empty($this->target)
61+
? '<target/>'
62+
: '<target>' . htmlspecialchars($this->target) . '</target>';
63+
}
64+
65+
public function __toString(): string
66+
{
67+
return ' <trans-unit id="' . $this->key . '" resname="' . $this->key . '"'
68+
. $this->getPreserved() . $this->getApproved() . '>' . LF
69+
. ' ' . $this->getSourceNode() . LF
70+
. ($this->langKey !== 'default' ? ' ' . $this->getTargetNode() . LF : '')
71+
. ' </trans-unit>';
72+
}
73+
}

Classes/Localization/Parser/LocallangXmlParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function getParsedDataForElement(\SimpleXMLElement $bodyOfFileTag, str
8787

8888
foreach ($children as $translationElement) {
8989
if ($translationElement->getName() === 'label') {
90-
$parsedData[(string)$translationElement['index']][0] = [
90+
$parsedData[(string)$translationElement['index']] = [
9191
$element => (string)$translationElement,
9292
];
9393
}

Configuration/Backend/Modules.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
'access' => 'user,group',
99
'path' => '/module/web/llxmlconverter',
1010
'iconIdentifier' => 'llxml2xlifficon',
11-
'inheritNavigationComponentFromMainModule' => false,
1211
'labels' => 'LLL:EXT:ew_llxml2xliff/Resources/Private/Language/locallang_mod.xlf',
1312
'routes' => [
1413
'_default' => [

Configuration/Services.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@ services:
66
public: false
77

88
Evoweb\EwLlxml2xliff\:
9-
resource: '../Classes/*'
10-
11-
Evoweb\EwLlxml2xliff\Controller\FileController:
12-
tags: [ 'backend.controller' ]
9+
resource: '../Classes/'

0 commit comments

Comments
 (0)