Skip to content

Commit

Permalink
Removes deprecated class Piwik\Translate (matomo-org#15586)
Browse files Browse the repository at this point in the history
* Removes deprecated class Piwik\Translate

* use submodule branches

* adjust test

* fix typo

* readd deprecated Piwik\Translate

* submodule updates
  • Loading branch information
sgiehl authored Feb 20, 2020
1 parent 361bc55 commit fa23dc7
Show file tree
Hide file tree
Showing 58 changed files with 151 additions and 138 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The Product Changelog at **[matomo.org/changelog](https://matomo.org/changelog)*
* The deprecated Platform API class `\Piwik\Registry` has been removed. Use `\Piwik\Container\StaticContainer` instead
* The deprecated Platform API class `\Piwik\TaskScheduler` has been removed. Use `\Piwik\Scheduler\Scheduler` instead
* The deprecated Platform API class `\Piwik\DeviceDetectorFactory` has been removed. Use `\Piwik\DeviceDetector\DeviceDetectorFactory` instead
* The deprecated Platform API class `\Piwik\Translate` has been removed. Use `\Piwik\Translation\Translator` instead.
* The JavaScript tracker now uses `sendBeacon` by default if supported by the browser. You can disable this by calling the tracker method `disableAlwaysUseSendBeacon`. As a result, callback parameters won't work anymore and a tracking request might not appear in the developer tools.
* The console option `--piwik-domain` has been removed. Use `--matomo-domain` instead
* The core plugin `CustomPiwikJs` has been renamed to `CustomJsTracker`
Expand Down
2 changes: 1 addition & 1 deletion core/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function getCssInclusionDirective()
*/
public function getJsInclusionDirective()
{
$result = "<script type=\"text/javascript\">\n" . Translate::getJavascriptTranslations() . "\n</script>";
$result = "<script type=\"text/javascript\">\n" . StaticContainer::get('Piwik\Translation\Translator')->getJavascriptTranslations() . "\n</script>";

if ($this->isMergedAssetsDisabled()) {
$this->getMergedCoreJSAsset()->delete();
Expand Down
3 changes: 2 additions & 1 deletion core/CacheId.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
*/
namespace Piwik;

use Piwik\Container\StaticContainer;
use Piwik\Plugin\Manager;

class CacheId
{
public static function languageAware($cacheId)
{
return $cacheId . '-' . Translate::getLanguageLoaded();
return $cacheId . '-' . StaticContainer::get('Piwik\Translation\Translator')->getCurrentLanguage();
}

public static function pluginAware($cacheId)
Expand Down
5 changes: 0 additions & 5 deletions core/Piwik.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\Translation\Translator;

/**
* @see core/Translate.php
*/
require_once PIWIK_INCLUDE_PATH . '/core/Translate.php';

/**
* Main piwik helper class.
*
Expand Down
20 changes: 19 additions & 1 deletion core/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Translate
*/
public static function clean($s)
{
return html_entity_decode(trim($s), ENT_QUOTES, 'UTF-8');
self::triggerDeprecationNotice();
return self::getTranslator()->clean($s);
}

/**
Expand All @@ -51,6 +52,7 @@ public static function unloadEnglishTranslation()
*/
public static function reloadLanguage($language = false)
{
self::triggerDeprecationNotice();
}

/**
Expand All @@ -61,6 +63,7 @@ public static function reloadLanguage($language = false)
*/
public static function loadCoreTranslation($language = false)
{
self::triggerDeprecationNotice();
self::getTranslator()->addDirectory(PIWIK_INCLUDE_PATH . '/lang');
}

Expand All @@ -69,6 +72,7 @@ public static function loadCoreTranslation($language = false)
*/
public static function mergeTranslationArray($translation)
{
self::triggerDeprecationNotice();
}

/**
Expand All @@ -77,12 +81,14 @@ public static function mergeTranslationArray($translation)
*/
public static function getLanguageToLoad()
{
self::triggerDeprecationNotice();
return self::getTranslator()->getCurrentLanguage();
}

/** Reset the cached language to load. Used in tests. */
public static function reset()
{
self::triggerDeprecationNotice();
self::getTranslator()->reset();
}

Expand All @@ -92,11 +98,13 @@ public static function reset()
*/
public static function getLanguageLoaded()
{
self::triggerDeprecationNotice();
return self::getTranslator()->getCurrentLanguage();
}

public static function getLanguageDefault()
{
self::triggerDeprecationNotice();
return self::getTranslator()->getDefaultLanguage();
}

Expand All @@ -105,11 +113,13 @@ public static function getLanguageDefault()
*/
public static function getJavascriptTranslations()
{
self::triggerDeprecationNotice();
return self::getTranslator()->getJavascriptTranslations();
}

public static function findTranslationKeyForTranslation($translation)
{
self::triggerDeprecationNotice();
return self::getTranslator()->findTranslationKeyForTranslation($translation);
}

Expand All @@ -123,7 +133,15 @@ private static function getTranslator()

public static function loadAllTranslations()
{
self::triggerDeprecationNotice();
self::loadCoreTranslation();
Manager::getInstance()->loadPluginTranslations();
}

protected static function triggerDeprecationNotice()
{
if (Development::isEnabled()) {
Log::warning('Using \Piwik\Translate is deprecated. Use \Piwik\Translation\Translator instead.');
}
}
}
11 changes: 11 additions & 0 deletions core/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public function __construct(LoaderInterface $loader, array $directories = null)
$this->directories = $directories;
}

/**
* Clean a string that may contain HTML special chars, single/double quotes, HTML entities, leading/trailing whitespace
*
* @param string $s
* @return string
*/
public static function clean($s)
{
return html_entity_decode(trim($s), ENT_QUOTES, 'UTF-8');
}

/**
* Returns an internationalized string using a translation ID. If a translation
* cannot be found for the ID, the ID is returned.
Expand Down
2 changes: 1 addition & 1 deletion core/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected function addFunction_getJavascriptTranslations()
{
$getJavascriptTranslations = new Twig_SimpleFunction(
'getJavascriptTranslations',
array('Piwik\\Translate', 'getJavascriptTranslations')
array(StaticContainer::get('Piwik\Translation\Translator'), 'getJavascriptTranslations')
);
$this->twig->addFunction($getJavascriptTranslations);
}
Expand Down
1 change: 0 additions & 1 deletion piwik.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
require_once PIWIK_INCLUDE_PATH . '/core/SettingsServer.php';
require_once PIWIK_INCLUDE_PATH . '/core/Tracker.php';
require_once PIWIK_INCLUDE_PATH . '/core/Config.php';
require_once PIWIK_INCLUDE_PATH . '/core/Translate.php';
require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Cache.php';
require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Request.php';
require_once PIWIK_INCLUDE_PATH . '/core/Cookie.php';
Expand Down
6 changes: 3 additions & 3 deletions plugins/Actions/tests/Unit/ArchiverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Piwik\Plugins\Actions\tests\Unit;

use Piwik\Plugins\Actions\ArchivingHelper;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tracker\Action;
use Piwik\Translate;

require_once PIWIK_INCLUDE_PATH . '/plugins/Actions/Actions.php';

Expand All @@ -23,12 +23,12 @@ class ArchiverTests extends \PHPUnit\Framework\TestCase
{
public function setUp()
{
Translate::loadAllTranslations();
Fixture::loadAllTranslations();
}

public function tearDown()
{
Translate::reset();
Fixture::resetTranslations();
}

public function getActionNameTestData()
Expand Down
6 changes: 3 additions & 3 deletions plugins/Contents/tests/System/ContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/
namespace Piwik\Plugins\Contents\tests\System;

use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
use Piwik\Plugins\Contents\tests\Fixtures\TwoVisitsWithContents;
use Piwik\Translate;

/**
* Testing Contents
Expand Down Expand Up @@ -48,13 +48,13 @@ protected function getApiToCall()
protected function setup()
{
parent::setup();
Translate::loadAllTranslations();
Fixture::loadAllTranslations();
}

protected function tearDown()
{
parent::tearDown();
Translate::reset();
Fixture::resetTranslations();
}

public function getApiForTesting()
Expand Down
5 changes: 2 additions & 3 deletions plugins/CoreConsole/Commands/GenerateReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
namespace Piwik\Plugins\CoreConsole\Commands;

use Piwik\Columns\Dimension;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugin\Report;
use Piwik\Plugin\ReportsProvider;
use Piwik\Translate;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -210,7 +209,7 @@ protected function getCategory(InputInterface $input, OutputInterface $output, $
$validate($category);
}

$translationKey = Translate::findTranslationKeyForTranslation($category);
$translationKey = StaticContainer::get('Piwik\Translation\Translator')->findTranslationKeyForTranslation($category);
if (!empty($translationKey)) {
return $translationKey;
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/CoreConsole/Commands/GenerateWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Piwik\Plugins\CoreConsole\Commands;

use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Translate;
use Piwik\Widget\WidgetsList;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -125,7 +125,7 @@ protected function getExistingCategories()
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @return string
* @throws \RuntimeException
*/
protected function getCategory(InputInterface $input, OutputInterface $output)
Expand All @@ -148,7 +148,7 @@ protected function getCategory(InputInterface $input, OutputInterface $output)
$validate($category);
}

$translationKey = Translate::findTranslationKeyForTranslation($category);
$translationKey = StaticContainer::get('Piwik\Translation\Translator')->findTranslationKeyForTranslation($category);
if (!empty($translationKey)) {
return $translationKey;
}
Expand All @@ -161,7 +161,7 @@ protected function getCategory(InputInterface $input, OutputInterface $output)
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @return string
* @throws \RuntimeException
*/
protected function getPluginName(InputInterface $input, OutputInterface $output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Translate;

/**
* @group CoreVisualizations
Expand All @@ -37,12 +36,12 @@ public function setUp()

$this->config = new Config();

Translate::loadAllTranslations();
Fixture::loadAllTranslations();
}

public function tearDown()
{
Translate::reset();
Fixture::resetTranslations();

parent::tearDown();
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/CustomAlerts
2 changes: 1 addition & 1 deletion plugins/DeviceDetectorCache
8 changes: 5 additions & 3 deletions plugins/Feedback/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
namespace Piwik\Plugins\Feedback;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\IP;
use Piwik\Mail;
use Piwik\Piwik;
use Piwik\Translate;
use Piwik\Url;
use Piwik\Version;

Expand Down Expand Up @@ -80,11 +80,13 @@ private function sendMail($subject, $body)

private function getEnglishTranslationForFeatureName($featureName)
{
if (Translate::getLanguageLoaded() == 'en') {
$translator = StaticContainer::get('Piwik\Translation\Translator');

if ($translator->getCurrentLanguage() == 'en') {
return $featureName;
}

$translationKeyForFeature = Translate::findTranslationKeyForTranslation($featureName);
$translationKeyForFeature = $translator->findTranslationKeyForTranslation($featureName);

return Piwik::translate($translationKeyForFeature, array(), 'en');
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/GeoIp2/tests/System/ConvertRegionCodesToIsoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Translate;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

Expand Down Expand Up @@ -126,7 +125,7 @@ public function testExecute_ShouldConvertRegionCodes()
);

// we need to manually reload the translations since they get reset for some reason in IntegrationTestCase::tearDown();
Translate::loadAllTranslations();
Fixture::loadAllTranslations();

$this->assertApiResponseEqualsExpected("UserCountry.getRegion", $queryParams);
$this->assertApiResponseEqualsExpected("UserCountry.getCountry", $queryParams);
Expand Down
4 changes: 2 additions & 2 deletions plugins/ImageGraph/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use Piwik\API\Request;
use Piwik\Archive\DataTableFactory;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\DataTable\Map;
use Piwik\Filesystem;
use Piwik\Period;
use Piwik\Piwik;
use Piwik\SettingsServer;
use Piwik\Translate;

/**
* The ImageGraph.get API call lets you generate beautiful static PNG Graphs for any existing Matomo report.
Expand Down Expand Up @@ -139,7 +139,7 @@ public function get(
$useUnicodeFont = array(
'am', 'ar', 'el', 'fa', 'fi', 'he', 'ja', 'ka', 'ko', 'te', 'th', 'zh-cn', 'zh-tw',
);
$languageLoaded = Translate::getLanguageLoaded();
$languageLoaded = StaticContainer::get('Piwik\Translation\Translator')->getCurrentLanguage();
$font = self::getFontPath(self::DEFAULT_FONT);
if (in_array($languageLoaded, $useUnicodeFont)) {
$unicodeFontPath = self::getFontPath(self::UNICODE_FONT);
Expand Down
Loading

0 comments on commit fa23dc7

Please sign in to comment.