|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\InlineTranslation; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\NamespacedItemResolver; |
|
6
|
|
|
use View; |
|
7
|
|
|
use File; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class InlineTranslation |
|
11
|
|
|
* @package BeyondCode\InlineTranslation |
|
12
|
|
|
*/ |
|
13
|
|
|
class InlineTranslation |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
public function isEnabled() |
|
17
|
|
|
{ |
|
18
|
|
|
return config('inline-translation.enabled'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function boot() |
|
22
|
|
|
{ |
|
23
|
|
|
View::composer('*', function ($view) { |
|
24
|
|
|
$viewContent = file_get_contents($view->getPath()); |
|
25
|
|
|
|
|
26
|
|
|
$file = tempnam(sys_get_temp_dir(), $view->name()); |
|
27
|
|
|
|
|
28
|
|
|
(new TemplateParser($view->getData()))->parseTranslationTags($viewContent); |
|
29
|
|
|
|
|
30
|
|
|
file_put_contents($file, $viewContent); |
|
31
|
|
|
|
|
32
|
|
|
$view->setPath($file); |
|
33
|
|
|
}); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $locale |
|
38
|
|
|
* @param string $key |
|
39
|
|
|
* @param string $value |
|
40
|
|
|
*/ |
|
41
|
|
|
public function updateTranslationFiles($locale, $key, $value) |
|
42
|
|
|
{ |
|
43
|
|
|
// Try JSON lookup first. |
|
44
|
|
|
$this->updateJsonLanguageFile(resource_path('lang/' . $locale . '.json'), $key, $value); |
|
45
|
|
|
|
|
46
|
|
|
// JSON does not exist - try php based language files |
|
47
|
|
|
list($namespace, $group, $item) = app(NamespacedItemResolver::class)->parseKey($key); |
|
48
|
|
|
|
|
49
|
|
|
if (is_null($namespace) || $namespace == '*') { |
|
50
|
|
|
$this->updateRequiredLanguageFile(resource_path("lang/$locale/$group.php"), $item, $value); |
|
51
|
|
|
} else { |
|
52
|
|
|
$this->updateRequiredLanguageFile(resource_path("lang/vendor/$namespace/$locale/$group.php"), $item, $value); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $path |
|
58
|
|
|
* @param string $item |
|
59
|
|
|
* @param string $value |
|
60
|
|
|
*/ |
|
61
|
|
|
private function updateRequiredLanguageFile($path, $item, $value) |
|
62
|
|
|
{ |
|
63
|
|
|
if (File::exists($path)) { |
|
64
|
|
|
$content = File::getRequire($path); |
|
65
|
|
|
|
|
66
|
|
|
if (array_has($content, $item)) { |
|
67
|
|
|
$content[$item] = $value; |
|
68
|
|
|
|
|
69
|
|
|
File::put($path, '<?php return '.var_export($content, true).';'); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $path |
|
76
|
|
|
* @param string $item |
|
77
|
|
|
* @param string $value |
|
78
|
|
|
*/ |
|
79
|
|
|
private function updateJsonLanguageFile($path, $item, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
if (File::exists($path)) { |
|
82
|
|
|
$decoded = json_decode(File::get($path), true); |
|
83
|
|
|
|
|
84
|
|
|
if (!is_null($decoded) && array_has($decoded, $item)) { |
|
85
|
|
|
$decoded[$item] = $value; |
|
86
|
|
|
|
|
87
|
|
|
File::put($path, json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |