Skip to content

Commit

Permalink
Variable checks: check patterns only on specific file extensions (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored Jun 19, 2024
1 parent d9d6028 commit 7ec77e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
22 changes: 21 additions & 1 deletion app/classes/Transvision/AnalyseStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ public static function differences($tmx_source, $tmx_target, $repo, $ignored_str
// %1$S or %S. %1$0.S and %0.S are valid too
'printf' => '/(%(?:[0-9]+\$){0,1}(?:[0-9].){0,1}([sS]))/',
// $BrandShortName, but not "My%1$SFeeds-%2$S.opml" or "{ $brandShortName }"
'properties' => '/(?<!%[0-9]|\{\s|\{)(\$[A-Za-z0-9\.]+)\b/',
'properties' => '/(?<!%[0-9]|\{\s|\{)(\$[A-Za-z0-9\._-]+)\b/',
// %1$s or %s. %d
'xml_android' => '/(%(?:[0-9]+\$){0,1}([sd]))/',
// %1, %2, etc.
'xliff-qt' => '/(%[0-9])/',
];
$pattern_extensions = [
'ios' => 'xml',
'l10njs' => 'properties',
'printf' => 'properties',
'xml_android' => 'xml',
'xliff-qt' => 'xliff',
];
$repo_patterns = Project::$repos_info[$repo]['variable_patterns'];

$patterns = array_filter($patterns, function($k) use ($repo_patterns) {
Expand All @@ -71,7 +78,20 @@ public static function differences($tmx_source, $tmx_target, $repo, $ignored_str

foreach ($patterns as $pattern_name => $pattern) {
foreach ($tmx_source as $key => $source) {
$file_path = explode(':', $key)[0];
$file_extension = explode('.', $file_path);
$file_extension = end($file_extension);

// If not specified, run all patterns
$check_pattern = True;
if (array_key_exists($file_extension, $patterns) || in_array($file_extension, array_values($pattern_extensions))) {
// Run current pattern only on specific file extensions
$valid_extension = $pattern_extensions[$pattern_name] ?? ".{$pattern_name}";
$check_pattern = Strings::endsWith($file_path, $valid_extension);
}

if (isset($tmx_target[$key])
&& $check_pattern
&& $tmx_target[$key] != ''
&& ! in_array($key, $ignored_strings)) {
/*
Expand Down
24 changes: 16 additions & 8 deletions tests/units/Transvision/AnalyseStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ public function differencesDP()
[],
[],
],
[
// .properties checks should not run in FTL messages
['browser.ftl:foobar16a1' => '{ $brandname } installed $properties_var'],
['browser.ftl:foobar16a1' => '{ $brandname } installato'],
'gecko_strings',
[],
[],
],
[
// Difference spacing in variable for FTL (not an error)
['browser:foobar16a2' => '{$brandname} installed'],
Expand Down Expand Up @@ -253,24 +261,24 @@ public function differencesDP()
],
[
// Mispelled variable
['ios:foobar1' => 'Introductory slide %1$@ of %2$@'],
['ios:foobar1' => 'Introduzione (passaggio %1$@ di %$@)'],
['iostest:foobar1' => 'Introductory slide %1$@ of %2$@'],
['iostest:foobar1' => 'Introduzione (passaggio %1$@ di %$@)'],
'firefox_ios',
[],
['ios:foobar1'],
['iostest:foobar1'],
],
[
// Missing variable
['ios:foobar2' => 'Do you want to save the password on %@?'],
['ios:foobar2' => 'Salvare la password?'],
['iostest:foobar2' => 'Do you want to save the password on %@?'],
['iostest:foobar2' => 'Salvare la password?'],
'firefox_ios',
[],
['ios:foobar2'],
['iostest:foobar2'],
],
[
// Changed order, not an error
['ios:foobar3' => 'Introductory slide %1$@ of %2$@'],
['ios:foobar3' => 'Introduzione (passaggio %2$@ di %1$@)'],
['iostest:foobar3' => 'Introductory slide %1$@ of %2$@'],
['iostest:foobar3' => 'Introduzione (passaggio %2$@ di %1$@)'],
'firefox_ios',
[],
[],
Expand Down

0 comments on commit 7ec77e2

Please sign in to comment.