Commit 1e624594 authored by Fabian Galinski's avatar Fabian Galinski 😾
Browse files

[BUGFIX] Errors can appear for the first usage of a language file with <CDATA[]]> values

parent 7a8433aa
......@@ -87,6 +87,8 @@ abstract class FileBaseService extends FileService {
$this->setWorkspace('base');
parent::init($file, $path);
$this->prepareLanguageFilesForFirstUsage();
}
/**
......@@ -141,6 +143,55 @@ abstract class FileBaseService extends FileService {
$this->localLang = $localLang;
$this->originLang = $originLang;
}
/**
* Prepares this language file and it's translations for the first usage.
*
* @throws LFException
* @return void
*/
protected function prepareLanguageFilesForFirstUsage() {
$adaptedContent = $this->checkFileForFirstImport($this->absFile);
if ($adaptedContent) {
$files = [];
$files[$this->absFile] = $adaptedContent;
$languages = SgLib::getSystemLanguages();
foreach ($languages as $lang) {
if ($lang === 'default') {
continue;
}
$file = dirname($this->absFile) . '/' . $this->nameLocalizedFile($lang);
if (file_exists($file) && $this->checkLocalizedFile(basename($file), $lang)) {
$adaptedContent = $this->checkFileForFirstImport($file);
if ($adaptedContent) {
$files[$file] = $adaptedContent;
}
}
}
$this->writeFilesWithContent($files);
}
}
/**
* Checks if the given file is generated by this extension, if not the data will be prepared and returned.
*
* @param string $file
* @return bool|string
*/
protected function checkFileForFirstImport($file) {
$fileContent = file_get_contents($file);
if (strpos($fileContent, '<generator>LFEditor</generator>') === FALSE) {
$fileContent = str_replace('<![CDATA[', '&lt;![CDATA[', $fileContent);
$fileContent = str_replace(']]>', ']]&gt;', $fileContent);
} else {
return FALSE;
}
return $fileContent;
}
}
?>
\ No newline at end of file
......@@ -277,16 +277,32 @@ abstract class FileService extends AbstractService {
public function writeFile($editedLanguages = NULL) {
// get prepared language files
$languageFiles = $this->prepareFileContents($editedLanguages);
$this->writeFilesWithContent($languageFiles);
}
/**
* Writes the given files with the given content.
*
* Array structure:
* array (
* '/var/www/file.xlf' => 'My content',
* ...
* )
*
* @param array $files
* @throws LFException
* @return void
*/
public function writeFilesWithContent(array $files = []) {
// check write permissions of all files
foreach ($languageFiles as $file => $content) {
foreach ($files as $file => $content) {
if (!SgLib::checkWritePerms($file)) {
throw new LFException('failure.file.badPermissions');
}
}
// write files
foreach ($languageFiles as $file => $content) {
foreach ($files as $file => $content) {
if (!GeneralUtility::writeFile($file, $content)) {
throw new LFException('failure.file.notWritten');
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment