Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Translation][TwigBridge] Implementing variables extraction in "translation:update" #38884

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

Closed
wants to merge 23 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06fba65
Implementing variables extraction in "translation:update"
liarco Oct 29, 2020
e2934b5
Applying patch from fabbot
liarco Oct 29, 2020
bedb0be
Fixing regression: Twig t() now works when domain is set with trans_d…
liarco Oct 30, 2020
87d542f
Fixing variables metadata overwriting previously set data (PHP extrac…
liarco Oct 30, 2020
8142f75
Updating existing tests to include variables
liarco Oct 30, 2020
a333f13
Applying patch from fabbot
liarco Oct 30, 2020
38d9e55
Supporting both long and short array syntax (PHP extractor)
liarco Oct 30, 2020
edb5aa9
Improving/fixing existing tests and adding variables tests (PHP extra…
liarco Oct 30, 2020
31f96a0
Adding variables tests (Twig extractor)
liarco Oct 31, 2020
5c2e782
Clearer naming
liarco Oct 31, 2020
c3e2e88
Avoid overwriting metadata completely when adding variables note (Twi…
liarco Oct 31, 2020
c50ce67
Supporting "t()" functions (Twig) without "|trans()" filter (e.g. as …
liarco Oct 31, 2020
6e4013c
Fixing duplicate notes. Extractors now keep the higher variables coun…
liarco Oct 31, 2020
3e0bb8e
Improving readability
liarco Oct 31, 2020
deb72d3
Always update variables with latest from code
liarco Oct 31, 2020
e8cee75
Fixing bug preventing update of variables from source in some cases
liarco Nov 13, 2020
63a52b9
Fixing unintended CS errors (thanks @wouterj)
liarco Nov 13, 2020
2a10f37
Applying patch from fabbot (reviewed manually)
liarco Dec 18, 2020
0a5366f
Minor fixes
liarco Mar 9, 2021
46d26ec
Using constants for metadata key and prefix
liarco Mar 9, 2021
1612c08
Fixing CI
liarco Mar 9, 2021
8ec6086
Improving code quality
liarco Mar 9, 2021
0025c52
Fixing static analysis (Psalm)
liarco Mar 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updating existing tests to include variables
  • Loading branch information
liarco committed Mar 9, 2021
commit 8142f75e2f9e6730d85b0969c8e44785e74b2871
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Translation\Tests\Extractor;

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Extractor\PhpExtractor;
use Symfony\Component\Translation\MessageCatalogue;
Expand Down Expand Up @@ -109,6 +110,78 @@ public function testExtraction($resource)
'typecast' => 'prefixtypecast',
],
];

// Expected metadata (variables)
$expectedVariables = [
'messages' => [
'translatable single-quoted key' => null,
'translatable double-quoted key' => null,
'translatable heredoc key' => null,
'translatable nowdoc key' => null,
"translatable double-quoted key with whitespace and escaped \$\n\" sequences" => null,
'translatable single-quoted key with whitespace and nonescaped \$\n\' sequences' => null,
'translatable single-quoted key with "quote mark at the end"' => null,
'translatable '.$expectedHeredoc => null,
'translatable '.$expectedNowdoc => null,
'translatable concatenated message with heredoc and nowdoc' => null,
'translatable default domain' => null,
'translatable-fqn single-quoted key' => null,
'translatable-fqn double-quoted key' => null,
'translatable-fqn heredoc key' => null,
'translatable-fqn nowdoc key' => null,
"translatable-fqn double-quoted key with whitespace and escaped \$\n\" sequences" => null,
'translatable-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences' => null,
'translatable-fqn single-quoted key with "quote mark at the end"' => null,
'translatable-fqn '.$expectedHeredoc => null,
'translatable-fqn '.$expectedNowdoc => null,
'translatable-fqn concatenated message with heredoc and nowdoc' => null,
'translatable-fqn default domain' => null,
'translatable-short single-quoted key' => null,
'translatable-short double-quoted key' => null,
'translatable-short heredoc key' => null,
'translatable-short nowdoc key' => null,
"translatable-short double-quoted key with whitespace and escaped \$\n\" sequences" => null,
'translatable-short single-quoted key with whitespace and nonescaped \$\n\' sequences' => null,
'translatable-short single-quoted key with "quote mark at the end"' => null,
'translatable-short '.$expectedHeredoc => null,
'translatable-short '.$expectedNowdoc => null,
'translatable-short concatenated message with heredoc and nowdoc' => null,
'translatable-short default domain' => null,
'single-quoted key' => null,
'double-quoted key' => null,
'heredoc key' => null,
'nowdoc key' => null,
"double-quoted key with whitespace and escaped \$\n\" sequences" => null,
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => null,
'single-quoted key with "quote mark at the end"' => null,
$expectedHeredoc => null,
$expectedNowdoc => null,
'concatenated message with heredoc and nowdoc' => null,
'default domain' => null,
],
'not_messages' => [
'translatable other-domain-test-no-params-short-array' => null,
'translatable other-domain-test-no-params-long-array' => null,
'translatable other-domain-test-params-short-array' => 'Available variables: foo',
'translatable other-domain-test-params-long-array' => 'Available variables: foo',
'translatable typecast' => 'Available variables: a',
'translatable-fqn other-domain-test-no-params-short-array' => null,
'translatable-fqn other-domain-test-no-params-long-array' => null,
'translatable-fqn other-domain-test-params-short-array' => 'Available variables: foo',
'translatable-fqn other-domain-test-params-long-array' => 'Available variables: foo',
'translatable-fqn typecast' => 'Available variables: a',
'translatable-short other-domain-test-no-params-short-array' => null,
'translatable-short other-domain-test-no-params-long-array' => null,
'translatable-short other-domain-test-params-short-array' => 'Available variables: foo',
'translatable-short other-domain-test-params-long-array' => 'Available variables: foo',
'translatable-short typecast' => 'Available variables: a',
'other-domain-test-no-params-short-array' => null,
'other-domain-test-no-params-long-array' => null,
'other-domain-test-params-short-array' => 'Available variables: foo',
'other-domain-test-params-long-array' => 'Available variables: foo',
'typecast' => 'Available variables: a',
],
];
$actualCatalogue = $catalogue->all();

$this->assertEquals($expectedCatalogue, $actualCatalogue);
Expand All @@ -128,6 +201,17 @@ public function testExtraction($resource)
$filename = str_replace(\DIRECTORY_SEPARATOR, '/', __DIR__).'/../fixtures/extractor/translation.html.php';
$this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('single-quoted key'));
$this->assertEquals(['sources' => [$filename.':37']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));

// Check variables metadata
foreach (array_keys($actualCatalogue) as $domain) {echo "[{$domain}]\n";
foreach (array_keys($actualCatalogue[$domain]) as $id) {
$this->assertTrue(array_key_exists($id, $expectedVariables[$domain]), 'Metadata for domain "'.$domain.'" and id "'.$id.'" was not expected!');

$extractedVariables = $this->getVariablesNoteContentFromMetadata($catalogue->getMetadata($id, $domain));

$this->assertEquals($expectedVariables[$domain][$id], $extractedVariables);
}
}
}

/**
Expand Down Expand Up @@ -175,4 +259,21 @@ public function resourcesProvider()
[new \ArrayObject($splFiles)],
];
}

private function getVariablesNoteContentFromMetadata(array $metadata)
{
/**
* $metadata = [
* 'notes' => [
* 0 => [
* 'category' => 'symfony-extractor-variables',
* 'content' => 'Available variables: var1, var2',
* ],
* ...
* ],
* ...
* ]
*/
return array_filter($metadata['notes'] ?? [], function ($note) { return $note['category'] === 'symfony-extractor-variables'; })[0]['content'] ?? null;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.