class TranslateCss in Advanced CSS/JS Aggregation 8.4
Same name and namespace in other branches
- 8.3 advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss
Applies the t() function to strings in CSS assets.
Hierarchy
- class \Drupal\advagg\Asset\SingleAssetOptimizerBase
- class \Drupal\advagg_mod\Asset\TranslateCss uses StringTranslationTrait
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
File
- advagg_mod/
src/ Asset/ TranslateCss.php, line 13
Namespace
Drupal\advagg_mod\AssetView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SingleAssetOptimizerBase:: |
protected | property | A config object for optimizer. | |
SingleAssetOptimizerBase:: |
protected | property | Logger service. | |
SingleAssetOptimizerBase:: |
public | function | If configured, add licence string to top/bottom of file. | |
SingleAssetOptimizerBase:: |
protected | function | Check if minification was successful before saving changes. | |
SingleAssetOptimizerBase:: |
protected | function | Check if the asset is already minified. | |
SingleAssetOptimizerBase:: |
protected | function | Checks if string contains multibyte characters. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TranslateCss:: |
public | function |
Optimize the asset's content. Overrides SingleAssetOptimizerBase:: |
|
TranslateCss:: |
protected | function | Run preg matches through the t() function. | |
TranslateCss:: |
public | function |
Construct the optimizer instance. Overrides SingleAssetOptimizerBase:: |