View source
<?php
namespace Drupal\lingotek;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Exception\LingotekApiException;
use Drupal\lingotek\Exception\LingotekContentEntityStorageException;
use Drupal\lingotek\Exception\LingotekDocumentArchivedException;
use Drupal\lingotek\Exception\LingotekDocumentLockedException;
use Drupal\lingotek\Exception\LingotekPaymentRequiredException;
class LingotekInterfaceTranslationService implements LingotekInterfaceTranslationServiceInterface {
use StringTranslationTrait;
protected $lingotek;
protected $languageLocaleMapper;
protected $lingotekConfiguration;
protected $languageManager;
protected $connection;
protected $moduleHandler;
protected $themeHandler;
public function __construct(LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper, LingotekConfigurationServiceInterface $lingotek_configuration, LanguageManagerInterface $language_manager, Connection $connection, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
$this->lingotek = $lingotek;
$this->languageLocaleMapper = $language_locale_mapper;
$this->lingotekConfiguration = $lingotek_configuration;
$this->languageManager = $language_manager;
$this->connection = $connection;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
}
protected function getMetadata($component) {
$state = \Drupal::state();
$translations_metadata = $state
->get('lingotek.interface_translations_metadata');
$component_metadata = [];
if ($translations_metadata) {
if (isset($translations_metadata[$component])) {
$component_metadata = $translations_metadata[$component];
}
}
return $component_metadata;
}
protected function saveMetadata($component, $metadata) {
$state = \Drupal::state();
$translations_metadata = $state
->get('lingotek.interface_translations_metadata');
if (!$translations_metadata) {
$translations_metadata = [];
}
$translations_metadata[$component] = $metadata;
$state
->set('lingotek.interface_translations_metadata', $translations_metadata);
}
public function checkSourceStatus($component) {
$document_id = $this
->getDocumentId($component);
if ($document_id) {
if ($this->lingotek
->getDocumentStatus($document_id)) {
$this
->setSourceStatus($component, Lingotek::STATUS_CURRENT);
return TRUE;
}
else {
return FALSE;
}
}
return FALSE;
}
public function getSourceStatus($component) {
$source_language = 'en';
return $this
->getTargetStatus($component, $source_language);
}
public function setSourceStatus($component, $status) {
$source_language = 'en';
return $this
->setTargetStatus($component, $source_language, $status);
}
protected function clearTargetStatuses($component) {
$source_status = $this
->getSourceStatus($component);
$metadata = $this
->getMetadata($component);
if (!empty($metadata) && isset($metadata['translation_status'])) {
unset($metadata['translation_status']);
$this
->saveMetadata($component, $metadata);
}
$this
->setTargetStatus($component, 'en', $source_status);
}
public function checkTargetStatuses($component) {
$document_id = $this
->getDocumentId($component);
$translation_statuses = $this->lingotek
->getDocumentTranslationStatuses($document_id);
$source_status = $this
->getSourceStatus($component);
$statuses = [];
$languages = $this->languageManager
->getLanguages();
foreach ($languages as $language) {
$statuses[$language
->getId()] = $this
->getTargetStatus($component, $language
->getId());
}
$this
->clearTargetStatuses($component);
foreach ($translation_statuses as $lingotek_locale => $progress) {
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($lingotek_locale);
if ($drupal_language == NULL) {
continue;
}
$langcode = $drupal_language
->id();
$current_target_status = $statuses[$langcode];
if (in_array($current_target_status, [
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_DISABLED,
Lingotek::STATUS_EDITED,
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_NONE,
Lingotek::STATUS_READY,
Lingotek::STATUS_PENDING,
Lingotek::STATUS_CANCELLED,
NULL,
])) {
if ($progress === Lingotek::STATUS_CANCELLED) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_CANCELLED);
}
elseif ($progress === Lingotek::PROGRESS_COMPLETE) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_READY);
}
else {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_PENDING);
}
}
if ($source_status !== Lingotek::STATUS_CURRENT && $statuses[$langcode] === Lingotek::STATUS_EDITED && $langcode !== 'en') {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_EDITED);
}
if ($source_status === Lingotek::STATUS_CURRENT && $statuses[$langcode] === Lingotek::STATUS_CURRENT && $langcode !== 'en') {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_CURRENT);
}
}
}
public function checkTargetStatus($component, $langcode) {
$current_status = $this
->getTargetStatus($component, $langcode);
$locale = $this->languageLocaleMapper
->getLocaleForLangcode($langcode);
$source_status = $this
->getSourceStatus($component);
$document_id = $this
->getDocumentId($component);
if ($langcode !== 'en') {
if (($current_status == Lingotek::STATUS_PENDING || $current_status == Lingotek::STATUS_EDITED) && $source_status !== Lingotek::STATUS_EDITED) {
$translation_status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
if ($translation_status === Lingotek::STATUS_CANCELLED) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_CANCELLED);
}
elseif ($translation_status === TRUE) {
$current_status = Lingotek::STATUS_READY;
$this
->setTargetStatus($component, $langcode, $current_status);
}
elseif ($this->lingotek
->downloadDocument($document_id, $locale)) {
}
}
elseif ($current_status == Lingotek::STATUS_REQUEST || $current_status == Lingotek::STATUS_UNTRACKED) {
$translation_status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
if ($translation_status === TRUE) {
$current_status = Lingotek::STATUS_READY;
$this
->setTargetStatus($component, $langcode, $current_status);
}
elseif ($translation_status !== FALSE) {
$current_status = Lingotek::STATUS_PENDING;
$this
->setTargetStatus($component, $langcode, $current_status);
}
}
}
return $current_status;
}
public function getTargetStatus($component, $langcode) {
$status = Lingotek::STATUS_UNTRACKED;
$statuses = $this
->getTargetStatuses($component);
if (isset($statuses[$langcode])) {
$status = $statuses[$langcode];
}
return $status;
}
public function getTargetStatuses($component) {
$metadata = $this
->getMetadata($component);
return isset($metadata['translation_status']) ? $metadata['translation_status'] : [];
}
public function setTargetStatus($component, $langcode, $status) {
$metadata = $this
->getMetadata($component);
$metadata['translation_status'][$langcode] = $status;
$this
->saveMetadata($component, $metadata);
return $component;
}
public function setTargetStatuses($component, $status) {
$target_languages = $this->languageManager
->getLanguages();
$source_langcode = 'en';
foreach ($target_languages as $langcode => $language) {
if ($langcode != $source_langcode && ($current_status = $this
->getTargetStatus($component, $langcode))) {
if ($current_status === Lingotek::STATUS_PENDING && $status === Lingotek::STATUS_REQUEST) {
continue;
}
if (in_array($current_status, [
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_DISABLED,
NULL,
]) && $status === Lingotek::STATUS_PENDING) {
continue;
}
if ($current_status == $status) {
continue;
}
if ($current_status != Lingotek::STATUS_EDITED && $current_status !== Lingotek::STATUS_CURRENT) {
$this
->setTargetStatus($component, $langcode, $status);
}
elseif ($current_status == Lingotek::STATUS_EDITED && in_array($status, [
Lingotek::STATUS_CURRENT,
Lingotek::STATUS_PENDING,
])) {
$this
->setTargetStatus($component, $langcode, $status);
}
if ($status === Lingotek::STATUS_CANCELLED) {
$this
->setTargetStatus($component, $langcode, $status);
}
if ($status === Lingotek::STATUS_DISABLED) {
$this
->setTargetStatus($component, $langcode, $status);
}
}
}
}
public function markTranslationsAsDirty($component) {
$target_languages = $this->languageManager
->getLanguages();
$source_langcode = 'en';
$to_change = [
Lingotek::STATUS_CURRENT,
];
foreach ($target_languages as $langcode => $language) {
if ($langcode != $source_langcode && ($current_status = $this
->getTargetStatus($component, $langcode))) {
if (in_array($current_status, $to_change)) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_PENDING);
}
}
}
}
public function getDocumentId($component) {
$doc_id = NULL;
$metadata = $this
->getMetadata($component);
if (!empty($metadata) && isset($metadata['document_id'])) {
$doc_id = $metadata['document_id'];
}
return $doc_id;
}
public function setDocumentId($component, $doc_id) {
$metadata = $this
->getMetadata($component);
$metadata['document_id'] = $doc_id;
$this
->saveMetadata($component, $metadata);
return $component;
}
public function getSourceLocale($component) {
$source_language = 'en';
return $this->languageLocaleMapper
->getLocaleForLangcode($source_language);
}
public function getSourceData($component) {
$data = [];
$potx_strings = $this
->extractPotxStrings($component);
if (!empty($potx_strings)) {
foreach ($potx_strings as $potx_string => $potx_string_meta) {
$translationStringKey = str_replace("\0", "<PLURAL>", $potx_string);
$explodedStrings = explode("\0", $potx_string);
$translationData = [];
foreach ($explodedStrings as $index => $explodedString) {
$translationData[$explodedString] = $explodedString;
}
foreach ($potx_string_meta as $context => $potx_string_meta_locations) {
$translationStringKeyWithContext = $translationStringKey . '<CONTEXT>' . $context;
$data[$translationStringKeyWithContext] = $translationData;
$data[$translationStringKeyWithContext]['_context'] = $context;
}
}
}
return $data;
}
public function updateEntityHash($component) {
$source_data = json_encode($this
->getSourceData($component));
$metadata = $this
->getMetadata($component);
if (!empty($metadata)) {
$metadata['lingotek_hash'] = md5($source_data);
$this
->saveMetadata($component, $metadata);
}
}
public function hasEntityChanged($component) {
return TRUE;
}
public function addTarget($component, $locale) {
$source_langcode = 'en';
$source_locale = $this->languageLocaleMapper
->getLocaleForLangcode($source_langcode);
if ($locale == $source_locale) {
return FALSE;
}
if ($document_id = $this
->getDocumentId($component)) {
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($locale);
$source_status = $this
->getSourceStatus($component);
$current_status = $this
->getTargetStatus($component, $drupal_language
->id());
$pristine_statuses = [
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_EDITED,
];
if (in_array($current_status, $pristine_statuses)) {
try {
$result = $this->lingotek
->addTarget($document_id, $locale, NULL);
} catch (LingotekDocumentLockedException $exception) {
$this
->setDocumentId($component, $exception
->getNewDocumentId());
throw $exception;
} catch (LingotekDocumentArchivedException $exception) {
$this
->setDocumentId($component, NULL);
$this
->deleteMetadata($component);
throw $exception;
} catch (LingotekPaymentRequiredException $exception) {
throw $exception;
} catch (LingotekApiException $exception) {
throw $exception;
}
if ($result) {
$this
->setTargetStatus($component, $drupal_language
->id(), Lingotek::STATUS_PENDING);
if ($source_status == Lingotek::STATUS_IMPORTING) {
$this
->setSourceStatus($component, Lingotek::STATUS_CURRENT);
}
return TRUE;
}
}
}
return FALSE;
}
public function requestTranslations($component) {
$languages = [];
if ($document_id = $this
->getDocumentId($component)) {
$target_languages = $this->languageManager
->getLanguages();
$target_languages = array_filter($target_languages, function (LanguageInterface $language) {
$configLanguage = ConfigurableLanguage::load($language
->getId());
return $this->lingotekConfiguration
->isLanguageEnabled($configLanguage);
});
$source_langcode = 'en';
foreach ($target_languages as $langcode => $language) {
$locale = $this->languageLocaleMapper
->getLocaleForLangcode($langcode);
if ($langcode !== $source_langcode) {
$source_status = $this
->getSourceStatus($component);
$current_status = $this
->getTargetStatus($component, $langcode);
if ($current_status !== Lingotek::STATUS_PENDING && $current_status !== Lingotek::STATUS_CURRENT && $current_status !== Lingotek::STATUS_EDITED && $current_status !== Lingotek::STATUS_READY) {
try {
$result = $this->lingotek
->addTarget($document_id, $locale, NULL);
} catch (LingotekDocumentLockedException $exception) {
$this
->setDocumentId($component, $exception
->getNewDocumentId());
throw $exception;
} catch (LingotekDocumentArchivedException $exception) {
$this
->setDocumentId($component, NULL);
$this
->deleteMetadata($component);
throw $exception;
} catch (LingotekPaymentRequiredException $exception) {
throw $exception;
} catch (LingotekApiException $exception) {
throw $exception;
}
if ($result) {
$languages[] = $langcode;
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_PENDING);
if ($source_status == Lingotek::STATUS_IMPORTING) {
$this
->setSourceStatus($component, Lingotek::STATUS_CURRENT);
}
}
}
}
}
}
return $languages;
}
public function uploadDocument($component, $job_id = NULL) {
if ($job_id === NULL) {
$job_id = $this
->getJobId($component) ?: NULL;
}
if ($document_id = $this
->getDocumentId($component)) {
return $this
->updateDocument($component, $job_id);
}
$source_data = $this
->getSourceData($component);
$document_name = 'Interface translation: ' . $component;
\Drupal::moduleHandler()
->invokeAll('lingotek_interface_translation_document_upload', [
&$source_data,
&$component,
]);
try {
$document_id = $this->lingotek
->uploadDocument($document_name, $source_data, $this
->getSourceLocale($component), NULL, NULL, $job_id);
} catch (LingotekPaymentRequiredException $exception) {
$this
->setSourceStatus($component, Lingotek::STATUS_ERROR);
throw $exception;
} catch (LingotekApiException $exception) {
$this
->setSourceStatus($component, Lingotek::STATUS_ERROR);
throw $exception;
}
if ($document_id) {
$this
->setDocumentId($component, $document_id);
$this
->setSourceStatus($component, Lingotek::STATUS_IMPORTING);
$this
->setTargetStatuses($component, Lingotek::STATUS_REQUEST);
$this
->setJobId($component, $job_id);
$this
->setLastUploaded($component, \Drupal::time()
->getRequestTime());
return $document_id;
}
return FALSE;
}
public function downloadDocument($component, $locale) {
if ($document_id = $this
->getDocumentId($component)) {
$source_status = $this
->getSourceStatus($component);
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($locale);
$langcode = $drupal_language
->id();
$data = [];
try {
if ($this->lingotek
->getDocumentTranslationStatus($document_id, $locale) !== FALSE) {
$data = $this->lingotek
->downloadDocument($document_id, $locale);
}
else {
\Drupal::logger('lingotek')
->warning('Avoided download for (%component): Source status is %source_status.', [
'%component' => $component,
'%source_status' => $this
->getSourceStatus($component),
]);
return NULL;
}
} catch (LingotekApiException $exception) {
\Drupal::logger('lingotek')
->error('Error happened downloading %document_id %locale: %message', [
'%document_id' => $document_id,
'%locale' => $locale,
'%message' => $exception
->getMessage(),
]);
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
throw $exception;
}
if ($data) {
$status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
$transaction = $this->connection
->startTransaction();
try {
$saved = $this
->saveTargetData($component, $langcode, $data);
if ($saved) {
if ($source_status == Lingotek::STATUS_IMPORTING) {
$this
->setSourceStatus($component, Lingotek::STATUS_CURRENT);
}
if ($source_status == Lingotek::STATUS_EDITED) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_EDITED);
}
elseif ($status === TRUE) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_CURRENT);
}
else {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_INTERMEDIATE);
}
}
} catch (\Exception $exception) {
$transaction
->rollBack();
\Drupal::logger('lingotek')
->error('Error happened (unknown) saving %document_id %locale: %message', [
'%document_id' => $document_id,
'%locale' => $locale,
'%message' => $exception
->getMessage(),
]);
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
return FALSE;
}
return TRUE;
}
}
\Drupal::logger('lingotek')
->warning('Error happened trying to download (%component): no document id found.', [
'%component' => $component,
]);
return FALSE;
}
public function updateDocument($component, $job_id = NULL) {
if ($job_id === NULL) {
$job_id = $this
->getJobId($component) ?: NULL;
}
$source_data = $this
->getSourceData($component);
$document_id = $this
->getDocumentId($component);
$document_name = 'Interface translation: ' . $component;
\Drupal::moduleHandler()
->invokeAll('lingotek_interface_translation_document_upload', [
&$source_data,
&$component,
]);
try {
$newDocumentID = $this->lingotek
->updateDocument($document_id, $source_data, NULL, $document_name, NULL, $job_id, $this
->getSourceLocale($component));
} catch (LingotekDocumentLockedException $exception) {
$this
->setDocumentId($component, $exception
->getNewDocumentId());
throw $exception;
} catch (LingotekDocumentArchivedException $exception) {
$this
->setDocumentId($component, NULL);
$this
->deleteMetadata($component);
throw $exception;
} catch (LingotekPaymentRequiredException $exception) {
$this
->setSourceStatus($component, Lingotek::STATUS_ERROR);
throw $exception;
} catch (LingotekApiException $exception) {
$this
->setSourceStatus($component, Lingotek::STATUS_ERROR);
throw $exception;
}
if ($newDocumentID) {
if (is_string($newDocumentID)) {
$document_id = $newDocumentID;
$this
->setDocumentId($component, $newDocumentID);
}
$this
->setSourceStatus($component, Lingotek::STATUS_IMPORTING);
$this
->setTargetStatuses($component, Lingotek::STATUS_PENDING);
$this
->setJobId($component, $job_id);
$this
->setLastUpdated($component, \Drupal::time()
->getRequestTime());
return $document_id;
}
return FALSE;
}
public function downloadDocuments($component) {
if ($document_id = $this
->getDocumentId($component)) {
$source_status = $this
->getSourceStatus($component);
$target_languages = $this->languageManager
->getLanguages();
$target_languages = array_filter($target_languages, function (LanguageInterface $language) {
$configLanguage = ConfigurableLanguage::load($language
->getId());
return $this->lingotekConfiguration
->isLanguageEnabled($configLanguage);
});
$source_langcode = 'en';
foreach ($target_languages as $langcode => $language) {
$locale = $this->languageLocaleMapper
->getLocaleForLangcode($langcode);
if ($langcode !== $source_langcode) {
try {
if ($this->lingotek
->getDocumentTranslationStatus($document_id, $locale) !== FALSE) {
$data = $this->lingotek
->downloadDocument($document_id, $locale);
if ($data) {
$status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
$transaction = $this->connection
->startTransaction();
try {
$saved = $this
->saveTargetData($component, $langcode, $data);
if ($saved) {
if ($source_status == Lingotek::STATUS_IMPORTING) {
$this
->setSourceStatus($component, Lingotek::STATUS_CURRENT);
}
if ($source_status == Lingotek::STATUS_EDITED) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_EDITED);
}
elseif ($status === TRUE) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_CURRENT);
}
else {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_INTERMEDIATE);
}
}
} catch (LingotekApiException $exception) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
throw $exception;
} catch (LingotekContentEntityStorageException $storageException) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
throw $storageException;
} catch (\Exception $exception) {
$transaction
->rollBack();
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
}
}
else {
return NULL;
}
}
} catch (LingotekApiException $exception) {
$this
->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
throw $exception;
}
}
}
}
return FALSE;
}
public function cancelDocument($component) {
$result = FALSE;
$doc_id = $this
->getDocumentId($component);
if ($doc_id) {
$result = $this->lingotek
->cancelDocument($doc_id);
$this
->setDocumentId($component, NULL);
}
$this
->setSourceStatus($component, Lingotek::STATUS_CANCELLED);
$this
->setTargetStatuses($component, Lingotek::STATUS_CANCELLED);
return $result;
}
public function cancelDocumentTarget($component, $locale) {
$source_langcode = 'en';
$source_locale = $this->languageLocaleMapper
->getLocaleForLangcode($source_langcode);
if ($locale == $source_locale) {
return FALSE;
}
if ($document_id = $this
->getDocumentId($component)) {
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($locale);
if ($this->lingotek
->cancelDocumentTarget($document_id, $locale)) {
$this
->setTargetStatus($component, $drupal_language
->id(), Lingotek::STATUS_CANCELLED);
return TRUE;
}
}
return FALSE;
}
public function deleteMetadata($component) {
$doc_id = $this
->getDocumentId($component);
if ($doc_id) {
$this
->cancelDocument($component);
}
$state = \Drupal::state();
$translations_metadata = $state
->get('lingotek.interface_translations_metadata');
if ($translations_metadata) {
unset($translations_metadata[$component]);
$state
->set('lingotek.interface_translations_metadata', $translations_metadata);
}
}
public function deleteAllMetadata() {
$state = \Drupal::state();
$translations_metadata = $state
->get('lingotek.interface_translations_metadata');
if ($translations_metadata) {
foreach ($translations_metadata as $component => $componentMetadata) {
if ($componentMetadata['document_id']) {
$this
->cancelDocument($component);
}
}
}
$state
->delete('lingotek.interface_translations_metadata');
}
public function loadByDocumentId($document_id) {
$state = \Drupal::state();
$translations_metadata = $state
->get('lingotek.interface_translations_metadata');
if ($translations_metadata) {
foreach ($translations_metadata as $component => $componentMetadata) {
if ($componentMetadata['document_id'] === $document_id) {
return $component;
}
}
}
return NULL;
}
public function getAllLocalDocumentIds() {
$state = \Drupal::state();
$translations_metadata = $state
->get('lingotek.interface_translations_metadata');
$docIds = [];
if ($translations_metadata) {
foreach ($translations_metadata as $component => $componentMetadata) {
$docIds[] = $componentMetadata['document_id'];
}
}
return $docIds;
}
public function saveTargetData($component, $langcode, $data) {
$customized = TRUE;
$overwrite_options['customized'] = TRUE;
$overwrite_options['not_customized'] = FALSE;
foreach ($data as $sourceData => $translationData) {
$context = $translationData['_context'];
unset($translationData['_context']);
if (count($translationData) == 1) {
$keys = array_keys($translationData);
$source = reset($keys);
$translation = reset($translationData);
}
else {
$keys = array_keys($translationData);
$source = implode(PoItem::DELIMITER, $keys);
$translation = implode(PoItem::DELIMITER, $translationData);
}
$strings = \Drupal::service('locale.storage')
->getTranslations([
'language' => $langcode,
'source' => $source,
'context' => $context,
]);
$string = reset($strings);
if (!empty($translation)) {
if (!locale_string_is_safe($translation)) {
\Drupal::logger('lingotek')
->error('Import of string "%string" was skipped because of disallowed or malformed HTML.', [
'%string' => $translation,
]);
}
elseif ($string) {
$string
->setString($translation);
if ($string
->isNew()) {
$string
->setValues([
'language' => $langcode,
'customized' => $customized,
]);
$string
->save();
}
elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
$string->customized = $customized;
$string
->save();
}
}
else {
$string = \Drupal::service('locale.storage')
->createString([
'source' => $source,
'context' => $context,
])
->save();
\Drupal::service('locale.storage')
->createTranslation([
'lid' => $string
->getId(),
'language' => $langcode,
'translation' => $translation,
'customized' => $customized,
])
->save();
}
}
}
return $component;
}
public function getJobId($component) {
$job_id = NULL;
$metadata = $this
->getMetadata($component);
if (!empty($metadata) && !empty($metadata['job_id'])) {
$job_id = $metadata['job_id'];
}
return $job_id;
}
public function setJobId($component, $job_id, $update_tms = FALSE) {
$metadata = $this
->getMetadata($component);
$newDocumentID = FALSE;
if ($update_tms && ($document_id = $this
->getDocumentId($component))) {
try {
$newDocumentID = $this->lingotek
->updateDocument($document_id, NULL, NULL, NULL, NULL, $job_id);
} catch (LingotekDocumentLockedException $exception) {
$this
->setDocumentId($component, $exception
->getNewDocumentId());
throw $exception;
} catch (LingotekDocumentArchivedException $exception) {
$old_job_id = $this
->getJobId($component);
$this
->deleteMetadata($component);
$metadata = $this
->getMetadata($component);
$metadata['job_id'] = $old_job_id;
$this
->saveMetadata($component, $metadata);
throw $exception;
} catch (LingotekPaymentRequiredException $exception) {
throw $exception;
} catch (LingotekApiException $exception) {
throw $exception;
}
}
if (is_string($newDocumentID)) {
$metadata['document_id'] = $newDocumentID;
}
$metadata['job_id'] = $job_id;
$this
->saveMetadata($component, $metadata);
return $component;
}
protected function extractPotxStrings($component) {
global $_potx_strings;
$this->moduleHandler
->loadInclude('potx', 'inc');
$this->moduleHandler
->loadInclude('potx', 'inc', 'potx.local');
potx_status('set', POTX_STATUS_MESSAGE);
$pathinfo = pathinfo($component);
if (!isset($pathinfo['filename'])) {
$pathinfo['filename'] = substr($pathinfo['basename'], 0, strrpos($pathinfo['basename'], '.'));
}
if (isset($pathinfo['extension'])) {
potx_local_init($pathinfo['dirname']);
$files = _potx_explore_dir($pathinfo['dirname'] . '/', $pathinfo['filename']);
$strip_prefix = 1 + strlen($pathinfo['dirname']);
}
else {
potx_local_init($component);
$files = _potx_explore_dir($component . '/');
$strip_prefix = 1 + strlen($component);
}
foreach ($files as $file) {
_potx_process_file($file, $strip_prefix);
}
potx_finish_processing('_potx_save_string');
return $_potx_strings;
}
public function setLastUploaded($component, int $timestamp) {
$metadata = $this
->getMetadata($component);
$metadata['uploaded_timestamp'] = $timestamp;
$this
->saveMetadata($component, $metadata);
return $component;
}
public function setLastUpdated($component, int $timestamp) {
$metadata = $this
->getMetadata($component);
$metadata['updated_timestamp'] = $timestamp;
$this
->saveMetadata($component, $metadata);
return $component;
}
public function getLastUploaded($component) {
$metadata = $this
->getMetadata($component);
return isset($metadata['uploaded_timestamp']) ? $metadata['uploaded_timestamp'] : NULL;
}
public function getUpdatedTime($component) {
$metadata = $this
->getMetadata($component);
return isset($metadata['updated_timestamp']) ? $metadata['updated_timestamp'] : NULL;
}
}