-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExistingTranslationsToDatabase.php
executable file
·129 lines (114 loc) · 4.38 KB
/
ExistingTranslationsToDatabase.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Copyright © Experius B.V. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Experius\MissingTranslations\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class ExistingTranslationsToDatabase
* @package Experius\MissingTranslations\Console\Command
*/
class ExistingTranslationsToDatabase extends Command
{
const INPUT_KEY_LOCALE = 'locale';
const SHORTCUT_KEY_LOCALE = 'l';
const INPUT_KEY_STORE = 'store';
const SHORTCUT_KEY_STORE = 's';
const INPUT_KEY_GLOBAL = 'global';
const SHORTCUT_KEY_GLOBAL = 'g';
/**
* @var Magento\Framework\App\State
*/
protected $state;
/**
* @var \Experius\MissingTranslations\Model\TranslationCollector
*/
protected $translationCollector;
/**
* AddTranslationsToDatabaseCommand constructor.
*
* @param \Magento\Framework\App\State $state
* @param \Experius\MissingTranslations\Model\TranslationCollector $translationCollector
*/
public function __construct(
\Magento\Framework\App\State $state,
\Experius\MissingTranslations\Model\TranslationCollector $translationCollector
) {
$this->state = $state;
$this->translationCollector = $translationCollector;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode('frontend');
if (!$input->getOption(self::INPUT_KEY_LOCALE)) {
throw new \InvalidArgumentException('Locale is not set. Please use --locale to set locale');
}
$locale = $input->getOption(self::INPUT_KEY_LOCALE);
$global = false;
if ($input->getOption(self::INPUT_KEY_GLOBAL)) {
$global = true;
if ($input->getOption(self::INPUT_KEY_STORE)) {
throw new \InvalidArgumentException('Store is not needed when --global flag is set.');
}
} elseif (!$input->getOption(self::INPUT_KEY_STORE)) {
throw new \InvalidArgumentException('Store is needed when --global flag is not set.');
}
$storeId = $global ? '0' : $input->getOption(self::INPUT_KEY_STORE);
$output->writeln(
'Inserting all existing csv translations into database for store id <info>' . $storeId
. '</info> and locale <info>' . $locale . '</info>'
);
$output->writeln('Still working... One moment.');
$insertionCount = $this->translationCollector->updateTranslationDatabase(
$storeId,
$locale,
\Experius\MissingTranslations\Model\TranslationCollector::TRANSLATION_TYPE_EXISTING
);
if ($insertionCount > 0) {
$output->writeln('Insertion was successful, <info>'
. $insertionCount . '</info> existing csv translations added to database');
} else {
$output->writeln('Nothing was inserted. All found existing csv translations already present in database.');
}
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('experius_missingtranslations:existing-translations-to-database');
$this->setDescription('Add all existing csv translations to database for easy editing');
$this->setDefinition([
new InputOption(
self::INPUT_KEY_LOCALE,
self::SHORTCUT_KEY_LOCALE,
InputOption::VALUE_REQUIRED,
'Use the --locale parameter to parse specific language.'
),
new InputOption(
self::INPUT_KEY_GLOBAL,
self::SHORTCUT_KEY_GLOBAL,
InputOption::VALUE_NONE,
'Use the --global parameter to add translations as global.' .
' Omit the parameter if a store is specified.'
),
new InputOption(
self::INPUT_KEY_STORE,
self::SHORTCUT_KEY_STORE,
InputArgument::OPTIONAL,
'Use the --store parameter to parse store. (for DB translation check)'
)
]);
parent::configure();
}
}