|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP Translation package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) PHP Translation team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Translation\PlatformAdapter\Transifex; |
|
13
|
|
|
|
|
14
|
|
|
use BabDev\Transifex\Transifex as TransifexClient; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Translation\Common\Exception\StorageException; |
|
17
|
|
|
use Translation\Common\Model\Message; |
|
18
|
|
|
use Translation\Common\Storage; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Transifex implements Storage |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var TransifexClient |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $domainToProjectId = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param TransifexClient $client |
|
37
|
|
|
* @param array $domainToProjectId |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(TransifexClient $client, array $domainToProjectId) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->client = $client; |
|
42
|
|
|
$this->domainToProjectId = $domainToProjectId; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function get($locale, $domain, $key) |
|
46
|
|
|
{ |
|
47
|
|
|
$projectKey = $this->getApiKey($domain); |
|
48
|
|
|
|
|
49
|
|
|
$translation = (string) $this->client->get('translations')->getTranslation($projectKey, $key, $locale)->getBody(); |
|
|
|
|
|
|
50
|
|
|
$meta = []; |
|
51
|
|
|
|
|
52
|
|
|
return new Message($key, $domain, $locale, $translation, $meta); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function create(Message $message) |
|
56
|
|
|
{ |
|
57
|
|
|
$projectKey = $this->getApiKey($message->getDomain()); |
|
58
|
|
|
|
|
59
|
|
|
$this->client->get('resources')->createResource($projectKey, $message->getKey(), $message->getKey(), 'TXT'); |
|
|
|
|
|
|
60
|
|
|
$translation = (string) $this->client->get('translations')->getTranslation($projectKey, $message->getKey(), $message->getLocale())->getBody(); |
|
|
|
|
|
|
61
|
|
|
if (empty($translation)) { |
|
62
|
|
|
$this->client->get('translations') |
|
|
|
|
|
|
63
|
|
|
->updateTranslation( |
|
64
|
|
|
$projectKey, |
|
65
|
|
|
$message->getKey(), |
|
66
|
|
|
$message->getLocale(), |
|
67
|
|
|
$message->getTranslation() |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function update(Message $message) |
|
73
|
|
|
{ |
|
74
|
|
|
$projectKey = $this->getApiKey($message->getDomain()); |
|
75
|
|
|
|
|
76
|
|
|
/** @var ResponseInterface $response */ |
|
77
|
|
|
$response = $this->client->get('translations') |
|
|
|
|
|
|
78
|
|
|
->updateTranslation($projectKey, $message->getKey(), $message->getLocale(), $message->getTranslation()); |
|
79
|
|
|
// Check it it was any error |
|
80
|
|
|
if ($response->getStatusCode() !== 200) { |
|
81
|
|
|
$this->create($message); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function delete($locale, $domain, $key) |
|
86
|
|
|
{ |
|
87
|
|
|
// Transifex API does not support deleting translations. |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $domain |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getApiKey($domain) |
|
96
|
|
|
{ |
|
97
|
|
|
if (isset($this->domainToProjectId[$domain])) { |
|
98
|
|
|
return $this->domainToProjectId[$domain]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
throw new StorageException(sprintf('Api key for domain "%s" has not been configured.', $domain)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: