|
1 | 1 | <?php |
2 | 2 |
|
3 | | -require __DIR__ . '/vendor/autoload.php'; |
| 3 | +# [START getting_started_background_translate] |
| 4 | +# [START getting_started_background_translate_setup] |
| 5 | +use Google\Cloud\Firestore\FirestoreClient; |
| 6 | +use Google\Cloud\Firestore\Transaction; |
| 7 | +use Google\Cloud\Translate\TranslateClient; |
4 | 8 |
|
5 | | -use Symfony\Component\HttpFoundation\Request; |
| 9 | +# [END getting_started_background_translate_setup] |
6 | 10 |
|
7 | | -(function () { |
8 | | - $target = getenv('FUNCTION_TARGET', true); |
9 | | - if ($target === false) { |
10 | | - throw new \Exception('FUNCTION_TARGET is not set'); |
| 11 | +/** |
| 12 | + * @param array $data { |
| 13 | + * The PubSub message data containing text and target language. |
| 14 | + * |
| 15 | + * @type string $text |
| 16 | + * The full text to translate. |
| 17 | + * @type string $language |
| 18 | + * The target language for the translation. |
| 19 | + * } |
| 20 | + */ |
| 21 | +function translateString(array $data) |
| 22 | +{ |
| 23 | + if (empty($data['language']) || empty($data['text'])) { |
| 24 | + throw new Exception('Error parsing translation data'); |
11 | 25 | } |
12 | 26 |
|
13 | | - $request = Request::createFromGlobals(); |
14 | | - $message = json_decode($request->getContent(), true); |
15 | | - if (empty($message['message']['data'])) { |
16 | | - throw new \Exception('No message received'); |
17 | | - } |
18 | | - $data = json_decode(base64_decode($message['message']['data']), true); |
19 | | - if (!$data) { |
20 | | - throw new \Exception('Error decoding data from message'); |
21 | | - } |
22 | | - call_user_func_array($target, [$data]); |
23 | | -})(); |
| 27 | + # [START getting_started_background_translate_init] |
| 28 | + $firestore = new FirestoreClient(); |
| 29 | + $translate = new TranslateClient(); |
| 30 | + |
| 31 | + $translation = [ |
| 32 | + 'original' => $data['text'], |
| 33 | + 'lang' => $data['language'], |
| 34 | + ]; |
| 35 | + # [END getting_started_background_translate_init] |
| 36 | + |
| 37 | + # [START getting_started_background_translate_transaction] |
| 38 | + $docId = sprintf('%s:%s', $data['language'], base64_encode($data['text'])); |
| 39 | + $docRef = $firestore->collection('translations')->document($docId); |
| 40 | + |
| 41 | + $firestore->runTransaction( |
| 42 | + function (Transaction $transaction) use ($translate, $translation, $docRef) { |
| 43 | + $snapshot = $transaction->snapshot($docRef); |
| 44 | + if ($snapshot->exists()) { |
| 45 | + return; // Do nothing if the document already exists |
| 46 | + } |
| 47 | + |
| 48 | + # [START getting_started_background_translate_string] |
| 49 | + $result = $translate->translate($translation['original'], [ |
| 50 | + 'target' => $translation['lang'], |
| 51 | + ]); |
| 52 | + # [END getting_started_background_translate_string] |
| 53 | + $transaction->set($docRef, $translation + [ |
| 54 | + 'translated' => $result['text'], |
| 55 | + 'originalLang' => $result['source'], |
| 56 | + ]); |
| 57 | + } |
| 58 | + ); |
| 59 | + # [END getting_started_background_translate_transaction] |
| 60 | + |
| 61 | + echo "Done."; |
| 62 | +} |
| 63 | +# [END getting_started_background_translate] |
0 commit comments