Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsgiehl <stefan@piwik.org>2013-08-30 20:39:11 +0400
committersgiehl <stefan@piwik.org>2013-09-04 02:15:14 +0400
commit30c244736fe04a6c2f3fee40ed6ecae0071fc655 (patch)
treed2f84fc05a463517dfb3cade5d0ef4c3ceef5a55
parent516dae50b6e16c5f045c0ea164cf994f6e887105 (diff)
added translations filter to filter by available base translations
-rw-r--r--core/Translate/Filter/ByBaseTranslations.php58
-rw-r--r--tests/PHPUnit/Core/Translate/Filter/ByBaseTranslationsTest.php125
2 files changed, 183 insertions, 0 deletions
diff --git a/core/Translate/Filter/ByBaseTranslations.php b/core/Translate/Filter/ByBaseTranslations.php
new file mode 100644
index 0000000000..0ba11b0c4c
--- /dev/null
+++ b/core/Translate/Filter/ByBaseTranslations.php
@@ -0,0 +1,58 @@
+<?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 ByBaseTranslations extends FilterAbstract
+{
+ /**
+ * Filter the given translations
+ *
+ * @param array $translations
+ *
+ * @return array filtered translations
+ *
+ */
+ public function filter($translations)
+ {
+ $cleanedTranslations = array();
+
+ // filter out all translations that don't exist in english translations
+ foreach ($translations AS $pluginName => $pluginTranslations) {
+
+ if (empty($this->_baseTranslations[$pluginName])) {
+ $this->_filteredData[$pluginName] = $pluginTranslations;
+ continue;
+ }
+
+ foreach ($pluginTranslations as $key => $translation) {
+ if (isset($this->_baseTranslations[$pluginName][$key])) {
+ $cleanedTranslations[$pluginName][$key] = $translation;
+ }
+ }
+
+ if (!empty($cleanedTranslations[$pluginName])) {
+ $diff = array_diff($translations[$pluginName], $cleanedTranslations[$pluginName]);
+ } else {
+ $diff = $translations[$pluginName];
+ }
+ if (!empty($diff)) $this->_filteredData[$pluginName] = $diff;
+ }
+
+ return $cleanedTranslations;
+ }
+} \ No newline at end of file
diff --git a/tests/PHPUnit/Core/Translate/Filter/ByBaseTranslationsTest.php b/tests/PHPUnit/Core/Translate/Filter/ByBaseTranslationsTest.php
new file mode 100644
index 0000000000..6596059ba7
--- /dev/null
+++ b/tests/PHPUnit/Core/Translate/Filter/ByBaseTranslationsTest.php
@@ -0,0 +1,125 @@
+<?php
+use Piwik\Translate\Filter\ByBaseTranslations;
+
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+class ByBaseTranslationsTest extends PHPUnit_Framework_TestCase
+{
+ public function getFilterTestData()
+ {
+ return array(
+ // empty stays empty
+ array(
+ array(),
+ array(),
+ array(),
+ array()
+ ),
+ // empty plugin is removed
+ array(
+ array(
+ 'test' => array()
+ ),
+ array(),
+ array(),
+ array(
+ 'test' => array()
+ ),
+ ),
+ // empty values/plugins are removed
+ array(
+ array(
+ 'test' => array(
+ 'key' => 'value',
+ 'test' => 'test'
+ )
+ ),
+ array(
+ 'test' => array(
+ 'key' => 'value',
+ 'x' => 'y'
+ )
+ ),
+ array(
+ 'test' => array(
+ 'key' => 'value',
+ )
+ ),
+ array(
+ 'test' => array(
+ 'test' => 'test',
+ )
+ ),
+ ),
+ // no change if no empty value
+ array(
+ array(
+ 'test' => array(
+ 'test' => 'test'
+ )
+ ),
+ array(
+ 'test' => array(
+ 'test' => 'test'
+ )
+ ),
+ array(
+ 'test' => array(
+ 'test' => 'test'
+ )
+ ),
+ array()
+ ),
+ // empty values are removed, others stay
+ array(
+ array(
+ 'empty' => array(
+ 'test' => 'test'
+ ),
+ 'test' => array(
+ 'test' => 'test',
+ 'empty' => ' ',
+ )
+ ),
+ array(
+ 'empty' => array(
+ 'test' => 'test'
+ ),
+ 'test' => array(
+ 'test' => 'test',
+ )
+ ),
+ array(
+ 'empty' => array(
+ 'test' => 'test'
+ ),
+ 'test' => array(
+ 'test' => 'test'
+ )
+ ),
+ array(
+ 'test' => array(
+ 'empty' => ' ',
+ )
+ )
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider getFilterTestData
+ * @group Core
+ * @group Translate
+ */
+ public function testFilter($translations, $baseTranslations, $expected, $filteredData)
+ {
+ $filter = new ByBaseTranslations($baseTranslations);
+ $result = $filter->filter($translations);
+ $this->assertEquals($expected, $result);
+ $this->assertEquals($filteredData, $filter->getFilteredData());
+ }
+}