class PoDatabaseWriter implements PoWriterInterface {
private $_options;
private $_langcode;
private $_header;
private $_report;
function __construct() {
$this
->setReport();
}
public function getLangcode() {
return $this->_langcode;
}
public function setLangcode($langcode) {
$this->_langcode = $langcode;
}
public function getReport() {
return $this->_report;
}
function setReport($report = array()) {
$report += array(
'additions' => 0,
'updates' => 0,
'deletes' => 0,
'skips' => 0,
'strings' => array(),
);
$this->_report = $report;
}
function getOptions() {
return $this->_options;
}
function setOptions(array $options) {
if (!isset($options['overwrite_options'])) {
$options['overwrite_options'] = array();
}
$options['overwrite_options'] += array(
'not_customized' => FALSE,
'customized' => FALSE,
);
$options += array(
'customized' => LOCALE_NOT_CUSTOMIZED,
);
$this->_options = $options;
}
function getHeader() {
return $this->_header;
}
function setHeader(PoHeader $header) {
$this->_header = $header;
$locale_plurals = variable_get('locale_translation_plurals', array());
$options = $this
->getOptions();
if (empty($options)) {
throw new Exception("Options should be set before assigning a PoHeader.");
}
$overwrite_options = $options['overwrite_options'];
$langcode = $this->_langcode;
if (empty($langcode)) {
throw new Exception("Langcode should be set before assigning a PoHeader.");
}
if (array_sum($overwrite_options) || empty($locale_plurals[$langcode]['plurals'])) {
$plural = $header
->getPluralForms();
if (isset($plural) && ($p = $header
->parsePluralForms($plural))) {
list($nplurals, $formula) = $p;
$locale_plurals[$langcode] = array(
'plurals' => $nplurals,
'formula' => $formula,
);
variable_set('locale_translation_plurals', $locale_plurals);
}
}
}
function writeItem(PoItem $item) {
if ($item
->isPlural()) {
$item
->setSource(join(LOCALE_PLURAL_DELIMITER, $item
->getSource()));
$item
->setTranslation(join(LOCALE_PLURAL_DELIMITER, $item
->getTranslation()));
}
$this
->importString($item);
}
public function writeItems(PoReaderInterface $reader, $count = -1) {
$forever = $count == -1;
while (($count-- > 0 || $forever) && ($item = $reader
->readItem())) {
$this
->writeItem($item);
}
}
private function importString(PoItem $item) {
$this->_options['overwrite_options'] += array(
'not_customized' => FALSE,
'customized' => FALSE,
);
$overwrite_options = $this->_options['overwrite_options'];
$customized = $this->_options['customized'];
$context = $item
->getContext();
$source = $item
->getSource();
$translation = $item
->getTranslation();
$strings = locale_storage()
->getTranslations(array(
'language' => $this->_langcode,
'source' => $source,
'context' => $context,
));
$string = reset($strings);
if (!empty($translation)) {
if (!locale_string_is_safe($translation)) {
watchdog('locale', 'Import of string "%string" was skipped because of disallowed or malformed HTML.', array(
'%string' => $translation,
), WATCHDOG_ERROR);
$this->_report['skips']++;
return 0;
}
elseif ($string) {
$string
->setString($translation);
if ($string
->isNew()) {
$string
->setValues(array(
'language' => $this->_langcode,
'customized' => $customized,
));
$string
->save();
$this->_report['additions']++;
}
elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
$string->customized = $customized;
$string
->save();
$this->_report['updates']++;
}
$this->_report['strings'][] = $string
->getId();
return $string->lid;
}
else {
$string = locale_storage()
->createString(array(
'source' => $source,
'context' => $context,
))
->save();
$target = locale_storage()
->createTranslation(array(
'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']) {
$string
->delete();
$this->_report['deletes']++;
$this->_report['strings'][] = $string->lid;
return $string->lid;
}
}
}