forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageLanguage.php
More file actions
114 lines (100 loc) · 3.17 KB
/
PackageLanguage.php
File metadata and controls
114 lines (100 loc) · 3.17 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
<?php
namespace ProcessMaker\Package;
use Illuminate\Support\Facades\File;
class PackageLanguage
{
/**
* @var string
*/
private $path = '';
/**
* @var array
*/
private $languages = [];
/**
* Register Path and languages of package
*
* @param $path
*/
public function registerPath($path)
{
$this->path = null;
if (File::exists($path)) {
$this->path = $path;
//Search by file of languages
foreach (glob($path . '*.json') as $filename) {
$this->languages[] = basename($filename);
}
}
}
/**
* Merge Language package with language bpm
*
* @return bool
*/
public function mergeLanguage()
{
try
{
$response = true;
foreach ($this->languages as $language) {
//Language local
$resourceLocal = resource_path('lang') . '/' . $language;
$localLanguage = [];
if (File::exists($resourceLocal)) {
$localLanguage = json_decode(file_get_contents($resourceLocal), true);
$response = $this->errors('BPM - ' . $language) ? $response : false;
}
//Language Package
$resourcePackage = $this->path . $language;
$packageLanguage = json_decode(utf8_encode(file_get_contents($resourcePackage)), true);
$response = $this->errors($language) ? $response : false;
if (!$packageLanguage) {
$packageLanguage = [];
}
$data = json_encode(array_merge($localLanguage, $packageLanguage), JSON_PRETTY_PRINT);
//Merge languages
file_put_contents($resourceLocal, stripslashes($data));
}
return $response;
} catch (\Exception $e) {
return false;
}
}
/**
* Show error with file json.
*
* @param $language
*
* @return bool
*/
private function errors($language)
{
$response = false;
switch(json_last_error()) {
case JSON_ERROR_NONE:
//without errors.
$response = true;
break;
case JSON_ERROR_DEPTH:
echo $language . ' - ' . __('Exceeded maximum stack size') . "\n";
break;
case JSON_ERROR_STATE_MISMATCH:
echo $language . ' - ' . __('Buffer overflow or modes do not match') . "\n";
break;
case JSON_ERROR_CTRL_CHAR:
echo $language . ' - ' . __('Found unexpected control character') . "\n";
break;
case JSON_ERROR_SYNTAX:
echo $language . ' - ' . __('Syntax error, malformed JSON') . "\n";
break;
case JSON_ERROR_UTF8:
echo $language . ' - ' . __('Uformed UTF-8 characters, possibly incorrectly coded') . "\n";
break;
default:
echo $language . ' - ' . __('Unknown error') . "\n";
break;
}
return $response;
}
}