Skip to content

Commit dd13d7a

Browse files
committed
Simplify the prompt && increase timeout
1 parent 3fe1c10 commit dd13d7a

3 files changed

Lines changed: 16 additions & 49 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"openai-php/client": "^0.8.0",
1616
"patrickschur/language-detection": "^5.0",
1717
"symfony/http-client": "^6.0|^7.0",
18-
"vlucas/phpdotenv": "^5.6"
18+
"vlucas/phpdotenv": "^5.6",
19+
"guzzlehttp/guzzle": "^7.0"
1920
},
2021
"require-dev": {
2122
"laravel/pint": "^1.18.1",

src/Services/OpenAITranslator.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Pollora\Pollingo\DTO\TranslationGroup;
1111
use Pollora\Pollingo\DTO\TranslationString;
1212
use RuntimeException;
13+
use GuzzleHttp\Client as GuzzleClient;
1314

1415
/**
1516
* @template TKey of string
@@ -24,9 +25,14 @@ final class OpenAITranslator implements Translator
2425

2526
public function __construct(
2627
string $apiKey,
27-
private readonly string $model = 'gpt-4o',
28+
private readonly string $model = 'gpt-4',
29+
private readonly int $timeout = 30 // default timeout in seconds
2830
) {
29-
$this->client = (new Factory())->withApiKey($apiKey)->make();
31+
$httpClient = new GuzzleClient(['timeout' => $this->timeout]);
32+
$this->client = (new Factory())
33+
->withApiKey($apiKey)
34+
->withHttpClient($httpClient)
35+
->make();
3036
$this->languageCodeService = new LanguageCodeService();
3137
}
3238

@@ -82,7 +88,7 @@ public function translate(array $groups, string $targetLanguage, ?string $source
8288
throw new RuntimeException('Invalid JSON in response');
8389
}
8490

85-
// Verify translations are valid
91+
// Verify that translations are different from the original text
8692
foreach ($translations as $key => $translation) {
8793
if (! isset($strings[$key])) {
8894
throw new RuntimeException(sprintf(
@@ -95,6 +101,10 @@ public function translate(array $groups, string $targetLanguage, ?string $source
95101
if (! is_string($translation)) {
96102
throw new RuntimeException("Invalid translation for key '{$key}': expected string, got ".gettype($translation));
97103
}
104+
105+
if (mb_strtolower($translation) === mb_strtolower($strings[$key]['text'])) {
106+
throw new RuntimeException("Translation for '{$key}' is the same as the original text");
107+
}
98108
}
99109

100110
// Verify all strings are translated
@@ -164,7 +174,7 @@ private function getSystemPrompt(): string
164174
Your task is to translate text while preserving meaning and context.
165175
166176
Important rules to follow:
167-
1. Translate the text to the target language, keeping the original text if it's already appropriate in the target language
177+
1. Always translate the text to the target language, never return it unchanged
168178
2. Preserve the meaning and context of each string
169179
3. Use appropriate translations based on context
170180
4. Return ONLY a valid JSON object with translations, nothing else
@@ -184,15 +194,6 @@ private function getSystemPrompt(): string
184194
"action": "Sauvegarder"
185195
}
186196
187-
Common translations from English to French:
188-
- "Hello" → "Bonjour" or "Salut"
189-
- "Save" → "Sauvegarder" or "Enregistrer"
190-
- "Welcome" → "Bienvenue"
191-
- "Error occurred" → "Une erreur est survenue"
192-
- "Cancel" → "Annuler"
193-
- "Success" → "Succès"
194-
- "Operation completed" → "Opération terminée"
195-
196197
IMPORTANT: Return ONLY the JSON object, no other text or explanations.
197198
PROMPT;
198199
}

tests/Feature/TranslationTest.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -126,38 +126,3 @@ public function translate(array $groups, string $targetLanguage, ?string $source
126126

127127
expect($translation)->toBe('translated_Welcome');
128128
});
129-
130-
test('it allows translations that are similar to the original text', function () {
131-
$similarTranslator = new class implements Translator {
132-
public function translate(array $groups, string $targetLanguage, ?string $sourceLanguage = null, ?string $globalContext = null): array
133-
{
134-
$result = [];
135-
foreach ($groups as $groupName => $group) {
136-
$translatedStrings = [];
137-
foreach ($group->getStrings() as $key => $string) {
138-
// For some words like "OK" or "Menu", the translation might be the same in many languages
139-
$translatedStrings[$key] = new TranslationString(
140-
text: $string->getText(),
141-
translatedText: $string->getText(), // Same as original
142-
context: $string->getContext(),
143-
);
144-
}
145-
$result[$groupName] = new TranslationGroup($groupName, $translatedStrings);
146-
}
147-
148-
return $result;
149-
}
150-
};
151-
152-
$pollingo = Pollingo::make(translator: $similarTranslator);
153-
$translation = $pollingo
154-
->to('fr')
155-
->group('ui', [
156-
'ok' => 'OK',
157-
'menu' => 'Menu',
158-
])
159-
->translate();
160-
161-
expect($translation['ui']['ok'])->toBe('OK');
162-
expect($translation['ui']['menu'])->toBe('Menu');
163-
});

0 commit comments

Comments
 (0)