/**
* Imports translations from a csv file
* The CSV file has to have the same format as an Pimcore translation-export-file
*
* @static
* @param $file - path to the csv file
* @param bool $replaceExistingTranslations
* @throws \Exception
*/
public static function importTranslationsFromFile($file, $replaceExistingTranslations = true, $languages = null)
{
$delta = [];
if (is_readable($file)) {
if (!$languages || empty($languages) || !is_array($languages)) {
$languages = Tool::getValidLanguages();
}
//read import data
$tmpData = file_get_contents($file);
//replace magic excel bytes
$tmpData = str_replace("", "", $tmpData);
//convert to utf-8 if needed
$tmpData = Tool\Text::convertToUTF8($tmpData);
//store data for further usage
$importFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations";
File::put($importFile, $tmpData);
$importFileOriginal = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations_original";
File::put($importFileOriginal, $tmpData);
// determine csv type
$dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations_original");
//read data
if (($handle = fopen(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations", "r")) !== false) {
while (($rowData = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar)) !== false) {
$data[] = $rowData;
}
fclose($handle);
}
//process translations
if (is_array($data) and count($data) > 1) {
$keys = $data[0];
// remove wrong quotes in some export/import constellations
$keys = array_map(function ($value) {
return trim($value, '""');
}, $keys);
$data = array_slice($data, 1);
foreach ($data as $row) {
$keyValueArray = [];
for ($counter = 0; $counter < count($row); $counter++) {
$rd = str_replace(""", '"', $row[$counter]);
$keyValueArray[$keys[$counter]] = $rd;
}
$textKey = $keyValueArray["key"];
if ($textKey) {
$t = static::getByKey($textKey, true);
$dirty = false;
foreach ($keyValueArray as $key => $value) {
if (in_array($key, $languages)) {
$currentTranslation = $t->getTranslation($key);
if ($replaceExistingTranslations) {
$t->addTranslation($key, $value);
if ($currentTranslation != $value) {
$dirty = true;
}
} else {
if (!$t->getTranslation($key)) {
$t->addTranslation($key, $value);
if ($currentTranslation != $value) {
$dirty = true;
}
} elseif ($t->getTranslation($key) != $value && $value) {
$delta[] = ["lg" => $key, "key" => $textKey, "text" => $t->getTranslation($key), "csv" => $value];
}
}
}
}
if ($dirty) {
if ($keyValueArray['creationDate']) {
$t->setCreationDate($keyValueArray['creationDate']);
}
$t->setModificationDate(time());
//ignore modificationDate from file
$t->save();
}
}
}
Model\Translation\AbstractTranslation::clearDependentCache();
} else {
throw new \Exception("less than 2 rows of data - nothing to import");
}
} else {
throw new \Exception("{$file} is not readable");
}
return $delta;
}