|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aitor24\Laralang; |
|
4
|
|
|
|
|
5
|
|
|
use Aitor24\Laralang\Builder\ApertiumTrans; |
|
6
|
|
|
use Aitor24\Laralang\Builder\GoogleTrans; |
|
7
|
|
|
use Aitor24\Laralang\Builder\MymemoryTrans; |
|
8
|
|
|
use Aitor24\Laralang\Models\DB_Translation; |
|
9
|
|
|
|
|
10
|
|
|
class Builder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Get the translation. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $string |
|
16
|
|
|
* |
|
17
|
|
|
* @return object |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function trans($string, $vars = []) |
|
20
|
|
|
{ |
|
21
|
|
|
$translator = config('laralang.default.translator'); |
|
22
|
|
|
if (!in_array(config('laralang.default.translator'), ['apertium', 'mymemory', 'google'])) { |
|
23
|
|
|
return "<font style='color:red;'>Laralang doesn't support $translator translator. Check config</font>"; |
|
24
|
|
|
} else { |
|
25
|
|
|
if (config('laralang.default.translator') == 'mymemory') { |
|
26
|
|
|
return new MymemoryTrans($string, $vars); |
|
27
|
|
|
} elseif (config('laralang.default.translator') == 'apertium') { |
|
28
|
|
|
return new ApertiumTrans($string, $vars); |
|
29
|
|
|
} elseif (config('laralang.default.translator') == 'google') { |
|
30
|
|
|
return new GoogleTrans($string, $vars); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Generate files json for a specify package. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $string |
|
|
|
|
|
|
39
|
|
|
* |
|
40
|
|
|
* @return object |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function generateTranslations($is_package, $package, $translationsPath, $toLangs = 'es') |
|
43
|
|
|
{ |
|
44
|
|
|
if ($is_package) { |
|
45
|
|
|
$path = realpath(__DIR__.'./../../../'.$package.$translationsPath); |
|
46
|
|
|
} else { |
|
47
|
|
|
$path = resource_path().'/'.$translationsPath; |
|
48
|
|
|
} |
|
49
|
|
|
$toLangs = explode('|', $toLangs); |
|
50
|
|
|
$files = glob($path.'/*.php'); |
|
51
|
|
|
foreach ($files as $file) { |
|
52
|
|
|
foreach ($toLangs as $lang) { |
|
53
|
|
|
$lines = include $file; |
|
54
|
|
|
$array = self::transArray($lines, $lang); |
|
55
|
|
|
$data = '<?php return '.str_replace(')', ']', str_replace('array (', '[', str_replace(")'", ']', str_replace("'array (", '[', $array)))).";\n"; |
|
56
|
|
|
|
|
57
|
|
|
$savePath = $path.'./../'.$lang.'/'; |
|
58
|
|
|
if (!file_exists($savePath)) { |
|
59
|
|
|
mkdir($savePath, 0777, true); |
|
60
|
|
|
} |
|
61
|
|
|
file_put_contents($savePath.basename($file), $data); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private static function transArray($array, $lang) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($array as $var => $text) { |
|
69
|
|
|
$vars = []; |
|
70
|
|
|
if (is_array($text)) { |
|
71
|
|
|
$array[$var] = self::transArray($text, $lang); |
|
72
|
|
|
} else { |
|
73
|
|
|
foreach (explode(' ', $text) as $word) { |
|
74
|
|
|
if (substr($word, 0, 1) == ':' && !in_array($word, $vars)) { |
|
75
|
|
|
array_push($vars, $word); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
$entry = $text; |
|
79
|
|
|
foreach ($vars as $key => $varreplace) { |
|
80
|
|
|
$entry = str_replace($varreplace, 'VAR_'.$key, $entry); |
|
81
|
|
|
} |
|
82
|
|
|
$array[$var] = self::trans($entry, $vars)->from('en')->to($lang)->Save(false)->__toString(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return stripslashes(var_export($array, true)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the languages that translations has been translated. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public static function toLanguages() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$locales = []; |
|
97
|
|
|
$translations = DB_Translation::distinct()->select('to_lang')->get(); |
|
98
|
|
|
foreach ($translations as $object) { |
|
99
|
|
|
array_push($locales, $object->to_lang); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $locales; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get the languages from translations has been translated. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public static function fromLanguages() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$locales = []; |
|
113
|
|
|
$translations = DB_Translation::distinct()->select('from_lang')->get(); |
|
114
|
|
|
foreach ($translations as $object) { |
|
115
|
|
|
array_push($locales, $object->from_lang); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $locales; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get all the available languages. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function allLanguages() |
|
127
|
|
|
{ |
|
128
|
|
|
return [ |
|
129
|
|
|
'en' => 'English', |
|
130
|
|
|
'es' => 'Spanish', |
|
131
|
|
|
'ca' => 'Catalan', |
|
132
|
|
|
'pt' => 'Portuguese', |
|
133
|
|
|
'zh' => 'Chinese', |
|
134
|
|
|
'ja' => 'Japanese', |
|
135
|
|
|
'de' => 'German', |
|
136
|
|
|
'fr' => 'French', |
|
137
|
|
|
'eu' => 'Basque', |
|
138
|
|
|
'ru' => 'Russian', |
|
139
|
|
|
]; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.