Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Generator.php
Go to the documentation of this file.
1 <?php
7 
11 
15 class Generator
16 {
22  protected $dictionaryLoader;
23 
29  protected $packWriter;
30 
36  protected $factory;
37 
45  public function __construct(
46  Dictionary\Loader\FileInterface $dictionaryLoader,
49  ) {
50  $this->dictionaryLoader = $dictionaryLoader;
51  $this->packWriter = $packWriter;
52  $this->factory = $factory;
53  }
54 
65  public function generate(
66  $dictionaryPath,
67  $locale,
69  $allowDuplicates = false
70  ) {
71  $locale = $this->factory->createLocale($locale);
72  $dictionary = $this->dictionaryLoader->load($dictionaryPath);
73 
74  $phrases = $dictionary->getPhrases();
75  if (!is_array($phrases) || !count($phrases)) {
76  throw new \UnexpectedValueException('No phrases have been found by the specified path.');
77  }
78 
79  if (!$allowDuplicates && ($duplicates = $dictionary->getDuplicates())) {
80  throw new \RuntimeException(
81  "Duplicated translation is found, but it is not allowed.\n"
82  . $this->createDuplicatesPhrasesError($duplicates)
83  );
84  }
85 
86  $this->packWriter->writeDictionary($dictionary, $locale, $mode);
87  }
88 
95  protected function createDuplicatesPhrasesError($duplicates)
96  {
97  $error = '';
98  foreach ($duplicates as $phrases) {
100  $phrase = $phrases[0];
101  $error .= sprintf(
102  "The phrase \"%s\" is translated in %d places.\n",
103  $phrase->getPhrase(),
104  count($phrases)
105  );
106  }
107  return $error;
108  }
109 }
__construct(Dictionary\Loader\FileInterface $dictionaryLoader, Pack\WriterInterface $packWriter, Factory $factory)
Definition: Generator.php:45
generate( $dictionaryPath, $locale, $mode=WriterInterface::MODE_REPLACE, $allowDuplicates=false)
Definition: Generator.php:65
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15