|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\I18nBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
7
|
|
|
use Symfony\Component\Console\Helper\ProgressHelper; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
|
12
|
|
|
|
|
13
|
|
|
class ImportViewTranslationsCommand extends ContainerAwareCommand |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
public function configure() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::configure(); |
|
21
|
|
|
|
|
22
|
|
|
$this |
|
23
|
|
|
->setName('victoire:i18n:import_view_translations') |
|
24
|
|
|
->setDescription('import view translations (csv)') |
|
25
|
|
|
->addOption( |
|
26
|
|
|
'src', |
|
27
|
|
|
null, |
|
28
|
|
|
InputOption::VALUE_REQUIRED, |
|
29
|
|
|
'myFile.tsv, file.csv...' |
|
30
|
|
|
) |
|
31
|
|
|
->addOption( |
|
32
|
|
|
'property', |
|
33
|
|
|
null, |
|
34
|
|
|
InputOption::VALUE_REQUIRED, |
|
35
|
|
|
'name, description...' |
|
36
|
|
|
) |
|
37
|
|
|
->addOption( |
|
38
|
|
|
'delimiter', |
|
39
|
|
|
null, |
|
40
|
|
|
InputOption::VALUE_OPTIONAL, |
|
41
|
|
|
'\n...', |
|
42
|
|
|
' ' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param InputInterface $input |
|
48
|
|
|
* @param OutputInterface $output |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
53
|
|
|
{ |
|
54
|
|
|
$property = $input->getOption('property'); |
|
55
|
|
|
$delimiter = $input->getOption('delimiter'); |
|
56
|
|
|
|
|
57
|
|
|
/** @var ProgressHelper $progress */ |
|
58
|
|
|
$progress = $this->getHelperSet()->get('progress'); |
|
59
|
|
|
$progress->setProgressCharacter('V'); |
|
60
|
|
|
$progress->setEmptyBarCharacter('-'); |
|
61
|
|
|
|
|
62
|
|
|
/** @var EntityManager $entityManager */ |
|
63
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
|
64
|
|
|
$repo = $entityManager->getRepository('VictoireCoreBundle:View'); |
|
65
|
|
|
|
|
66
|
|
|
$contentsOfFile = file_get_contents($input->getOption('src')); |
|
67
|
|
|
|
|
68
|
|
|
$lines = explode("\n", $contentsOfFile); |
|
69
|
|
|
$header = array_shift($lines); |
|
70
|
|
|
$header = explode($delimiter, $header); |
|
71
|
|
|
array_shift($header); |
|
72
|
|
|
|
|
73
|
|
|
$total = 0; |
|
74
|
|
|
$progress->start($output, count($lines)); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($lines as $line) { |
|
77
|
|
|
$translations = explode($delimiter, $line); |
|
78
|
|
|
$id = array_shift($translations); |
|
79
|
|
|
|
|
80
|
|
|
if ($view = $repo->find($id)) { |
|
81
|
|
|
foreach ($translations as $key => $translation) { |
|
82
|
|
|
$locale = rtrim(strtolower($header[$key]), "\r"); |
|
83
|
|
|
$view->translate($locale)->{'set'.ucfirst($property)}($translation); |
|
84
|
|
|
} |
|
85
|
|
|
$view->mergeNewTranslations(); |
|
86
|
|
|
$entityManager->flush(); |
|
87
|
|
|
} else { |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
$progress->advance(); |
|
90
|
|
|
$total++; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$progress->finish(); |
|
94
|
|
|
|
|
95
|
|
|
$output->writeln(sprintf('<comment>Ok, %s translations updated !</comment>', $total)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for the
elsebranches ofifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elsebranches can be removed.could be turned into
This is much more concise to read.