You are here

class TranslateCss in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss

Applies the t() function to strings in CSS assets.

Hierarchy

Expanded class hierarchy of TranslateCss

1 file declares its use of TranslateCss
InitSubscriber.php in advagg_mod/src/EventSubscriber/InitSubscriber.php
1 string reference to 'TranslateCss'
advagg_mod.services.yml in advagg_mod/advagg_mod.services.yml
advagg_mod/advagg_mod.services.yml
1 service uses TranslateCss
advagg_mod.translate_css in advagg_mod/advagg_mod.services.yml
Drupal\advagg_mod\Asset\TranslateCss

File

advagg_mod/src/Asset/TranslateCss.php, line 13

Namespace

Drupal\advagg_mod\Asset
View source
class TranslateCss extends SingleAssetOptimizerBase {
  use StringTranslationTrait;

  /**
   * Construct the optimizer instance.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   */
  public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
    parent::__construct($logger);
    $this->config = $config_factory
      ->get('advagg_mod.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function optimize($contents, array $asset, array $data) {

    // Code taken from \Drupal\Core\Asset\CssOptimizer::processCss().
    // Regexp to match double quoted strings.
    $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';

    // Regexp to match single quoted strings.
    $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";

    // Extract all content inside of quotes.
    $pattern = "/content:.*?({$double_quot}|{$single_quot}|(?:\\;|\\})).*?(?:\\;|\\})/";

    // Run strings inside of quotes of the content attribute through the t
    // function.
    return preg_replace_callback($pattern, [
      $this,
      'translateCallback',
    ], $contents);
  }

  /**
   * Run preg matches through the t() function.
   *
   * @param array $matches
   *   Array of matches from preg_replace_callback().
   *
   * @return string
   *   Replaced string.
   */
  protected function translateCallback(array $matches) {

    // Skip if equal to ; or }.
    if ($matches[1] === ';' || $matches[1] === '}') {
      return $matches[0];
    }

    // Remove quotes for t function.
    $before = substr($matches[1], 1, -1);

    // Only run if it contains A-Za-z.
    if (!preg_match('/[A-Za-z]/', $before)) {
      return $matches[0];
    }

    // Only run if it contains characters other than unicode.
    $css_unicode_pattern = '/\\\\[0-9a-fA-F]{1,6}(?:\\r\\n|[ \\t\\r\\n\\f])?/';
    $unicode_removed = preg_replace($css_unicode_pattern, '', $before);
    if (empty($unicode_removed)) {
      return $matches[0];
    }

    // Run t function and put back into string.
    return str_replace($before, (string) $this
      ->t('@before', [
      '@before' => $before,
    ]), $matches[0]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SingleAssetOptimizerBase::$config protected property A config object for optimizer.
SingleAssetOptimizerBase::$logger protected property Logger service.
SingleAssetOptimizerBase::addLicense public function If configured, add licence string to top/bottom of file.
SingleAssetOptimizerBase::isMinificationSuccess protected function Check if minification was successful before saving changes.
SingleAssetOptimizerBase::isMinified protected function Check if the asset is already minified.
SingleAssetOptimizerBase::stringContainsMultibyteCharacters protected function Checks if string contains multibyte characters.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TranslateCss::optimize public function Optimize the asset's content. Overrides SingleAssetOptimizerBase::optimize
TranslateCss::translateCallback protected function Run preg matches through the t() function.
TranslateCss::__construct public function Construct the optimizer instance. Overrides SingleAssetOptimizerBase::__construct