1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Module\I18n\Pack;
use Magento\Setup\Module\I18n\Dictionary;
use Magento\Setup\Module\I18n\Factory;
use Magento\Setup\Module\I18n\Pack;
/**
* Pack generator
*/
class Generator
{
/**
* Dictionary loader
*
* @var \Magento\Setup\Module\I18n\Dictionary\Loader\FileInterface
*/
protected $dictionaryLoader;
/**
* Pack writer
*
* @var \Magento\Setup\Module\I18n\Pack\WriterInterface
*/
protected $packWriter;
/**
* Domain abstract factory
*
* @var \Magento\Setup\Module\I18n\Factory
*/
protected $factory;
/**
* Loader construct
*
* @param \Magento\Setup\Module\I18n\Dictionary\Loader\FileInterface $dictionaryLoader
* @param \Magento\Setup\Module\I18n\Pack\WriterInterface $packWriter
* @param \Magento\Setup\Module\I18n\Factory $factory
*/
public function __construct(
Dictionary\Loader\FileInterface $dictionaryLoader,
Pack\WriterInterface $packWriter,
Factory $factory
) {
$this->dictionaryLoader = $dictionaryLoader;
$this->packWriter = $packWriter;
$this->factory = $factory;
}
/**
* Generate language pack
*
* @param string $dictionaryPath
* @param string $locale
* @param string $mode One of const of WriterInterface::MODE_
* @param bool $allowDuplicates
* @return void
* @throws \RuntimeException
*/
public function generate(
$dictionaryPath,
$locale,
$mode = WriterInterface::MODE_REPLACE,
$allowDuplicates = false
) {
$locale = $this->factory->createLocale($locale);
$dictionary = $this->dictionaryLoader->load($dictionaryPath);
$phrases = $dictionary->getPhrases();
if (!is_array($phrases) || !count($phrases)) {
throw new \UnexpectedValueException('No phrases have been found by the specified path.');
}
if (!$allowDuplicates && ($duplicates = $dictionary->getDuplicates())) {
throw new \RuntimeException(
"Duplicated translation is found, but it is not allowed.\n"
. $this->createDuplicatesPhrasesError($duplicates)
);
}
$this->packWriter->writeDictionary($dictionary, $locale, $mode);
}
/**
* Get duplicates error
*
* @param array $duplicates
* @return string
*/
protected function createDuplicatesPhrasesError($duplicates)
{
$error = '';
foreach ($duplicates as $phrases) {
/** @var \Magento\Setup\Module\I18n\Dictionary\Phrase $phrase */
$phrase = $phrases[0];
$error .= sprintf(
"The phrase \"%s\" is translated in %d places.\n",
$phrase->getPhrase(),
count($phrases)
);
}
return $error;
}
}