-
Notifications
You must be signed in to change notification settings - Fork 92
/
index.php
63 lines (54 loc) · 2.06 KB
/
index.php
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
<?php
# [START getting_started_background_translate]
# [START getting_started_background_translate_setup]
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\Transaction;
use Google\Cloud\Translate\TranslateClient;
# [END getting_started_background_translate_setup]
/**
* @param array $data {
* The PubSub message data containing text and target language.
*
* @type string $text
* The full text to translate.
* @type string $language
* The target language for the translation.
* }
*/
function translateString(array $data)
{
if (empty($data['language']) || empty($data['text'])) {
throw new Exception('Error parsing translation data');
}
# [START getting_started_background_translate_init]
$firestore = new FirestoreClient();
$translate = new TranslateClient();
$translation = [
'original' => $data['text'],
'lang' => $data['language'],
];
# [END getting_started_background_translate_init]
# [START getting_started_background_translate_transaction]
$docId = sprintf('%s:%s', $data['language'], base64_encode($data['text']));
$docRef = $firestore->collection('translations')->document($docId);
$firestore->runTransaction(
function (Transaction $transaction) use ($translate, $translation, $docRef) {
$snapshot = $transaction->snapshot($docRef);
if ($snapshot->exists()) {
return; // Do nothing if the document already exists
}
# [START getting_started_background_translate_string]
$result = $translate->translate($translation['original'], [
'target' => $translation['lang'],
]);
# [END getting_started_background_translate_string]
$transaction->set($docRef, $translation + [
'translated' => $result['text'],
'originalLang' => $result['source'],
]);
}
);
# [END getting_started_background_translate_transaction]
echo "Done.";
}
# [END getting_started_background_translate]