View source
<?php
namespace Drupal\locale\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\locale\StringStorageInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class TranslateFormBase extends FormBase {
protected $localeStorage;
protected $state;
protected $languageManager;
protected static $filterValues;
public function __construct(StringStorageInterface $locale_storage, StateInterface $state, LanguageManagerInterface $language_manager) {
$this->localeStorage = $locale_storage;
$this->state = $state;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('locale.storage'), $container
->get('state'), $container
->get('language_manager'));
}
protected function translateFilterLoadStrings() {
$filter_values = $this
->translateFilterValues();
$conditions = [
'language' => $filter_values['langcode'],
];
$options = [
'pager limit' => 30,
'translated' => TRUE,
'untranslated' => TRUE,
];
switch ($filter_values['translation']) {
case 'translated':
$conditions['translated'] = TRUE;
if ($filter_values['customized'] != 'all') {
$conditions['customized'] = $filter_values['customized'];
}
break;
case 'untranslated':
$conditions['translated'] = FALSE;
break;
}
if (!empty($filter_values['string'])) {
$options['filters']['source'] = $filter_values['string'];
if ($options['translated']) {
$options['filters']['translation'] = $filter_values['string'];
}
}
return $this->localeStorage
->getTranslations($conditions, $options);
}
protected function translateFilterValues($reset = FALSE) {
if (!$reset && static::$filterValues) {
return static::$filterValues;
}
$filter_values = [];
$filters = $this
->translateFilters();
$request = $this
->getRequest();
$session_filters = $request
->getSession()
->get('locale_translate_filter', []);
foreach ($filters as $key => $filter) {
$filter_values[$key] = $filter['default'];
if ($request->query
->has($key)) {
$value = $request->query
->get($key);
if (!isset($filter['options']) || isset($filter['options'][$value])) {
$filter_values[$key] = $value;
}
}
elseif (isset($session_filters[$key])) {
if (!isset($filter['options']) || isset($filter['options'][$session_filters[$key]])) {
$filter_values[$key] = $session_filters[$key];
}
}
}
return static::$filterValues = $filter_values;
}
protected function translateFilters() {
$filters = [];
$this->languageManager
->reset();
$languages = $this->languageManager
->getLanguages();
$language_options = [];
foreach ($languages as $langcode => $language) {
if (locale_is_translatable($langcode)) {
$language_options[$langcode] = $language
->getName();
}
}
$default_langcode = $this->languageManager
->getCurrentLanguage()
->getId();
if (!isset($language_options[$default_langcode])) {
$available_langcodes = array_keys($language_options);
$default_langcode = array_shift($available_langcodes);
}
$filters['string'] = [
'title' => $this
->t('String contains'),
'description' => $this
->t('Leave blank to show all strings. The search is case sensitive.'),
'default' => '',
];
$filters['langcode'] = [
'title' => $this
->t('Translation language'),
'options' => $language_options,
'default' => $default_langcode,
];
$filters['translation'] = [
'title' => $this
->t('Search in'),
'options' => [
'all' => $this
->t('Both translated and untranslated strings'),
'translated' => $this
->t('Only translated strings'),
'untranslated' => $this
->t('Only untranslated strings'),
],
'default' => 'all',
];
$filters['customized'] = [
'title' => $this
->t('Translation type'),
'options' => [
'all' => $this
->t('All'),
LOCALE_NOT_CUSTOMIZED => $this
->t('Non-customized translation'),
LOCALE_CUSTOMIZED => $this
->t('Customized translation'),
],
'states' => [
'visible' => [
':input[name=translation]' => [
'value' => 'translated',
],
],
],
'default' => 'all',
];
return $filters;
}
}