-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.php
More file actions
127 lines (107 loc) · 3.76 KB
/
Translator.php
File metadata and controls
127 lines (107 loc) · 3.76 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
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
class Translator
{
/**
* @var string[]
*/
private $config;
private $translations = null;
/**
* @var InvisibleWrapper
*/
private $encoder;
/**
* Constructor
*
* @param array $config Configuration array with the following keys:
* - apiUrl: Base URL for the Tolgee API
* - projectId: Tolgee project ID
* - language: Language code to fetch translations for
* - apiKey: Tolgee API key for authentication
*/
public function __construct($config = [])
{
// Set default configuration values
$this->config = array_merge([
'apiUrl' => 'https://app.tolgee.io',
'language' => 'en',
'apiKey' => '',
'mode' => 'development'
], $config);
// Initialize the encoder
$this->encoder = new InvisibleWrapper();
}
/**
* Fetch translations from Tolgee API
*
* @return array Translations array or empty array on failure
*/
private function fetchTranslations()
{
// If no project ID or API key is provided, return empty array
if (empty($this->config['apiKey'])) {
error_log('Tolgee Translator: Missing projectId or apiKey in configuration');
return [];
}
$url = rtrim($this->config['apiUrl'], '/') .
'/v2/projects/' .
'translations/' .
$this->config['language'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-API-Key: ' . $this->config['apiKey'],
'Accept: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new \RuntimeException('Tolgee Translator: API request failed with status code '
. $httpCode .
'. Response: '
. $response
);
}
$data = json_decode($response, true);
if (!$data) {
error_log('Tolgee Translator: Invalid API response format');
return [];
}
// The response format is expected to be:
// {"en":{"what a key":"Translated value","another key":"Another key translated"}}
if (isset($data[$this->config['language']])) {
return $data[$this->config['language']];
}
error_log('Tolgee Translator: Language not found in response');
return [];
}
/**
* Translate a key with optional parameters
*
* @param string $keyName The key to translate
* @param array $params Optional parameters to replace placeholders in the translation
* @return string The translated string or the key if not found
*/
public function translate($keyName, $params = [])
{
// Lazy load translations on first use
if ($this->translations === null) {
$this->translations = $this->fetchTranslations();
}
// Check if the key exists in the translations
if (!isset($this->translations[$keyName])) {
return $keyName; // Return the key itself if not found
}
$translation = $this->translations[$keyName];
// Replace parameters in the translation
foreach ($params as $param => $value) {
$translation = str_replace('{' . $param . '}', $value, $translation);
}
if ($this->config['mode'] == 'development') {
$translation = $this->encoder->wrapWithFullKeyEncode($keyName, null, $translation);
}
return $translation;
}
}