-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create view to display translation inconsistencies
- Loading branch information
Showing
14 changed files
with
486 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
namespace Transvision; | ||
|
||
/** | ||
* Consistency class | ||
* | ||
* Functions used to analyze translation consistency | ||
* | ||
* @package Transvision | ||
*/ | ||
class Consistency | ||
{ | ||
/** | ||
* Find duplicated strings (strings with the same text, ignoring case). | ||
* | ||
* @param array $strings_array Array of strings in the form | ||
* string_id => string_value | ||
* | ||
* @return array Array of duplicated strings, same format as the | ||
* input array | ||
*/ | ||
public static function findDuplicates($strings_array) | ||
{ | ||
$duplicates = []; | ||
// Use array_filter to exclude empty strings | ||
$strings_array = array_filter($strings_array); | ||
asort($strings_array); | ||
|
||
$previous_key = ''; | ||
$previous_value = ''; | ||
foreach ($strings_array as $key => $value) { | ||
if (strcasecmp($previous_value, $value) === 0) { | ||
$duplicates[$previous_key] = $previous_value; | ||
$duplicates[$key] = $value; | ||
} | ||
$previous_value = $value; | ||
$previous_key = $key; | ||
} | ||
|
||
return $duplicates; | ||
} | ||
|
||
/** | ||
* Filter out strings that should not be evaluted for consistency. | ||
* | ||
* @param array $strings_array Array of strings in the form | ||
* string_id => string_value | ||
* @param string Repository identifier | ||
* | ||
* @return array Array of filtered strings, with known false positives | ||
* removed | ||
*/ | ||
public static function filterStrings($strings_array, $repo) | ||
{ | ||
if (in_array($repo, Project::getDesktopRepositories())) { | ||
$repository_type = 'desktop'; | ||
} elseif (in_array($repo, Project::getGaiaRepositories())) { | ||
$repository_type = 'gaia'; | ||
} else { | ||
$repository_type = $repo; | ||
} | ||
|
||
// Determine if a string should be excluded | ||
$ignore_string = function ($key, $value) use ($repository_type) { | ||
// Ignore strings of length 1 (e.g. accesskeys) and empty strings. | ||
if (strlen($value) <= 1) { | ||
return true; | ||
} | ||
|
||
// Exclude CSS rules, "width:" or "height:" | ||
if (Strings::inString($value, ['width:', 'height:'])) { | ||
return true; | ||
} | ||
|
||
// Exclude CSS width values like '38em' | ||
if (preg_match('/[\d|\.]+em/', $value)) { | ||
return true; | ||
} | ||
|
||
if ($repository_type == 'gaia') { | ||
// Ignore plural forms containing [] in the key | ||
if (Strings::inString($key, ['[', ']'])) { | ||
return true; | ||
} | ||
|
||
// Ignore accessibility strings | ||
if (strpos($key, 'accessibility.properties') !== false) { | ||
return true; | ||
} | ||
} | ||
|
||
if ($repository_type == 'desktop') { | ||
/* | ||
Ignore some specific files: | ||
- AccessFu.properties: there are accessibility | ||
strings that remain identical for English in full and | ||
abbreviated form. | ||
- defines.inc (language pack attribution) | ||
- region.properties | ||
*/ | ||
if (Strings::inString($key, ['AccessFu.properties', 'defines.inc', 'region.properties'])) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
foreach ($strings_array as $key => $value) { | ||
if ($ignore_string($key, $value)) { | ||
unset($strings_array[$key]); | ||
} | ||
} | ||
|
||
return $strings_array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace Transvision; | ||
|
||
// Get requested repo and locale | ||
require_once INC . 'l10n-init.php'; | ||
|
||
include MODELS . 'consistency.php'; | ||
include VIEWS . 'consistency.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
namespace Transvision; | ||
|
||
// Build arrays for the search form, ignore mozilla.org | ||
$channel_selector = Utils::getHtmlSelectOptions( | ||
array_diff($repos_nice_names, ['mozilla.org']), | ||
$repo, | ||
true | ||
); | ||
|
||
$target_locales_list = Utils::getHtmlSelectOptions( | ||
Project::getRepositoryLocales($repo), | ||
$locale | ||
); | ||
|
||
$reference_locale = Project::getReferenceLocale($repo); | ||
|
||
// Set a default for the number of strings to display | ||
$strings_number = 0; | ||
|
||
$source_strings = Utils::getRepoStrings($reference_locale, $repo); | ||
// Ignore empty strings in translations | ||
$target_strings = array_filter(Utils::getRepoStrings($locale, $repo)); | ||
|
||
if (! empty($source_strings)) { | ||
// Remove known problematic strings | ||
$duplicated_strings_english = Consistency::filterStrings($source_strings, $repo); | ||
// Find strings that are identical in English | ||
$duplicated_strings_english = Consistency::findDuplicates($duplicated_strings_english); | ||
} | ||
|
||
if (! empty($duplicated_strings_english)) { | ||
$inconsistent_translations = []; | ||
$available_translations = []; | ||
|
||
/* | ||
Using CachingIterator to be able to look ahead during the foreach | ||
cycle, since it's an associative array. | ||
http://php.net/manual/en/class.cachingiterator.php | ||
*/ | ||
$collection = new \CachingIterator(new \ArrayIterator($duplicated_strings_english)); | ||
foreach ($collection as $key => $value) { | ||
// Ignore this string if is not available in the localization | ||
if (! isset($target_strings[$key])) { | ||
$available_translations = []; | ||
continue; | ||
} | ||
|
||
$available_translations[] = $target_strings[$key]; | ||
/* | ||
If the current English string is different from the previous one, | ||
or I am at the last element, I need to store it with all available | ||
translations collected so far. | ||
$collection->getInnerIterator()->current() stores the value of | ||
the next element. | ||
*/ | ||
if (! $collection->hasNext() || $collection->getInnerIterator()->current() != $value) { | ||
$available_translations = array_unique($available_translations); | ||
/* | ||
Store this string only if there's more than one translation. | ||
If there's only one, translations are consistent. | ||
*/ | ||
if (count($available_translations) > 1) { | ||
$inconsistent_translations[] = [ | ||
'source' => $value, | ||
'target' => $available_translations, | ||
]; | ||
} | ||
$available_translations = []; | ||
} | ||
} | ||
|
||
$strings_number = count($inconsistent_translations); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.