Skip to content

Commit bd9ef8a

Browse files
authored
Merge pull request #202 from kylemilloy/master
2 parents 0d3de08 + be2446b commit bd9ef8a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/GoogleTranslate.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function translate(string $string): ?string
257257
// Extract replaceable keywords from string and transform to array for use later
258258
$replacements = $this->getParameters($string);
259259

260-
// Replace replaceable keywords with ${\d} for replacement later
260+
// Replace replaceable keywords with #{\d} for replacement later
261261
$responseArray = $this->getResponse($this->extractParameters($string));
262262

263263
// Check if translation exists
@@ -341,15 +341,15 @@ protected function extractParameters(string $string): string
341341
return $string;
342342
}
343343

344-
// Replace all matches of our pattern with ${\d} for replacement later
344+
// Replace all matches of our pattern with #{\d} for replacement later
345345
return preg_replace_callback(
346346
$this->pattern,
347347
function ($matches) {
348348
static $index = -1;
349349

350350
$index++;
351351

352-
return '${' . $index . '}';
352+
return '#{' . $index . '}';
353353
},
354354
$string
355355
);
@@ -365,7 +365,7 @@ function ($matches) {
365365
protected function injectParameters(string $string, array $replacements): string
366366
{
367367
return preg_replace_callback(
368-
'/\${(\d+)}/',
368+
'/\#{(\d+)}/',
369369
fn($matches) => $replacements[$matches[1]],
370370
$string
371371
);

tests/TranslationTest.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function testTranslationKeyExtraction(): void
3434
$resultOne = GoogleTranslate::trans('Hello :name how are :type_of_greeting?', 'fr', 'en', preserveParameters: true);
3535
$resultTwo = $this->tr->setSource('en')->setTarget('fr')->preserveParameters()->translate('Hello :name, how are :type_of_greeting?');
3636

37-
$this->assertEquals('Bonjour :name, comment vont :type_of_greeting ?', $resultOne, 'Translation should be correct with proper key extraction.');
38-
$this->assertEquals('Bonjour :name, comment vont :type_of_greeting ?', $resultTwo, 'Translation should be correct with proper key extraction.');
37+
$this->assertEquals('Bonjour :name, comment va :type_of_greeting ?', $resultOne, 'Translation should be correct with proper key extraction.');
38+
$this->assertEquals('Bonjour :name, comment va :type_of_greeting ?', $resultTwo, 'Translation should be correct with proper key extraction.');
3939
}
4040

4141
public function testCanIgnoreTranslationKeyExtraction(): void
@@ -52,8 +52,8 @@ public function testCanCustomizeExtractionPattern(): void
5252
$resultOne = GoogleTranslate::trans('Hello {{name}}, how are {{type_of_greeting}}?', 'fr', 'en', preserveParameters: '/\{\{([^}]+)\}\}/');
5353
$resultTwo = $this->tr->setSource('en')->setTarget('fr')->preserveParameters('/\{\{([^}]+)\}\}/')->translate('Hello {{name}}, how are {{type_of_greeting}}?');
5454

55-
$this->assertEquals('Bonjour {{name}}, comment vont {{type_of_greeting}} ?', $resultOne, 'Translation should be correct and ignores key extraction if not set.');
56-
$this->assertEquals('Bonjour {{name}}, comment vont {{type_of_greeting}} ?', $resultTwo, 'Translation should be correct and ignores key extraction if not set.');
55+
$this->assertEquals('Bonjour {{name}}, comment va {{type_of_greeting}} ?', $resultOne, 'Translation should be correct and ignores key extraction if not set.');
56+
$this->assertEquals('Bonjour {{name}}, comment va {{type_of_greeting}} ?', $resultTwo, 'Translation should be correct and ignores key extraction if not set.');
5757
}
5858

5959
public function testNewerLanguageTranslation(): void
@@ -87,4 +87,16 @@ public function testRawResponse(): void
8787

8888
$this->assertIsArray($rawResult, 'Method getResponse() should return an array');
8989
}
90+
91+
/**
92+
* @see https://github.com/Stichoza/google-translate-php/issues/201
93+
*/
94+
public function testItProperlyTranslateStringsInFrenchThatWouldOtherwiseCauseIssues(): void
95+
{
96+
$resultOne = $this->tr->setSource('en')->setTarget('fr')->translate('What is :real_q_encoded?');
97+
$resultTwo = $this->tr->setSource('en')->setTarget('fr')->preserveParameters('#\{([^}]+)}#')->translate('What is {real_q_encoded}?');
98+
99+
$this->assertEquals('Qu\'est-ce que :real_q_encoded ?', $resultOne, 'Translation should be correct.');
100+
$this->assertEquals('Qu\'est-ce que {real_q_encoded} ?', $resultTwo, 'Translation should be correct.');
101+
}
90102
}

0 commit comments

Comments
 (0)