Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (french): replace ${\d} delimiter with #{\d} to resolve currency bugs #202

Merged
merged 5 commits into from Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/GoogleTranslate.php
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
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.');
}
}