forked from upchuk/excel_to_po
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
93 lines (79 loc) · 2.94 KB
/
index.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
<?php
use Gettext\Generator\PoGenerator;
use Gettext\Translation;
use Gettext\Translations;
use Shuchkin\SimpleXLSX;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
require 'vendor/autoload.php';
(new SingleCommandApplication())
->setName('Process and Excel to PO')
->addArgument('file', InputArgument::REQUIRED, 'The file')
->addOption('rows', null, InputOption::VALUE_OPTIONAL, 'The number of rows to export', 1000000)
->setCode(function (InputInterface $input, OutputInterface $output): int {
$file = $input->getArgument('file');
$total_rows = $input->getOption('rows');
if (!file_exists($file)) {
$output->writeln('<error>The file does not exist</error>');
return Command::INVALID;
}
$xlsx = SimpleXLSX::parse($file);
$originals = [];
foreach ($xlsx->rowsEx() as $row_index => $row) {
if ($row_index === 0) {
continue;
}
foreach ($row as $col_index => $col_info) {
if ($col_index === 1) {
$originals[$row_index] = $col_info['value'];
}
}
}
$translations = [];
$language_column_numbers = [];
foreach ($xlsx->rowsEx() as $row_index => $row) {
if ($row_index == ((int) $total_rows + 1)) {
break;
}
if ($row_index === 0) {
foreach ($row as $col_index => $col_info) {
if (in_array($col_index, [0, 1])) {
continue;
}
$col_letter = preg_replace("/[^A-Z]+/", "", $col_info['name']);
$language_column_numbers[$col_letter] = $col_info['value'];
}
continue;
}
foreach ($row as $col_index => $col_info) {
if (in_array($col_index, [0, 1])) {
continue;
}
$col_letter = preg_replace("/[^A-Z]+/", "", $col_info['name']);
$langcode = $language_column_numbers[$col_letter];
$translations[$row_index][$langcode] = $col_info['value'];
}
}
$translations_objects = [];
foreach ($translations as $row_index => $row_translations) {
foreach ($row_translations as $langcode => $translation_value) {
if (!isset($translations_objects[$langcode])) {
$translations_objects[$langcode] = Translations::create('excel_to_po', $langcode);
}
$translations_object = $translations_objects[$langcode];
$translation = Translation::create('', $originals[$row_index]);
$translation->translate($translation_value);
$translations_object->add($translation);
}
}
$generator = new PoGenerator();
foreach ($translations_objects as $object) {
$generator->generateFile($object, __DIR__ . '/output/' . $object->getLanguage() . '.po');
}
return Command::SUCCESS;
})
->run();