locale_override-8.x-1.1/locale_override.module

locale_override.module
<?php

/**
 * @file
 * Stores translated user interface strings in a config entity.
 */

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function locale_override_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'locale_override.settings':
      return '<p>' . t('Allows translated strings to be overridden and replaced with strings stored in a translatable configuration entity.') . '</p>';

    case 'locale_override.import':
      return '<p>' . t('Allows translated strings to be imported and stored in a translatable configuration entity.') . '</p>';
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function locale_override_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface $cacheability) {
  if (isset($data['tabs'])
    && isset($data['tabs'][0])
    && isset($data['tabs'][0]['config_translation.local_tasks:config_translation.item.overview.locale_override.settings'])) {
    $data['tabs'][0]['config_translation.local_tasks:config_translation.item.overview.locale_override.settings']['#link']['title'] = t('Translate');
  }
}

/**
 * Implements hook_form_alter().
 */
function locale_override_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, ['config_translation_add_form', 'config_translation_edit_form'])
    && isset($form['config_names']['locale_override.settings'])) {
    // Convert details to simple <div> containers.
    $elements =& $form['config_names']['locale_override.settings']['strings'];
    foreach (Element::children($elements) as $child_key) {
      $elements[$child_key]['#type'] = 'container';
      unset($elements[$child_key]['#open']);

      $elements[$child_key]['translation']['source']['#title'] = t('Source');

      $context = \Drupal::configFactory()->get('locale_override.settings')->get('strings.' . $child_key .'.context');
      if ($context) {
        $elements[$child_key]['translation']['translation']['#description'] = t('Context: @context', ['@context' => $context]);
      }
    }
    // Add submit handler to clear cached local override strings.
    $form['#submit'][] = '_locale_override_form_config_translate_add_form_submit';
  }
}

/**
 * Validate callback; Validates and cleanups locale_override elements.
 */
function _locale_override_form_config_translate_add_form_submit(&$form, FormStateInterface $form_state) {
  // Clear cache.
  Cache::invalidateTags(['local_override']);
}

/**
 * Implements hook_page_attachments().
 */
function locale_override_page_attachments(array &$attachments) {
  $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $cid = 'locale_overrides:javascript.' . $langcode;
  if ($cache = \Drupal::cache()->get($cid)) {
    $translations = $cache->data;
  }
  else {
    $strings = \Drupal::config('locale_override.settings')->get('strings') ?: [];
    $translations = [];
    foreach ($strings as $string) {
      if ($string['source'] !== $string['translation']
        && $string['javascript']) {
        $translations[$string['context']][$string['source']] = $string['translation'];
      }
    }
    \Drupal::cache()->set($cid, $translations, Cache::PERMANENT, ['local_override']);
  }

  if ($translations) {
    $attachments['#attached']['library'][] = 'locale_override/locale_override';
    $attachments['#attached']['drupalSettings']['locale_override']['strings'] = $translations;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc