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 trnaslation. |
14
|
|
|
* |
15
|
|
|
* @param string $string |
16
|
|
|
* |
17
|
|
|
* @return object |
18
|
|
|
*/ |
19
|
|
|
public static function trans($string) |
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); |
27
|
|
|
} elseif (config('laralang.default.translator') == 'apertium') { |
28
|
|
|
return new ApertiumTrans($string); |
29
|
|
|
} elseif (config('laralang.default.translator') == 'google') { |
30
|
|
|
return new GoogleTrans($string); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the languages that translations has been translated. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public static function toLanguages() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$locales = []; |
43
|
|
|
$translations = DB_Translation::distinct()->select('to_lang')->get(); |
44
|
|
|
foreach ($translations as $object) { |
45
|
|
|
array_push($locales, $object->to_lang); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $locales; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the languages from translations has been translated. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public static function fromLanguages() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$locales = []; |
59
|
|
|
$translations = DB_Translation::distinct()->select('from_lang')->get(); |
60
|
|
|
foreach ($translations as $object) { |
61
|
|
|
array_push($locales, $object->from_lang); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $locales; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get all the available languages. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public static function allLanguages() |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'en' => 'English', |
76
|
|
|
'es' => 'Spanish', |
77
|
|
|
'ca' => 'Catalan', |
78
|
|
|
'pt' => 'Portuguese', |
79
|
|
|
'zh' => 'Chinese', |
80
|
|
|
'ja' => 'Japanese', |
81
|
|
|
'de' => 'German', |
82
|
|
|
'fr' => 'French', |
83
|
|
|
'eu' => 'Basque', |
84
|
|
|
'ru' => 'Russian', |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return secure_asset method or asset method depending on config https |
91
|
|
|
* |
92
|
|
|
* @param string $asset |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public static function checkAsset($asset) |
97
|
|
|
{ |
98
|
|
|
if (config('laralang.default.https')) { |
99
|
|
|
return secure_asset($asset); |
100
|
|
|
} |
101
|
|
|
return asset($asset); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return secure_url method or url method depending on config https |
106
|
|
|
* |
107
|
|
|
* @param string $url |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public static function checkUrl($url) |
112
|
|
|
{ |
113
|
|
|
if (config('laralang.default.https')) { |
114
|
|
|
return secure_url($url); |
115
|
|
|
} |
116
|
|
|
return url($url); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.