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 * @param array $options
114 * An associative array containing:
115 * - overwrite_options: An array of options. Each option contains:
116 * - not_customized: Boolean indicating that not customized strings should
118 * - customized: Boolean indicating that customized strings should be
120 * - customized: The strings being imported should be saved as customized.
121 * One of LOCALE_CUSTOMIZED or LOCALE_NOT_CUSTOMIZED.
123 public function setOptions(array $options) {
124 if (!isset($options['overwrite_options'])) {
125 $options['overwrite_options'] = [];
127 $options['overwrite_options'] += [
128 'not_customized' => FALSE,
129 'customized' => FALSE,
132 'customized' => LOCALE_NOT_CUSTOMIZED,
134 $this->options = $options;
140 public function getHeader() {
141 return $this->header;
145 * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader().
147 * Sets the header and configure Drupal accordingly.
149 * Before being able to process the given header we need to know in what
150 * context this database write is done. For this the options must be set.
152 * A langcode is required to set the current header's PluralForm.
154 * @param \Drupal\Component\Gettext\PoHeader $header
159 public function setHeader(PoHeader $header) {
160 $this->header = $header;
161 $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: [];
163 // Check for options.
164 $options = $this->getOptions();
165 if (empty($options)) {
166 throw new \Exception('Options should be set before assigning a PoHeader.');
168 $overwrite_options = $options['overwrite_options'];
170 // Check for langcode.
171 $langcode = $this->langcode;
172 if (empty($langcode)) {
173 throw new \Exception('Langcode should be set before assigning a PoHeader.');
176 if (array_sum($overwrite_options) || empty($locale_plurals[$langcode]['plurals'])) {
177 // Get and store the plural formula if available.
178 $plural = $header->getPluralForms();
179 if (isset($plural) && $p = $header->parsePluralForms($plural)) {
180 list($nplurals, $formula) = $p;
181 \Drupal::service('locale.plural.formula')->setPluralFormula($langcode, $nplurals, $formula);
189 public function writeItem(PoItem $item) {
190 if ($item->isPlural()) {
191 $item->setSource(implode(LOCALE_PLURAL_DELIMITER, $item->getSource()));
192 $item->setTranslation(implode(LOCALE_PLURAL_DELIMITER, $item->getTranslation()));
194 $this->importString($item);
200 public function writeItems(PoReaderInterface $reader, $count = -1) {
201 $forever = $count == -1;
202 while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
203 $this->writeItem($item);
208 * Imports one string into the database.
210 * @param \Drupal\Component\Gettext\PoItem $item
211 * The item being imported.
214 * The string ID of the existing string modified or the new string added.
216 private function importString(PoItem $item) {
217 // Initialize overwrite options if not set.
218 $this->options['overwrite_options'] += [
219 'not_customized' => FALSE,
220 'customized' => FALSE,
222 $overwrite_options = $this->options['overwrite_options'];
223 $customized = $this->options['customized'];
225 $context = $item->getContext();
226 $source = $item->getSource();
227 $translation = $item->getTranslation();
229 // Look up the source string and any existing translation.
230 $strings = \Drupal::service('locale.storage')->getTranslations([
231 'language' => $this->langcode,
233 'context' => $context,
235 $string = reset($strings);
237 if (!empty($translation)) {
238 // Skip this string unless it passes a check for dangerous code.
239 if (!locale_string_is_safe($translation)) {
240 \Drupal::logger('locale')->error('Import of string "%string" was skipped because of disallowed or malformed HTML.', ['%string' => $translation]);
241 $this->report['skips']++;
245 $string->setString($translation);
246 if ($string->isNew()) {
247 // No translation in this language.
249 'language' => $this->langcode,
250 'customized' => $customized,
253 $this->report['additions']++;
255 elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
256 // Translation exists, only overwrite if instructed.
257 $string->customized = $customized;
259 $this->report['updates']++;
261 $this->report['strings'][] = $string->getId();
265 // No such source string in the database yet.
266 $string = \Drupal::service('locale.storage')->createString(['source' => $source, 'context' => $context])
268 \Drupal::service('locale.storage')->createTranslation([
269 'lid' => $string->getId(),
270 'language' => $this->langcode,
271 'translation' => $translation,
272 'customized' => $customized,
275 $this->report['additions']++;
276 $this->report['strings'][] = $string->getId();
280 elseif ($string && !$string->isNew() && $overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
281 // Empty translation, remove existing if instructed.
283 $this->report['deletes']++;
284 $this->report['strings'][] = $string->lid;