Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TranslationFilesTest.php
Go to the documentation of this file.
1 <?php
7 
13 
18 {
24  protected $context;
25 
36  public function testDefaultLocale($file, $phrases)
37  {
38  $this->markTestSkipped('MAGETWO-26083');
39  $failures = $this->comparePhrase($phrases, $this->csvParser->getDataPairs($file));
40  $this->assertEmpty(
41  $failures,
42  $this->printMessage([$file => $failures])
43  );
44  }
45 
50  public function defaultLocaleDataProvider()
51  {
52  $parser = $this->prepareParser();
53 
54  $optionResolverFactory = new ResolverFactory();
55  $optionResolver = $optionResolverFactory->create(BP, true);
56 
57  $parser->parse($optionResolver->getOptions());
58 
59  $defaultLocale = [];
60  foreach ($parser->getPhrases() as $key => $phrase) {
61  if (!$phrase->getContextType() || !$phrase->getContextValue()) {
62  throw new \RuntimeException(sprintf('Missed context in row #%d.', $key + 1));
63  }
64  foreach ($phrase->getContextValue() as $context) {
65  $phraseText = $this->eliminateSpecialChars($phrase->getPhrase());
66  $phraseTranslation = $this->eliminateSpecialChars($phrase->getTranslation());
67  $file = $this->buildFilePath($phrase, $context);
68  $defaultLocale[$file]['file'] = $file;
69  $defaultLocale[$file]['phrases'][$phraseText] = $phraseTranslation;
70  }
71  }
72  return $defaultLocale;
73  }
74 
80  protected function buildFilePath($phrase, $context)
81  {
82  $path = $this->getContext()->buildPathToLocaleDirectoryByContext($phrase->getContextType(), $context);
84  }
85 
89  protected function getContext()
90  {
91  if ($this->context === null) {
92  $this->context = new \Magento\Setup\Module\I18n\Context(new ComponentRegistrar());
93  }
94  return $this->context;
95  }
96 
100  protected function prepareParser()
101  {
102  $filesCollector = new \Magento\Setup\Module\I18n\FilesCollector();
103 
104  $phraseCollector = new \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector(
105  new \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer()
106  );
107  $adapters = [
108  'php' => new \Magento\Setup\Module\I18n\Parser\Adapter\Php($phraseCollector),
109  'js' => new \Magento\Setup\Module\I18n\Parser\Adapter\Js(),
110  'xml' => new \Magento\Setup\Module\I18n\Parser\Adapter\Xml(),
111  'html' => new \Magento\Setup\Module\I18n\Parser\Adapter\Html(),
112  ];
113 
114  $parserContextual = new \Magento\Setup\Module\I18n\Parser\Contextual(
115  $filesCollector,
116  new \Magento\Setup\Module\I18n\Factory(),
117  new \Magento\Setup\Module\I18n\Context(new ComponentRegistrar())
118  );
119  foreach ($adapters as $type => $adapter) {
120  $parserContextual->addAdapter($type, $adapter);
121  }
122 
123  return $parserContextual;
124  }
125 
130  protected function eliminateSpecialChars($text)
131  {
132  return preg_replace(['/\\\\\'/', '/\\\\\\\\/'], ['\'', '\\'], $text);
133  }
134 
142  public function testPhrasePlaceHolders($placePath)
143  {
144  $this->markTestSkipped('MAGETWO-26083');
145  $files = $this->getCsvFiles($placePath);
146 
147  $failures = [];
148  foreach ($files as $locale => $file) {
149  $fileData = $this->csvParser->getDataPairs($file);
150  foreach ($fileData as $key => $translate) {
151  preg_match_all('/%(\d+)/', $key, $keyMatches);
152  preg_match_all('/%(\d+)/', $translate, $translateMatches);
153  if (count(array_unique($keyMatches[1])) != count(array_unique($translateMatches[1]))) {
154  $failures[$locale][$key][] = $translate;
155  }
156  }
157  }
158  $this->assertEmpty(
159  $failures,
160  $this->printMessage(
161  $failures,
162  'Found discrepancy between keys and translations in count of numeric placeholders'
163  )
164  );
165  }
166 }
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$adapter
Definition: webapi_user.php:16
$type
Definition: item.phtml:13
const BP
Definition: autoload.php:14
foreach($appDirs as $dir) $files