15 class Parser implements \Magento\Framework\Translate\Inline\ParserInterface
56 'legend' =>
'Caption for the fieldset element',
57 'label' =>
'Label for an input element.',
58 'button' =>
'Push button',
61 'strong' =>
'Strong emphasized text',
63 'em' =>
'Emphasized text',
64 'u' =>
'Underlined text',
65 'sup' =>
'Superscript text',
66 'sub' =>
'Subscript text',
67 'span' =>
'Span element',
68 'small' =>
'Smaller text',
69 'big' =>
'Bigger text',
70 'address' =>
'Contact information',
71 'blockquote' =>
'Long quotation',
72 'q' =>
'Short quotation',
74 'caption' =>
'Table caption',
75 'abbr' =>
'Abbreviated phrase',
76 'acronym' =>
'An acronym',
77 'var' =>
'Variable part of a text',
79 'strike' =>
'Strikethrough text',
80 'del' =>
'Deleted text',
81 'ins' =>
'Inserted text',
82 'h1' =>
'Heading level 1',
83 'h2' =>
'Heading level 2',
84 'h3' =>
'Heading level 3',
85 'h4' =>
'Heading level 4',
86 'h5' =>
'Heading level 5',
87 'h6' =>
'Heading level 6',
88 'center' =>
'Centered text',
89 'select' =>
'List options',
91 'input' =>
'Form element',
127 private $cacheManager;
132 private $relatedCacheTypes;
139 private function getCacheManger()
146 return $this->cacheManager;
165 \
Magento\Framework\
App\Cache\TypeListInterface $appCache,
166 \
Magento\Framework\Translate\InlineInterface $translateInline,
167 array $relatedCacheTypes = []
171 $this->_inputFilter = $inputFilter;
172 $this->_appState = $appState;
173 $this->_appCache = $appCache;
174 $this->_translateInline = $translateInline;
175 $this->relatedCacheTypes = $relatedCacheTypes;
186 if (!$this->_translateInline->isAllowed()) {
187 return [
'inline' =>
'not allowed'];
189 if (!empty($this->relatedCacheTypes)) {
190 $this->_appCache->invalidate($this->relatedCacheTypes);
197 $validStoreId = $this->_storeManager->getStore()->getId();
200 $resource = $this->_resourceFactory->create();
201 foreach ($translateParams as $param) {
205 if (empty($param[
'perstore'])) {
206 $resource->deleteTranslate($param[
'original'],
null,
false);
215 return $this->getCacheManger()->updateAndGetTranslations();
227 foreach ($translateParams as $param) {
228 if (!is_array($param) || !isset($param[
'original']) || !isset($param[
'custom'])) {
229 throw new \InvalidArgumentException(
230 'Both original and custom phrases are required for inline translation.' 245 foreach ($translateParams as &$param) {
246 foreach ($fieldNames as $fieldName) {
247 $param[$fieldName] = $this->_inputFilter->filter($param[$fieldName]);
260 $this->_content = $body;
262 $this->_specialTags();
263 $this->_tagAttributes();
298 $this->_isJson = $flag;
313 return 'Tag attribute (ALT, TITLE, etc.)';
326 $tagName = strtolower(
$options[
'tagName']);
328 if (isset(
$options[
'tagList'][$tagName])) {
329 return $options[
'tagList'][$tagName];
332 return ucfirst($tagName) .
' Text';
345 $specialTags = $tagHtml .
'<span class="translate-inline-' . $tagName .
'" ' . $this->_getHtmlAttribute(
346 self::DATA_TRANSLATE,
347 '[' . htmlspecialchars(join(
',', $trArr)) .
']' 350 if ($additionalAttr !==
null) {
351 $specialTags .=
' ' . $additionalAttr .
'>';
353 $specialTags .=
'>' . strtoupper($tagName);
355 $specialTags .=
'</span>';
369 $simpleTags = substr(
373 ) .
' ' . $this->_getHtmlAttribute(
374 self::DATA_TRANSLATE,
375 htmlspecialchars(
'[' . join(
',', $trArr) .
']')
378 if ($additionalAttr !==
null) {
379 $simpleTags .=
' ' . $additionalAttr;
381 $simpleTags .= substr($tagHtml, strlen($tagName) + 1);
394 private function _getTranslateData($regexp, &
$text, $locationCallback,
$options = [])
398 while (preg_match($regexp,
$text, $matches, PREG_OFFSET_CAPTURE, $next)) {
399 $trArr[] = json_encode(
401 'shown' => htmlspecialchars_decode($matches[1][0]),
402 'translated' => htmlspecialchars_decode($matches[2][0]),
403 'original' => htmlspecialchars_decode($matches[3][0]),
407 $text = substr_replace(
$text, $matches[1][0], $matches[0][1], strlen($matches[0][0]));
408 $next = $matches[0][1];
418 private function _tagAttributes()
420 $this->_prepareTagAttributesForContent($this->_content);
429 private function _prepareTagAttributesForContent(&
$content)
431 $quoteHtml = $this->_getHtmlQuote();
434 $tagRegExp =
'#<([a-z]+)\s*?[^>]+?((' . self::REGEXP_TOKEN .
')[^>]*?)+\\\\?/?>#iS';
435 while (preg_match($tagRegExp,
$content, $tagMatch, PREG_OFFSET_CAPTURE, $nextTag)) {
436 $tagHtml = $tagMatch[0][0];
438 $attrRegExp =
'#' . self::REGEXP_TOKEN .
'#S';
439 $trArr = $this->_getTranslateData($attrRegExp, $tagHtml, [$this,
'_getAttributeLocation']);
441 $transRegExp =
'# ' . $this->_getHtmlAttribute(
442 self::DATA_TRANSLATE,
443 '\[([^' . preg_quote($quoteHtml) .
']*)]' 445 if (preg_match($transRegExp, $tagHtml, $matches)) {
446 $tagHtml = str_replace($matches[0],
'', $tagHtml);
447 $trAttr =
' ' . $this->_getHtmlAttribute(
448 self::DATA_TRANSLATE,
449 '[' . htmlspecialchars($matches[1]) .
',' . str_replace(
"\"",
"'", join(
',', $trArr)) .
']' 452 $trAttr =
' ' . $this->_getHtmlAttribute(
453 self::DATA_TRANSLATE,
454 '[' . str_replace(
"\"",
"'", join(
',', $trArr)) .
']' 457 $trAttr = $this->_addTranslateAttribute($trAttr);
459 $tagHtml = substr_replace($tagHtml, $trAttr, strlen($tagMatch[1][0]) + 1, 1);
460 $content = substr_replace(
$content, $tagHtml, $tagMatch[0][1], strlen($tagMatch[0][0]));
462 $nextTag = $tagMatch[0][1] + strlen($tagHtml);
475 return $name .
'=' . $this->_getHtmlQuote() .
$value . $this->_getHtmlQuote();
484 private function _addTranslateAttribute($trAttr)
486 $translateAttr = $trAttr;
488 if ($additionalAttr !==
null) {
489 $translateAttr .=
' ' . $additionalAttr .
' ';
491 return $translateAttr;
499 private function _getHtmlQuote()
501 if ($this->_isJson) {
513 private function _specialTags()
515 $this->_translateTags($this->_content, $this->_allowedTagsGlobal,
'_applySpecialTagsFormat');
516 $this->_translateTags($this->_content, $this->_allowedTagsSimple,
'_applySimpleTagsFormat');
527 private function _translateTags(&
$content, $tagsList, $formatCallback)
530 $tagRegExpBody =
'#<(body)(/?>| \s*[^>]*+/?>)#iSU';
532 $tags = implode(
'|', array_keys($tagsList));
533 $tagRegExp =
'#<(' . $tags .
')(/?>| \s*[^>]*+/?>)#iSU';
535 $headTranslateTags =
'';
536 while (preg_match($tagRegExp,
$content, $tagMatch, PREG_OFFSET_CAPTURE, $nextTag)) {
537 $tagName = strtolower($tagMatch[1][0]);
538 if (substr($tagMatch[0][0], -2) ==
'/>') {
539 $tagClosurePos = $tagMatch[0][1] + strlen($tagMatch[0][0]);
541 $tagClosurePos = $this->_findEndOfTag(
$content, $tagName, $tagMatch[0][1]);
544 if ($tagClosurePos ===
false) {
545 $nextTag += strlen($tagMatch[0][0]);
549 $tagLength = $tagClosurePos - $tagMatch[0][1];
551 $tagStartLength = strlen($tagMatch[0][0]);
553 $tagHtml = $tagMatch[0][0] . substr(
555 $tagMatch[0][1] + $tagStartLength,
556 $tagLength - $tagStartLength
558 $tagClosurePos = $tagMatch[0][1] + strlen($tagHtml);
560 $trArr = $this->_getTranslateData(
561 '#' . self::REGEXP_TOKEN .
'#iS',
563 [$this,
'_getTagLocation'],
564 [
'tagName' => $tagName,
'tagList' => $tagsList]
567 if (!empty($trArr)) {
568 $trArr = array_unique($trArr);
571 preg_match($tagRegExpBody,
$content, $tagBodyMatch, PREG_OFFSET_CAPTURE);
572 if (!empty($tagBodyMatch)) {
573 $tagBodyOpenStartPosition = $tagBodyMatch[0][1];
575 if (array_key_exists($tagName, $this->_allowedTagsGlobal)
576 && $tagBodyOpenStartPosition > $tagMatch[0][1]
578 $tagHtmlHead =
call_user_func([$this, $formatCallback], $tagHtml, $tagName, $trArr);
579 $headTranslateTags .= substr($tagHtmlHead, strlen($tagHtml));
581 $tagHtml =
call_user_func([$this, $formatCallback], $tagHtml, $tagName, $trArr);
585 $tagClosurePos = $tagMatch[0][1] + strlen($tagHtml);
588 $nextTag = $tagClosurePos;
590 if ($headTranslateTags) {
592 preg_match($tagRegExpBody,
$content, $tagBodyMatch, PREG_OFFSET_CAPTURE);
593 $tagBodyOpenStartPosition = $tagBodyMatch[0][1];
594 $openTagBodyEndPosition = $tagBodyOpenStartPosition + strlen($tagBodyMatch[0][0]);
597 . substr(
$content, $openTagBodyEndPosition);
609 private function _findEndOfTag($body, $tagName, $from)
611 $openTag =
'<' . $tagName;
612 $closeTag = ($this->_isJson ?
'<\\/' :
'</') . $tagName;
613 $tagLength = strlen($tagName);
614 $length = $tagLength + 1;
616 while (substr_count($body, $openTag, $from, $length) !== substr_count($body, $closeTag, $from, $length)) {
617 $end = strpos($body, $closeTag, $end + $tagLength + 1);
618 if ($end ===
false) {
621 $length = $end - $from + $tagLength + 3;
623 if (preg_match(
'#<\\\\?\/' . $tagName .
'\s*?>#i', $body, $tagMatch,
null, $end)) {
624 return $end + strlen($tagMatch[0]);
635 private function _otherText()
639 while (preg_match(
'#' . self::REGEXP_TOKEN .
'#', $this->_content, $matches, PREG_OFFSET_CAPTURE, $next)) {
640 $translateProperties = json_encode(
642 'shown' => $matches[1][0],
643 'translated' => $matches[2][0],
644 'original' => $matches[3][0],
645 'location' =>
'Text',
646 'scope' => $matches[4][0],
652 '[' . htmlspecialchars($translateProperties) .
']',
655 $this->_content = substr_replace($this->_content, $spanHtml, $matches[0][1], strlen($matches[0][0]));
656 $next = $matches[0][1] + strlen($spanHtml) - 1;
669 $translateSpan =
'<span ' . $this->_getHtmlAttribute(self::DATA_TRANSLATE,
$data);
671 if ($additionalAttr !==
null) {
672 $translateSpan .=
' ' . $additionalAttr;
674 $translateSpan .=
'>' .
$text .
'</span>';
675 return $translateSpan;
686 return $this->_translateInline->getAdditionalHtmlAttribute($tagName);
_getTagLocation($matches, $options)
processAjaxPost(array $translateParams)
_validateTranslationParams(array $translateParams)
call_user_func($callable, $param)
processResponseBodyString($body)
_getDataTranslateSpan($data, $text)
_filterTranslationParams(array &$translateParams, array $fieldNames)
_applySimpleTagsFormat($tagHtml, $tagName, $trArr)
_applySpecialTagsFormat($tagHtml, $tagName, $trArr)
_getAttributeLocation($matches, $options)
__construct(\Magento\Translation\Model\ResourceModel\StringUtilsFactory $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Zend_Filter_Interface $inputFilter, \Magento\Framework\App\State $appState, \Magento\Framework\App\Cache\TypeListInterface $appCache, \Magento\Framework\Translate\InlineInterface $translateInline, array $relatedCacheTypes=[])
_getAdditionalHtmlAttribute($tagName=null)
if(!isset($_GET['name'])) $name