3 namespace Drupal\locale;
5 use Drupal\Component\Gettext\PoHeader;
6 use Drupal\Component\Gettext\PoItem;
7 use Drupal\Component\Gettext\PoReaderInterface;
8 use Drupal\Component\Gettext\PoWriterInterface;
11 * Gettext PO writer working with the locale module database.
13 class PoDatabaseWriter implements PoWriterInterface {
16 * An associative array indicating what data should be overwritten, if any.
18 * Elements of the array:
20 * - not_customized: boolean indicating that not customized strings should
22 * - customized: boolean indicating that customized strings should be
24 * - customized: the strings being imported should be saved as customized.
25 * One of LOCALE_CUSTOMIZED or LOCALE_NOT_CUSTOMIZED.
32 * Language code of the language being written to the database.
39 * Header of the po file written to the database.
41 * @var \Drupal\Component\Gettext\PoHeader
46 * Associative array summarizing the number of changes done.
49 * - additions: number of source strings newly added
50 * - updates: number of translations updated
51 * - deletes: number of translations deleted
52 * - skips: number of strings skipped due to disallowed HTML
59 * Constructor, initialize reporting array.
61 public function __construct() {
68 public function getLangcode() {
69 return $this->langcode;
75 public function setLangcode($langcode) {
76 $this->langcode = $langcode;
80 * Get the report of the write operations.
82 public function getReport() {
87 * Set the report array of write operations.
89 * @param array $report
90 * Associative array with result information.
92 public function setReport($report = []) {
100 $this->report = $report;
104 * Get the options used by the writer.
106 public function getOptions() {
107 return $this->options;
111 * Set the options for the current writer.
113 public function setOptions(array $options) {
114 if (!isset($options['overwrite_options'])) {
115 $options['overwrite_options'] = [];
117 $options['overwrite_options'] += [
118 'not_customized' => FALSE,
119 'customized' => FALSE,
122 'customized' => LOCALE_NOT_CUSTOMIZED,
124 $this->options = $options;
130 public function getHeader() {
131 return $this->header;
135 * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader().
137 * Sets the header and configure Drupal accordingly.
139 * Before being able to process the given header we need to know in what
140 * context this database write is done. For this the options must be set.
142 * A langcode is required to set the current header's PluralForm.
144 * @param \Drupal\Component\Gettext\PoHeader $header
149 public function setHeader(PoHeader $header) {
150 $this->header = $header;
151 $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: [];
153 // Check for options.
154 $options = $this->getOptions();
155 if (empty($options)) {
156 throw new \Exception('Options should be set before assigning a PoHeader.');
158 $overwrite_options = $options['overwrite_options'];
160 // Check for langcode.
161 $langcode = $this->langcode;
162 if (empty($langcode)) {
163 throw new \Exception('Langcode should be set before assigning a PoHeader.');
166 if (array_sum($overwrite_options) || empty($locale_plurals[$langcode]['plurals'])) {
167 // Get and store the plural formula if available.
168 $plural = $header->getPluralForms();
169 if (isset($plural) && $p = $header->parsePluralForms($plural)) {
170 list($nplurals, $formula) = $p;
171 \Drupal::service('locale.plural.formula')->setPluralFormula($langcode, $nplurals, $formula);
179 public function writeItem(PoItem $item) {
180 if ($item->isPlural()) {
181 $item->setSource(implode(LOCALE_PLURAL_DELIMITER, $item->getSource()));
182 $item->setTranslation(implode(LOCALE_PLURAL_DELIMITER, $item->getTranslation()));
184 $this->importString($item);
190 public function writeItems(PoReaderInterface $reader, $count = -1) {
191 $forever = $count == -1;
192 while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
193 $this->writeItem($item);
198 * Imports one string into the database.
200 * @param \Drupal\Component\Gettext\PoItem $item
201 * The item being imported.
204 * The string ID of the existing string modified or the new string added.
206 private function importString(PoItem $item) {
207 // Initialize overwrite options if not set.
208 $this->options['overwrite_options'] += [
209 'not_customized' => FALSE,
210 'customized' => FALSE,
212 $overwrite_options = $this->options['overwrite_options'];
213 $customized = $this->options['customized'];
215 $context = $item->getContext();
216 $source = $item->getSource();
217 $translation = $item->getTranslation();
219 // Look up the source string and any existing translation.
220 $strings = \Drupal::service('locale.storage')->getTranslations([
221 'language' => $this->langcode,
223 'context' => $context,
225 $string = reset($strings);
227 if (!empty($translation)) {
228 // Skip this string unless it passes a check for dangerous code.
229 if (!locale_string_is_safe($translation)) {
230 \Drupal::logger('locale')->error('Import of string "%string" was skipped because of disallowed or malformed HTML.', ['%string' => $translation]);
231 $this->report['skips']++;
235 $string->setString($translation);
236 if ($string->isNew()) {
237 // No translation in this language.
239 'language' => $this->langcode,
240 'customized' => $customized,
243 $this->report['additions']++;
245 elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
246 // Translation exists, only overwrite if instructed.
247 $string->customized = $customized;
249 $this->report['updates']++;
251 $this->report['strings'][] = $string->getId();
255 // No such source string in the database yet.
256 $string = \Drupal::service('locale.storage')->createString(['source' => $source, 'context' => $context])
258 \Drupal::service('locale.storage')->createTranslation([
259 'lid' => $string->getId(),
260 'language' => $this->langcode,
261 'translation' => $translation,
262 'customized' => $customized,
265 $this->report['additions']++;
266 $this->report['strings'][] = $string->getId();
270 elseif ($string && !$string->isNew() && $overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
271 // Empty translation, remove existing if instructed.
273 $this->report['deletes']++;
274 $this->report['strings'][] = $string->lid;