From f490b954401a6aa1faf85d6d1afd652524e63301 Mon Sep 17 00:00:00 2001 From: Kyle Milloy <k@fanda.io> Date: Tue, 23 Jan 2024 20:31:23 -0700 Subject: [PATCH 1/5] change symbol for replacement --- src/GoogleTranslate.php | 14 +++++++------- tests/TranslationTest.php | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 6e55912..4b0bc4a 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -119,7 +119,7 @@ class GoogleTranslate public function __construct(string $target = 'en', string $source = null, array $options = [], TokenProviderInterface $tokenProvider = null, bool|string $preserveParameters = false) { $this->client = new Client(); - $this->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator) + $this->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator()) ->setOptions($options) // Options are already set in client constructor tho. ->setSource($source) ->setTarget($target) @@ -225,8 +225,8 @@ public function getLastDetectedSource(): ?string */ public static function trans(string $string, string $target = 'en', string $source = null, array $options = [], TokenProviderInterface $tokenProvider = null, bool|string $preserveParameters = false): ?string { - return (new self) - ->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator) + return (new self()) + ->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator()) ->setOptions($options) // Options are already set in client constructor tho. ->setSource($source) ->setTarget($target) @@ -257,7 +257,7 @@ public function translate(string $string): ?string // Extract replaceable keywords from string and transform to array for use later $replacements = $this->getParameters($string); - // Replace replaceable keywords with ${\d} for replacement later + // Replace replaceable keywords with #{\d} for replacement later $responseArray = $this->getResponse($this->extractParameters($string)); // Check if translation exists @@ -341,7 +341,7 @@ protected function extractParameters(string $string): string return $string; } - // Replace all matches of our pattern with ${\d} for replacement later + // Replace all matches of our pattern with #{\d} for replacement later return preg_replace_callback( $this->pattern, function ($matches) { @@ -349,7 +349,7 @@ function ($matches) { $index++; - return '${' . $index . '}'; + return '#{' . $index . '}'; }, $string ); @@ -365,7 +365,7 @@ function ($matches) { protected function injectParameters(string $string, array $replacements): string { return preg_replace_callback( - '/\${(\d+)}/', + '/\#{(\d+)}/', fn($matches) => $replacements[$matches[1]], $string ); diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 8765a7b..0b2daf6 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -34,8 +34,8 @@ public function testTranslationKeyExtraction(): void $resultOne = GoogleTranslate::trans('Hello :name how are :type_of_greeting?', 'fr', 'en', preserveParameters: true); $resultTwo = $this->tr->setSource('en')->setTarget('fr')->preserveParameters()->translate('Hello :name, how are :type_of_greeting?'); - $this->assertEquals('Bonjour :name, comment vont :type_of_greeting ?', $resultOne, 'Translation should be correct with proper key extraction.'); - $this->assertEquals('Bonjour :name, comment vont :type_of_greeting ?', $resultTwo, 'Translation should be correct with proper key extraction.'); + $this->assertEquals('Bonjour :name, comment va :type_of_greeting ?', $resultOne, 'Translation should be correct with proper key extraction.'); + $this->assertEquals('Bonjour :name, comment va :type_of_greeting ?', $resultTwo, 'Translation should be correct with proper key extraction.'); } public function testCanIgnoreTranslationKeyExtraction(): void @@ -52,8 +52,8 @@ public function testCanCustomizeExtractionPattern(): void $resultOne = GoogleTranslate::trans('Hello {{name}}, how are {{type_of_greeting}}?', 'fr', 'en', preserveParameters: '/\{\{([^}]+)\}\}/'); $resultTwo = $this->tr->setSource('en')->setTarget('fr')->preserveParameters('/\{\{([^}]+)\}\}/')->translate('Hello {{name}}, how are {{type_of_greeting}}?'); - $this->assertEquals('Bonjour {{name}}, comment vont {{type_of_greeting}} ?', $resultOne, 'Translation should be correct and ignores key extraction if not set.'); - $this->assertEquals('Bonjour {{name}}, comment vont {{type_of_greeting}} ?', $resultTwo, 'Translation should be correct and ignores key extraction if not set.'); + $this->assertEquals('Bonjour {{name}}, comment va {{type_of_greeting}} ?', $resultOne, 'Translation should be correct and ignores key extraction if not set.'); + $this->assertEquals('Bonjour {{name}}, comment va {{type_of_greeting}} ?', $resultTwo, 'Translation should be correct and ignores key extraction if not set.'); } public function testNewerLanguageTranslation(): void From 8a9f7144ce9a9cb2e2e651b33aed0aee44c0aa4c Mon Sep 17 00:00:00 2001 From: Kyle Milloy <k@fanda.io> Date: Tue, 23 Jan 2024 20:32:42 -0700 Subject: [PATCH 2/5] style --- src/GoogleTranslate.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 4b0bc4a..474effc 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -118,8 +118,8 @@ class GoogleTranslate */ public function __construct(string $target = 'en', string $source = null, array $options = [], TokenProviderInterface $tokenProvider = null, bool|string $preserveParameters = false) { - $this->client = new Client(); - $this->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator()) + $this->client = new Client; + $this->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator) ->setOptions($options) // Options are already set in client constructor tho. ->setSource($source) ->setTarget($target) @@ -225,8 +225,8 @@ public function getLastDetectedSource(): ?string */ public static function trans(string $string, string $target = 'en', string $source = null, array $options = [], TokenProviderInterface $tokenProvider = null, bool|string $preserveParameters = false): ?string { - return (new self()) - ->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator()) + return (new self) + ->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator) ->setOptions($options) // Options are already set in client constructor tho. ->setSource($source) ->setTarget($target) From 4e9388bd0fb0bdfa0df2931c16f2b2e71fa205de Mon Sep 17 00:00:00 2001 From: Kyle Milloy <k@fanda.io> Date: Tue, 23 Jan 2024 20:33:08 -0700 Subject: [PATCH 3/5] match style --- src/GoogleTranslate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 474effc..efc1e37 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -118,7 +118,7 @@ class GoogleTranslate */ public function __construct(string $target = 'en', string $source = null, array $options = [], TokenProviderInterface $tokenProvider = null, bool|string $preserveParameters = false) { - $this->client = new Client; + $this->client = new Client(); $this->setTokenProvider($tokenProvider ?? new GoogleTokenGenerator) ->setOptions($options) // Options are already set in client constructor tho. ->setSource($source) From 45410934e73712ab7caf39bf8bc1afb09f2ac868 Mon Sep 17 00:00:00 2001 From: Kyle Milloy <k@fanda.io> Date: Tue, 23 Jan 2024 20:42:32 -0700 Subject: [PATCH 4/5] update test to include specific french example --- .phpunit.result.cache | 1 + tests/TranslationTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..850ef22 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testItProperlyTranslateStringsInFrenchThatWouldOtherwiseCauseIssues":3},"times":{"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testRateLimitException":0.668,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testRateLimitCaptchaException":0.483,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testLargeTextException":0.474,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testTranslationRequestException":0.486,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testTranslationDecodingException":0.501,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testInheritanceForUnexpectedValueException":0.59,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testInheritanceForErrorException":0.647,"Stichoza\\GoogleTranslate\\Tests\\LanguageDetectionTest::testSingleWord":0.726,"Stichoza\\GoogleTranslate\\Tests\\LanguageDetectionTest::testSingleSentence":0.327,"Stichoza\\GoogleTranslate\\Tests\\LanguageDetectionTest::testMultipleSentence":0.344,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testTranslation":0.161,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testTranslationEquality":0.361,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testTranslationKeyExtraction":0.371,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testCanIgnoreTranslationKeyExtraction":0.301,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testCanCustomizeExtractionPattern":0.45,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testNewerLanguageTranslation":0.256,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testUTF16Translation":0.311,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testLargeTextTranslation":0.266,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testRawResponse":0.16,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testItProperlyTranslateStringsInFrenchThatWouldOtherwiseCauseIssues":0.451,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testIsValidLocale":0.002,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testSetOptions":0.358,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testSetUrl":0.145,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testSetClient":0.001}} \ No newline at end of file diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 0b2daf6..f7c6dd0 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -87,4 +87,16 @@ public function testRawResponse(): void $this->assertIsArray($rawResult, 'Method getResponse() should return an array'); } + + /** + * @see https://github.com/Stichoza/google-translate-php/issues/201 + */ + public function testItProperlyTranslateStringsInFrenchThatWouldOtherwiseCauseIssues(): void + { + $resultOne = $this->tr->setSource('en')->setTarget('fr')->translate('What is :real_q_encoded?'); + $resultTwo = $this->tr->setSource('en')->setTarget('fr')->preserveParameters('#\{([^}]+)}#')->translate('What is {real_q_encoded}?'); + + $this->assertEquals('Qu\'est-ce que :real_q_encoded ?', $resultOne, 'Translation should be correct.'); + $this->assertEquals('Qu\'est-ce que {real_q_encoded} ?', $resultTwo, 'Translation should be correct.'); + } } From be2446b16ea94f4f6ad0baff4ff596a635fd92a1 Mon Sep 17 00:00:00 2001 From: Kyle Milloy <k@fanda.io> Date: Tue, 23 Jan 2024 20:47:27 -0700 Subject: [PATCH 5/5] Delete .phpunit.result.cache --- .phpunit.result.cache | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index 850ef22..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"defects":{"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testItProperlyTranslateStringsInFrenchThatWouldOtherwiseCauseIssues":3},"times":{"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testRateLimitException":0.668,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testRateLimitCaptchaException":0.483,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testLargeTextException":0.474,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testTranslationRequestException":0.486,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testTranslationDecodingException":0.501,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testInheritanceForUnexpectedValueException":0.59,"Stichoza\\GoogleTranslate\\Tests\\ExceptionTest::testInheritanceForErrorException":0.647,"Stichoza\\GoogleTranslate\\Tests\\LanguageDetectionTest::testSingleWord":0.726,"Stichoza\\GoogleTranslate\\Tests\\LanguageDetectionTest::testSingleSentence":0.327,"Stichoza\\GoogleTranslate\\Tests\\LanguageDetectionTest::testMultipleSentence":0.344,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testTranslation":0.161,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testTranslationEquality":0.361,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testTranslationKeyExtraction":0.371,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testCanIgnoreTranslationKeyExtraction":0.301,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testCanCustomizeExtractionPattern":0.45,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testNewerLanguageTranslation":0.256,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testUTF16Translation":0.311,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testLargeTextTranslation":0.266,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testRawResponse":0.16,"Stichoza\\GoogleTranslate\\Tests\\TranslationTest::testItProperlyTranslateStringsInFrenchThatWouldOtherwiseCauseIssues":0.451,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testIsValidLocale":0.002,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testSetOptions":0.358,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testSetUrl":0.145,"Stichoza\\GoogleTranslate\\Tests\\UtilityTest::testSetClient":0.001}} \ No newline at end of file