|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * File Translations API Example |
| 5 | + * |
| 6 | + * This example demonstrates how to use the File Translations API to: |
| 7 | + * 1. Upload a file for machine translation |
| 8 | + * 2. Initiate translation to multiple target locales |
| 9 | + * 3. Poll translation progress |
| 10 | + * 4. Download translated files |
| 11 | + * 5. Download all translations as ZIP |
| 12 | + * 6. Cancel a translation (optional) |
| 13 | + * |
| 14 | + * Usage: |
| 15 | + * php file-translations-example.php --account-uid=XXX --user-id=XXX --secret-key=XXX [--file-path=path/to/file.json] |
| 16 | + */ |
| 17 | + |
| 18 | +require_once '../vendor/autoload.php'; |
| 19 | + |
| 20 | +use Smartling\AuthApi\AuthTokenProvider; |
| 21 | +use Smartling\FileTranslations\FileTranslationsApi; |
| 22 | +use Smartling\FileTranslations\Params\TranslateFileParameters; |
| 23 | + |
| 24 | +// Parse command line arguments |
| 25 | +$options = getopt('', [ |
| 26 | + 'account-uid:', |
| 27 | + 'user-id:', |
| 28 | + 'secret-key:', |
| 29 | + 'file-path::', |
| 30 | +]); |
| 31 | + |
| 32 | +if (!isset($options['account-uid']) || !isset($options['user-id']) || !isset($options['secret-key'])) { |
| 33 | + echo "Usage: php file-translations-example.php --account-uid=XXX --user-id=XXX --secret-key=XXX [--file-path=path/to/file.json]\n"; |
| 34 | + exit(1); |
| 35 | +} |
| 36 | + |
| 37 | +$accountUid = $options['account-uid']; |
| 38 | +$userId = $options['user-id']; |
| 39 | +$secretKey = $options['secret-key']; |
| 40 | +$filePath = $options['file-path'] ?? __DIR__ . '/../tests/resources/test-fts.json'; |
| 41 | + |
| 42 | +// Verify file exists |
| 43 | +if (!file_exists($filePath)) { |
| 44 | + echo "Error: File not found: {$filePath}\n"; |
| 45 | + exit(1); |
| 46 | +} |
| 47 | + |
| 48 | +try { |
| 49 | + echo "=== File Translations API Example ===\n\n"; |
| 50 | + |
| 51 | + // Step 1: Initialize API client |
| 52 | + echo "1. Initializing API client...\n"; |
| 53 | + $authProvider = AuthTokenProvider::create($userId, $secretKey); |
| 54 | + $api = FileTranslationsApi::create($authProvider, $accountUid); |
| 55 | + echo " ✓ API client initialized\n\n"; |
| 56 | + |
| 57 | + // Step 2: Upload file |
| 58 | + echo "2. Uploading file: {$filePath}\n"; |
| 59 | + $fileName = basename($filePath); |
| 60 | + $fileType = pathinfo($filePath, PATHINFO_EXTENSION); |
| 61 | + |
| 62 | + $uploadResult = $api->uploadFile($filePath, $fileName, $fileType); |
| 63 | + $fileUid = $uploadResult['fileUid']; |
| 64 | + echo " ✓ File uploaded successfully\n"; |
| 65 | + echo " File UID: {$fileUid}\n\n"; |
| 66 | + |
| 67 | + // Step 3: Initiate translation |
| 68 | + echo "3. Initiating translation to Spanish, French, and German...\n"; |
| 69 | + $translateParams = new TranslateFileParameters(); |
| 70 | + $translateParams |
| 71 | + ->setSourceLocaleId('en') |
| 72 | + ->setTargetLocaleIds(['es', 'fr', 'de']); |
| 73 | + |
| 74 | + $translateResult = $api->translateFile($fileUid, $translateParams); |
| 75 | + $mtUid = $translateResult['mtUid']; |
| 76 | + echo " ✓ Translation initiated\n"; |
| 77 | + echo " MT UID: {$mtUid}\n\n"; |
| 78 | + |
| 79 | + // Step 4: Poll translation progress |
| 80 | + echo "4. Polling translation progress...\n"; |
| 81 | + $maxAttempts = 60; |
| 82 | + $pollInterval = 5; // seconds |
| 83 | + $completed = false; |
| 84 | + |
| 85 | + for ($i = 0; $i < $maxAttempts; $i++) { |
| 86 | + $progress = $api->getTranslationProgress($fileUid, $mtUid); |
| 87 | + $status = $progress['state']; |
| 88 | + |
| 89 | + echo " Status: {$status}"; |
| 90 | + |
| 91 | + if (isset($progress['completedLocales'])) { |
| 92 | + $completedCount = count($progress['completedLocales']); |
| 93 | + $totalCount = count($translateParams->exportToArray()['targetLocaleIds']); |
| 94 | + echo " ({$completedCount}/{$totalCount} locales completed)"; |
| 95 | + } |
| 96 | + |
| 97 | + echo "\n"; |
| 98 | + |
| 99 | + if ($status === 'COMPLETED') { |
| 100 | + $completed = true; |
| 101 | + echo " ✓ Translation completed!\n\n"; |
| 102 | + break; |
| 103 | + } elseif ($status === 'FAILED') { |
| 104 | + echo " ✗ Translation failed\n"; |
| 105 | + print_r($progress); |
| 106 | + exit(1); |
| 107 | + } elseif ($status === 'CANCELLED') { |
| 108 | + echo " ✗ Translation was cancelled\n"; |
| 109 | + exit(1); |
| 110 | + } |
| 111 | + |
| 112 | + if ($i < $maxAttempts - 1) { |
| 113 | + sleep($pollInterval); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (!$completed) { |
| 118 | + echo " ⚠ Translation not completed within expected time. Continuing anyway...\n\n"; |
| 119 | + } |
| 120 | + |
| 121 | + // Step 5: Download translated files |
| 122 | + echo "5. Downloading translated files...\n"; |
| 123 | + $targetLocales = ['es', 'fr', 'de']; |
| 124 | + |
| 125 | + foreach ($targetLocales as $locale) { |
| 126 | + try { |
| 127 | + $translatedContent = $api->downloadTranslatedFile($fileUid, $mtUid, $locale); |
| 128 | + $outputPath = "/tmp/translated-{$locale}-{$fileName}"; |
| 129 | + file_put_contents($outputPath, $translatedContent); |
| 130 | + echo " ✓ Downloaded {$locale}: {$outputPath}\n"; |
| 131 | + |
| 132 | + // Show first 100 chars of content |
| 133 | + $preview = substr($translatedContent, 0, 100); |
| 134 | + if (strlen($translatedContent) > 100) { |
| 135 | + $preview .= '...'; |
| 136 | + } |
| 137 | + echo " Preview: {$preview}\n"; |
| 138 | + } catch (Exception $e) { |
| 139 | + echo " ✗ Failed to download {$locale}: {$e->getMessage()}\n"; |
| 140 | + } |
| 141 | + } |
| 142 | + echo "\n"; |
| 143 | + |
| 144 | + // Step 6: Download all translations as ZIP |
| 145 | + echo "6. Downloading all translations as ZIP...\n"; |
| 146 | + try { |
| 147 | + $zipContent = $api->downloadAllTranslationsZip($fileUid, $mtUid); |
| 148 | + $zipPath = "/tmp/all-translations-{$fileUid}.zip"; |
| 149 | + file_put_contents($zipPath, $zipContent); |
| 150 | + echo " ✓ Downloaded ZIP: {$zipPath}\n"; |
| 151 | + echo " Size: " . strlen($zipContent) . " bytes\n\n"; |
| 152 | + } catch (Exception $e) { |
| 153 | + echo " ✗ Failed to download ZIP: {$e->getMessage()}\n\n"; |
| 154 | + } |
| 155 | + |
| 156 | + // Optional: Demonstrate cancellation with a new translation |
| 157 | + echo "7. (Optional) Demonstrating translation cancellation...\n"; |
| 158 | + echo " Uploading another file...\n"; |
| 159 | + $uploadResult2 = $api->uploadFile($filePath, "cancel-demo-{$fileName}", $fileType); |
| 160 | + $fileUid2 = $uploadResult2['fileUid']; |
| 161 | + |
| 162 | + echo " Starting translation to many locales...\n"; |
| 163 | + $translateParams2 = new TranslateFileParameters(); |
| 164 | + $translateParams2 |
| 165 | + ->setSourceLocaleId('en') |
| 166 | + ->setTargetLocaleIds(['es', 'fr', 'de', 'it', 'pt', 'ja', 'zh', 'ru']); |
| 167 | + |
| 168 | + $translateResult2 = $api->translateFile($fileUid2, $translateParams2); |
| 169 | + $mtUid2 = $translateResult2['mtUid']; |
| 170 | + |
| 171 | + echo " Cancelling translation...\n"; |
| 172 | + $api->cancelFileTranslation($fileUid2, $mtUid2); |
| 173 | + echo " ✓ Cancellation request sent\n"; |
| 174 | + |
| 175 | + sleep(2); |
| 176 | + $progress = $api->getTranslationProgress($fileUid2, $mtUid2); |
| 177 | + echo " Final status: {$progress['state']}\n\n"; |
| 178 | + |
| 179 | + echo "=== Example completed successfully ===\n"; |
| 180 | + |
| 181 | +} catch (Exception $e) { |
| 182 | + echo "\nError: {$e->getMessage()}\n"; |
| 183 | + if (method_exists($e, 'getTraceAsString')) { |
| 184 | + echo $e->getTraceAsString() . "\n"; |
| 185 | + } |
| 186 | + exit(1); |
| 187 | +} |
0 commit comments