Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PathTranslationParser.php
Go to the documentation of this file.
1 <?php
2 
4 
9 abstract class PathTranslationParser implements Parser
10 {
15  protected $pathPrefixVariants = array('', './');
16 
21  protected $pathPrefixTranslations = array();
22 
23  protected $pathSuffix;
24 
30  public function __construct($translations, $pathSuffix)
31  {
32  $this->pathPrefixTranslations = $this->createPrefixVariants($translations);
33  $this->pathSuffix = $pathSuffix;
34  }
35 
44  protected function createPrefixVariants($translations)
45  {
46  $newTranslations = array();
47  foreach($translations as $key => $value) {
48  foreach($this->pathPrefixVariants as $variant) {
49  $newTranslations[$variant.$key] = $value;
50  }
51  }
52 
53  return $newTranslations;
54  }
55 
68  public function translatePathMappings($mappings)
69  {
70  // each element of $mappings is an array with two elements; first is
71  // the source and second is the target
72  foreach($mappings as &$mapping) {
73  foreach($this->pathPrefixTranslations as $prefix => $translate) {
74  if(strpos($mapping[1], $prefix) === 0) {
75  // replace the old prefix with the translated version
76  $mapping[1] = $translate . substr($mapping[1], strlen($prefix));
77  // should never need to translate a prefix more than once
78  // per path mapping
79  break;
80  }
81  }
82  //Adding path Suffix to the mapping info.
83  $mapping[1] = $this->pathSuffix . $mapping[1];
84  }
85  return $mappings;
86  }
87 }
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16