1010use Pollora \Pollingo \DTO \TranslationGroup ;
1111use Pollora \Pollingo \DTO \TranslationString ;
1212use 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
164174Your task is to translate text while preserving meaning and context.
165175
166176Important 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
1681782. Preserve the meaning and context of each string
1691793. Use appropriate translations based on context
1701804. 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-
196197IMPORTANT: Return ONLY the JSON object, no other text or explanations.
197198PROMPT;
198199 }
0 commit comments