-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathImportFromCsvCommand.php
More file actions
157 lines (144 loc) · 4.79 KB
/
ImportFromCsvCommand.php
File metadata and controls
157 lines (144 loc) · 4.79 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace UFirst\LangImportExport\Console;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Arr;
use Lang;
use \UFirst\LangImportExport\Facades\LangListService;
use Symfony\Component\VarExporter\VarExporter;
class ImportFromCsvCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'lang-import:csv';
/**
* The console command description.
*
* @var string
*/
protected $description = "Imports the language files from CSV files";
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('file', InputArgument::REQUIRED, 'The CSV file to be imported'),
);
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getOptions()
{
return array(
array('delimiter', 'd', InputOption::VALUE_OPTIONAL, 'The optional delimiter parameter sets the field delimiter (one character only).', ','),
array('enclosure', 'c', InputOption::VALUE_OPTIONAL, 'The optional enclosure parameter sets the field enclosure (one character only).', '"'),
array('escape', 'e', InputOption::VALUE_OPTIONAL, 'The escape character (one character only). Defaults as a backslash.', '\\'),
array('merge', 'm', InputOption::VALUE_OPTIONAL, 'Merge translations in single file instead of overwriting whole file.', false),
);
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$file = $this->argument('file');
$delimiter = $this->option('delimiter');
$enclosure = $this->option('enclosure');
$escape = $this->option('escape');
$merge = $this->option('merge');
// Create output device and write CSV.
if (($input_fp = fopen($file, 'r')) === FALSE) {
$this->error('Can\'t open the input file!');
}
// Write CSV lintes
$languages = fgetcsv($input_fp, 0, $delimiter, $enclosure, $escape);
array_shift($languages);
$translations = [];
$lineNumber = 1;
while (($data = fgetcsv($input_fp, 0, $delimiter, $enclosure, $escape)) !== FALSE) {
$lineNumber++;
try {
$translations[array_shift($data)] = array_combine($languages, $data);
} catch (\Exception $e) {
$this->error("Failed to import line {$lineNumber}. Languages" . implode(", ", $languages) . ' | Translations: ' . implode(", ", $data) . ' ' . $e->getMessage());
}
}
fclose($input_fp);
$this->writeLangList($languages, $translations, $merge);
}
private function getGroupsFromNewTranslations($new_translations)
{
$groups = [];
foreach ($new_translations as $key => $value) {
$group = explode('.', $key)[0];
$groups[$group] = $group;
}
return $groups;
}
private function writeLangList($languages, $new_translations, $should_merge_translations = false)
{
$groups = $this->getGroupsFromNewTranslations($new_translations);
foreach ($languages as $locale) {
foreach ($groups as $group) {
$translations = LangListService::loadLangList($locale, $group);
$override_translations = array_filter($new_translations, function ($key) use ($group) {
return strpos($key, $group) === 0;
}, ARRAY_FILTER_USE_KEY);
if (count($override_translations) === 0) {
$this->info("No translations were found for locale {$locale} within group {$group}");
continue;
}
foreach ($override_translations as $key => $value) {
if ($value[$locale]) {
if (!$should_merge_translations) {
Arr::set($translations, $key, $value[$locale]);
} else {
$translations[$key] = $value[$locale];
}
} else {
Arr::forget($translations, $key);
}
}
if ($should_merge_translations) {
$undotted_translations = [];
foreach ($translations as $key => $translation) {
Arr::set($undotted_translations, $key, $translation);
}
$translations = $undotted_translations;
}
$header = "<?php\n\nreturn ";
$language_dir = base_path("resources/lang/{$locale}");
if (! file_exists($language_dir) && file_exists(base_path("lang/{$locale}"))) {
$language_dir = base_path("lang/{$locale}");
}
if (!is_writable($language_dir)) {
$this->error("Language directory $language_dir does not exist or is not writeable. Skipping");
continue;
}
$language_file = $language_dir . "/{$group}.php";
if (!is_writable($language_file)) {
$this->info("Creating language file: $language_file");
touch($language_file);
}
if (($fp = fopen($language_file, 'w')) !== FALSE) {
fputs($fp, $header . VarExporter::export($translations[$group]) . ";\n");
fclose($fp);
} else {
$this->error("Cannot open language file at {$language_file} for writing. Check the file permissions.");
}
}
}
}
}