Passed
Push — master ( ee2ae7...58f4d7 )
by Thomas
07:24 queued 04:58
created

GoogleTranslateHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 132
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 4 1
A google_translate() 0 56 5
A proxy_translate() 0 37 6
1
<?php
2
3
namespace LeKoala\Multilingual\Helpers;
4
5
use Exception;
6
7
/**
8
 * A simple helper to help translating strings
9
 */
10
class GoogleTranslateHelper
11
{
12
    /**
13
     * @var string
14
     */
15
    public static $provider = 'google_translate';
16
17
    /**
18
     * Translate from provider
19
     *
20
     * @param string $sourceText
21
     * @param string $targetLang
22
     * @param string $sourceLang
23
     * @return string
24
     */
25
    public static function translate($sourceText, $targetLang = null, $sourceLang = null)
26
    {
27
        $func = self::$provider;
28
        return self::$func($sourceText, $targetLang, $sourceLang);
29
    }
30
31
    /**
32
     * Translate from google public api
33
     *
34
     * @param string $sourceText
35
     * @param string $targetLang
36
     * @param string $sourceLang
37
     * @return string
38
     */
39
    public static function google_translate($sourceText, $targetLang = null, $sourceLang = null)
40
    {
41
        if (!$sourceLang) {
42
            $sourceLang = 'auto';
43
        }
44
        if (!$targetLang) {
45
            $targetLang = 'en';
46
        }
47
48
        $targetLang = substr($targetLang, 0, 2);
49
50
        $url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
51
            . $sourceLang . "&tl=" . $targetLang . "&dt=t&q=" . urlencode($sourceText);
52
53
        $result = file_get_contents($url);
54
        if (!$result) {
55
            throw new Exception("Failed to fetch content from $url");
56
        }
57
        $data = json_decode($result, true);
58
        if (!$data) {
59
            throw new Exception("Failed to decode json : " . json_last_error_msg());
60
        }
61
62
        // array:9 [▼
63
        // 0 => array:1 [▼
64
        //   0 => array:5 [▼
65
        //     0 => "TargetTextHere"
66
        //     1 => "SourceTextHere"
67
        //     2 => null
68
        //     3 => null
69
        //     4 => 1
70
        //   ]
71
        // ]
72
        // 1 => null
73
        // 2 => "en"
74
        // 3 => null
75
        // 4 => null
76
        // 5 => null
77
        // 6 => 1.0
78
        // 7 => []
79
        // 8 => array:4 [▼
80
        //   0 => array:1 [▼
81
        //     0 => "en"
82
        //   ]
83
        //   1 => null
84
        //   2 => array:1 [▼
85
        //     0 => 1.0
86
        //   ]
87
        //   3 => array:1 [▼
88
        //     0 => "en"
89
        //   ]
90
        // ]
91
92
        $translatedText = $data[0][0][0];
93
94
        return $translatedText;
95
    }
96
97
    /**
98
     * Translate from proxy api
99
     *
100
     * @param string $sourceText
101
     * @param string $targetLang
102
     * @param string $sourceLang
103
     * @return string
104
     */
105
    public static function proxy_translate($sourceText, $targetLang = null, $sourceLang = null)
106
    {
107
        if (!$sourceLang) {
108
            $sourceLang = 'auto';
109
        }
110
        if (!$targetLang) {
111
            $targetLang = 'en';
112
        }
113
114
        $targetLang = substr($targetLang, 0, 2);
115
116
        //translate/{to}[/{from}]?text=your_url_encoded_text
117
        $url = 'https://vercel-translate-api.vercel.app/translate/' . $targetLang . '/' . $sourceLang . '?text=' . urlencode($sourceText);
118
119
        // {
120
        //     "data": {
121
        //     "from": "auto",
122
        //     "to": "fr",
123
        //     "text": "hello",
124
        //     "translation": "bonjour"
125
        //     }
126
        //     }
127
128
        $result = file_get_contents($url);
129
        if (!$result) {
130
            throw new Exception("Failed to fetch content from $url");
131
        }
132
        $data = json_decode($result, true);
133
        if (!$data) {
134
            throw new Exception("Failed to decode json : " . json_last_error_msg());
135
        }
136
137
        if (!empty($data['error'])) {
138
            throw new Exception($data['error']);
139
        }
140
141
        return $data['data']['translation'];
142
    }
143
}
144