function PoDatabaseWriter::importString
Same name in other branches
- 9 core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::importString()
- 8.9.x core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::importString()
- 10 core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::importString()
Imports one string into the database.
Parameters
\Drupal\Component\Gettext\PoItem $item: The item being imported.
Return value
int The string ID of the existing string modified or the new string added.
1 call to PoDatabaseWriter::importString()
- PoDatabaseWriter::writeItem in core/
modules/ locale/ src/ PoDatabaseWriter.php - Writes the given item.
File
-
core/
modules/ locale/ src/ PoDatabaseWriter.php, line 216
Class
- PoDatabaseWriter
- Gettext PO writer working with the locale module database.
Namespace
Drupal\localeCode
private function importString(PoItem $item) {
// Initialize overwrite options if not set.
$this->options['overwrite_options'] += [
'not_customized' => FALSE,
'customized' => FALSE,
];
$overwrite_options = $this->options['overwrite_options'];
$customized = $this->options['customized'];
$context = $item->getContext();
$source = $item->getSource();
$translation = $item->getTranslation();
// Look up the source string and any existing translation.
$strings = \Drupal::service('locale.storage')->getTranslations([
'language' => $this->langcode,
'source' => $source,
'context' => $context,
]);
$string = reset($strings);
if (!empty($translation)) {
// Skip this string unless it passes a check for dangerous code.
if (!locale_string_is_safe($translation)) {
\Drupal::logger('locale')->error('Import of string "%string" was skipped because of disallowed or malformed HTML.', [
'%string' => $translation,
]);
$this->report['skips']++;
return 0;
}
elseif ($string) {
$string->setString($translation);
if ($string->isNew()) {
// No translation in this language.
$string->setValues([
'language' => $this->langcode,
'customized' => $customized,
]);
$string->save();
$this->report['additions']++;
}
elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
// Translation exists, only overwrite if instructed.
$string->customized = $customized;
$string->save();
$this->report['updates']++;
}
$this->report['strings'][] = $string->getId();
return $string->lid;
}
else {
// No such source string in the database yet.
$string = \Drupal::service('locale.storage')->createString([
'source' => $source,
'context' => $context,
])
->save();
\Drupal::service('locale.storage')->createTranslation([
'lid' => $string->getId(),
'language' => $this->langcode,
'translation' => $translation,
'customized' => $customized,
])
->save();
$this->report['additions']++;
$this->report['strings'][] = $string->getId();
return $string->lid;
}
}
elseif ($string && !$string->isNew() && $overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
// Empty translation, remove existing if instructed.
$string->delete();
$this->report['deletes']++;
$this->report['strings'][] = $string->lid;
return $string->lid;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.