1
|
|
|
<?php |
2
|
|
|
use Illuminate\Support\Str; |
3
|
|
|
use TopviewDigital\TranslationHelper\Model\VocabTerm; |
4
|
|
|
use TopviewDigital\TranslationHelper\Model\VocabCite; |
5
|
|
|
use TopviewDigital\TranslationHelper\Service\Translation; |
6
|
|
|
|
7
|
|
|
if (!function_exists('is_json')) { |
8
|
|
|
function is_json($string) |
9
|
|
|
{ |
10
|
|
|
if (!is_string($string)) { |
11
|
|
|
return false; |
12
|
|
|
} |
13
|
|
|
json_decode($string); |
14
|
|
|
|
15
|
|
|
return json_last_error() == JSON_ERROR_NONE; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if (!function_exists('auto_trans_able')) { |
20
|
|
|
function auto_trans_able() |
21
|
|
|
{ |
22
|
|
|
return config('trans-helper.translation.mode') == 'auto' |
23
|
|
|
&& config('queue.default') != 'sync'; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
if (!function_exists('localize')) { |
27
|
|
|
function localize($languages, string $failback = '') |
28
|
|
|
{ |
29
|
|
|
if (is_array($languages) || is_json($languages)) { |
30
|
|
|
$languages = (!is_array($languages)) ? (array)json_decode($languages) : $languages; |
31
|
|
|
$locales = array_keys($languages); |
32
|
|
|
$system = \App::getLocale(); |
33
|
|
|
$default = config('app.locale'); |
34
|
|
|
|
35
|
|
|
$locale = in_array($system, $locales) ? $system : (in_array($default, $locales) ? $default : null); |
36
|
|
|
|
37
|
|
|
return $locale ? $languages[$locale] : $failback; |
38
|
|
|
} |
39
|
|
|
if (is_string($languages) && empty($failback)) { |
40
|
|
|
$tracer = (new Exception())->getTrace(); |
41
|
|
|
$cite = []; |
42
|
|
|
$cite['file'] = preg_replace('/(\.php)(.+)$/', '${1}', $tracer[0]['file']); |
43
|
|
|
$cite['line'] = $tracer[0]['line']; |
44
|
|
|
array_shift($tracer); |
45
|
|
|
$cite['function'] = $tracer[0]['function'] ?? ''; |
46
|
|
|
$cite['class'] = $tracer[0]['class'] ?? ''; |
47
|
|
|
$vocab = []; |
48
|
|
|
$vocab['namespace'] = preg_replace( |
49
|
|
|
'/(^' . addcslashes(base_path(), '\/') . ')|(\.php$)/', |
50
|
|
|
'', |
51
|
|
|
$cite['file'] |
52
|
|
|
); |
53
|
|
|
$vocab['term'] = $languages; |
54
|
|
|
$vocab = VocabTerm::firstOrCreate($vocab); |
55
|
|
|
if (!$vocab->transaltion) { |
56
|
|
|
$vocab->translation = [config('app.locale') => $vocab['term']]; |
57
|
|
|
$vocab->save(); |
58
|
|
|
if (auto_trans_able()) { |
59
|
|
|
dispatch(new Translation($vocab)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
if (auto_trans_able() && !in_array(app()->getLocale(), array_keys($vocab->translation))) { |
|
|
|
|
63
|
|
|
dispatch(new Translation($vocab, [app()->getLocale()])); |
64
|
|
|
} |
65
|
|
|
$cite['file'] = preg_replace( |
66
|
|
|
'/^' . addcslashes(base_path(), '\/') . '/', |
67
|
|
|
'', |
68
|
|
|
$cite['file'] |
69
|
|
|
); |
70
|
|
|
$cite = VocabCite::firstOrCreate($cite); |
71
|
|
|
$vocab->cites()->sync([$cite->id], false); |
72
|
|
|
if (!$cite->code) { |
73
|
|
|
$lines = explode("\n", file_get_contents(base_path() . $cite->file)); |
74
|
|
|
$cite->code = $lines[$cite->line - 1]; |
75
|
|
|
if (substr($cite->file, -10) != '.blade.php') { |
76
|
|
|
for ($start = $cite->line - 2; $start > -1; $start--) { |
77
|
|
|
$char = substr(rtrim($lines[$start]), -1); |
78
|
|
|
if ($char == ';' || $char == '}' || $char == '{') { |
79
|
|
|
$start++; |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$count = count($lines); |
84
|
|
|
for ($end = $cite->line - 1; $end < $count; $end++) { |
85
|
|
|
$char = substr(rtrim($lines[$end]), -1); |
86
|
|
|
if ($char == ';') { |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
$code = array_filter(array_slice($lines, $start, $end - $start + 1, true), 'trim'); |
91
|
|
|
$max = strlen($end); |
92
|
|
|
$cite->code = implode("\n", array_map(function ($u, $v) use ($max) { |
93
|
|
|
return sprintf("%{$max}d %s", $u + 1, rtrim($v)); |
94
|
|
|
}, array_keys($code), $code)); |
95
|
|
|
$cite->save(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return localize($vocab->translation, $vocab->term); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $failback; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!function_exists('sweep')) { |
107
|
|
|
function sweep() |
108
|
|
|
{ |
109
|
|
|
array_map( |
110
|
|
|
function ($u) { |
111
|
|
|
$u->sweep(); |
112
|
|
|
}, |
113
|
|
|
array_merge( |
114
|
|
|
VocabCite::get()->all(), |
115
|
|
|
VocabTerm::get()->all() |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!function_exists('translate')) { |
122
|
|
|
function translate($locales = []) |
123
|
|
|
{ |
124
|
|
|
dispatch(new Translation(null, $locales)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
if (!function_exists('slugify')) { |
128
|
|
|
function slugify($text) |
129
|
|
|
{ |
130
|
|
|
// replace non letter or digits by - |
131
|
|
|
$text = preg_replace('~[^\pL\d]+~u', '-', $text); |
132
|
|
|
|
133
|
|
|
// transliterate |
134
|
|
|
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); |
135
|
|
|
|
136
|
|
|
// remove unwanted characters |
137
|
|
|
$text = preg_replace('~[^-\w]+~', '', $text); |
138
|
|
|
|
139
|
|
|
// trim |
140
|
|
|
$text = trim($text, '-'); |
141
|
|
|
|
142
|
|
|
// remove duplicated - symbols |
143
|
|
|
$text = preg_replace('~-+~', '-', $text); |
144
|
|
|
|
145
|
|
|
// lowercase |
146
|
|
|
$text = strtolower($text); |
147
|
|
|
|
148
|
|
|
if (empty($text)) { |
149
|
|
|
return 'n-a'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $text; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
if (!function_exists('lang_file_name')) { |
156
|
|
|
function lang_file_name($path, $locale, $namespace) |
157
|
|
|
{ |
158
|
|
|
$lang_file = str_replace('/', '.', strtolower(ltrim(Str::snake($namespace), '/'))); |
159
|
|
|
$lang_file = str_replace('//', '/', implode('/', [$path, $locale, $lang_file])); |
160
|
|
|
$lang_file = str_replace('._', '.', $lang_file) . '.php'; |
161
|
|
|
return $lang_file; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!function_exists('export')) { |
166
|
|
|
function export($path = null) |
167
|
|
|
{ |
168
|
|
|
$path = $path ?? config('trans-helper.export.path'); |
169
|
|
|
$locales = VocabTerm::locales(); |
170
|
|
|
$namespaces = VocabTerm::namespaces(); |
171
|
|
|
foreach ($namespaces as $namespace) { |
172
|
|
|
foreach ($locales as $locale) { |
173
|
|
|
if (file_exists($path . '/' . $locale) && !is_dir($path . '/' . $locale)) { |
174
|
|
|
unlink($path . '/' . $locale); |
175
|
|
|
} |
176
|
|
|
if (!file_exists($path . '/' . $locale)) { |
177
|
|
|
mkdir($path . '/' . $locale, 0777, true); |
178
|
|
|
} |
179
|
|
|
$lang_file = lang_file_name($path, $locale, $namespace); |
180
|
|
|
|
181
|
|
|
$lines = ['<?php', '', 'return [']; |
182
|
|
|
foreach (VocabTerm::where('namespace', $namespace)->get() as $term) { |
183
|
|
|
$lines[] = sprintf( |
184
|
|
|
" '%s'=>'%s',", |
185
|
|
|
$term->slug, |
186
|
|
|
localize($term->translation, $term->translation[config('app.locale')]) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
$lines[] = "];\n"; |
190
|
|
|
file_put_contents($lang_file, implode("\n", $lines)); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|