Skip to content
Extraits de code Groupes Projets
Valider b5caa5e2 rédigé par sgiehl's avatar sgiehl
Parcourir les fichiers

added translations filter to filter unnecassary whitespaces and linebreaks

parent c125af24
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/
namespace Piwik\Translate\Filter;
use Piwik\Translate\Filter\FilterAbstract;
/**
* @package Piwik
* @subpackage Piwik_Translate
*/
class UnnecassaryWhitespaces extends FilterAbstract
{
/**
* Filter the given translations
*
* @param array $translations
*
* @return array filtered translations
*
*/
public function filter($translations)
{
foreach ($translations AS $pluginName => $pluginTranslations) {
foreach ($pluginTranslations AS $key => $translation) {
$baseTranslation = $this->_baseTranslations[$pluginName][$key];
// remove excessive line breaks (and leading/trailing whitespace) from translations
$stringNoLineBreak = trim($translation);
$stringNoLineBreak = str_replace("\r", "", $stringNoLineBreak); # remove useless carrige renturns
$stringNoLineBreak = preg_replace('/(\n[ ]+)/', "\n", $stringNoLineBreak); # remove excessive white spaces
$stringNoLineBreak = preg_replace('/([\n]{2,})/', "\n\n", $stringNoLineBreak); # remove excessive line breaks
if (!isset($baseTranslation) || !substr_count($baseTranslation, "\n")) {
$stringNoLineBreak = preg_replace("/[\n]+/", " ", $stringNoLineBreak); # remove all line breaks if english string doesn't contain any
}
$stringNoLineBreak = preg_replace('/([ ]{2,})/', " ", $stringNoLineBreak); # remove excessive white spaces again as there might be there now, after removing line breaks
if ($translation !== $stringNoLineBreak) {
$this->_filteredData[$pluginName][$key] = $translation;
$translations[$pluginName][$key] = $stringNoLineBreak;
continue;
}
}
}
return $translations;
}
}
\ No newline at end of file
<?php
use Piwik\Translate\Filter\UnnecassaryWhitespaces;
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
class UnnecassaryWhitepsacesTest extends PHPUnit_Framework_TestCase
{
public function getFilterTestData()
{
return array(
// empty stays empty - nothing to filter
array(
array(),
array(),
array(),
array()
),
// no entites - nothing to filter
array(
array(
'test' => array(
'key' => "val\n\n\r\n\nue",
'test' => 'test'
)
),
array(
'test' => array(
'key' => "base val\n\nue",
'test' => 'test'
)
),
array(
'test' => array(
'key' => "val\n\nue",
'test' => 'test'
)
),
array(
'test' => array(
'key' => "val\n\n\r\n\nue",
)
),
),
// entities needs to be decodded
array(
array(
'test' => array(
'test' => 'test palim'
)
),
array(
'test' => array(
'test' => 'no line breaks'
)
),
array(
'test' => array(
'test' => 'test palim'
)
),
array(
'test' => array(
'test' => 'test palim'
)
),
),
array(
array(
'empty' => array(
'test' => "test\n\n\ntest"
),
),
array(
'empty' => array(
'test' => 'no line break'
),
),
array(
'empty' => array(
'test' => 'test test'
),
),
array(
'empty' => array(
'test' => "test\n\n\ntest"
),
),
),
array(
array(
'empty' => array(
'test' => "test\n \n\n test"
),
),
array(
'empty' => array(
'test' => 'no line break'
),
),
array(
'empty' => array(
'test' => 'test test'
),
),
array(
'empty' => array(
'test' => "test\n \n\n test"
),
),
),
array(
array(
'empty' => array(
'test' => "test\n \n\n test"
),
),
array(
'empty' => array(
'test' => "line\n break"
),
),
array(
'empty' => array(
'test' => "test\n\ntest"
),
),
array(
'empty' => array(
'test' => "test\n \n\n test"
),
),
),
);
}
/**
* @dataProvider getFilterTestData
* @group Core
* @group Translate
*/
public function testFilter($translations, $baseTranslations, $expected, $filteredData)
{
$filter = new UnnecassaryWhitespaces($baseTranslations);
$result = $filter->filter($translations);
$this->assertEquals($expected, $result);
$this->assertEquals($filteredData, $filter->getFilteredData());
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter