Skip to content

Commit

Permalink
Merge pull request #202 from kylemilloy/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Stichoza committed Jan 24, 2024
2 parents 0d3de08 + be2446b commit bd9ef8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -341,15 +341,15 @@ 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) {
static $index = -1;

$index++;

return '${' . $index . '}';
return '#{' . $index . '}';
},
$string
);
Expand All @@ -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
);
Expand Down
20 changes: 16 additions & 4 deletions tests/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.');
}
}

0 comments on commit bd9ef8a

Please sign in to comment.