GoogleTranslateHelper   A
last analyzed

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