From 842060e1dd5575219cfbd9fcde36b668dd33974f Mon Sep 17 00:00:00 2001 From: Pascal Craponne Date: Tue, 15 Aug 2017 21:49:33 +0200 Subject: [PATCH] Locales support for packages display name and description (#47) LGTM ! * Locales in pkg name & description JSON result * Fixed tests * Scrutinizer didn't like string interpolation * JsonOutput $langue default to 'enu' --- .gitignore | 1 + lib/SSpkS/Handler/SynologyHandler.php | 8 ++++++-- lib/SSpkS/Output/JsonOutput.php | 12 +++++++----- tests/JsonOutputTest.php | 4 ++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 7518e91..a6b76a7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /cache/*.png /packages/*.spk /vendor/* +/.idea/ diff --git a/lib/SSpkS/Handler/SynologyHandler.php b/lib/SSpkS/Handler/SynologyHandler.php index 03a5547..7dba985 100644 --- a/lib/SSpkS/Handler/SynologyHandler.php +++ b/lib/SSpkS/Handler/SynologyHandler.php @@ -44,7 +44,11 @@ public function handle() $minor = trim($_REQUEST['minor']); $build = trim($_REQUEST['build']); $channel = trim($_REQUEST['package_update_channel']); - // more parameters: language, timezone and unique + if (isset($_REQUEST['language'])) + $language = trim($_REQUEST['language']); + else + $language = ''; + // more parameters: timezone and unique if ($arch == '88f6282') { $arch = '88f6281'; @@ -66,6 +70,6 @@ public function handle() $jo = new JsonOutput($this->config); $jo->setExcludedServices($this->config->excludedSynoServices); - $jo->outputPackages($filteredPkgList); + $jo->outputPackages($filteredPkgList, $language); } } diff --git a/lib/SSpkS/Output/JsonOutput.php b/lib/SSpkS/Output/JsonOutput.php index 8d936b4..01c55fb 100644 --- a/lib/SSpkS/Output/JsonOutput.php +++ b/lib/SSpkS/Output/JsonOutput.php @@ -46,9 +46,10 @@ private function ifEmpty($obj, $property, $alternative = null) * Returns JSON-ready array of Package $pkg. * * @param \SSpkS\Package\Package $pkg Package + * @param string $language The output language (this has impact on display name and description) * @return array JSON-ready array of $pkg. */ - private function packageToJson($pkg) + private function packageToJson($pkg, $language) { /* package @@ -98,8 +99,8 @@ private function packageToJson($pkg) $packageJSON = array( 'package' => $pkg->package, 'version' => $pkg->version, - 'dname' => $pkg->displayname, - 'desc' => $pkg->description, + 'dname' => $this->ifEmpty($pkg, 'displayname_' . $language, $pkg->displayname), + 'desc' => $this->ifEmpty($pkg, 'description_' . $language, $pkg->description), 'price' => 0, 'download_count' => 0, // Will only display values over 1000, do not display it by default 'recent_download_count' => 0, @@ -137,14 +138,15 @@ private function packageToJson($pkg) * Outputs given packages as JSON. * * @param \SSpkS\Package\Package[] $pkgList List of packages to output. + * @param string $language The output language (this has impact on display name and description) */ - public function outputPackages($pkgList) + public function outputPackages($pkgList, $language = 'enu') { $jsonOutput = array( 'packages' => array(), ); foreach ($pkgList as $pkg) { - $pkgJson = $this->packageToJson($pkg); + $pkgJson = $this->packageToJson($pkg, $language); $jsonOutput['packages'][] = $pkgJson; } diff --git a/tests/JsonOutputTest.php b/tests/JsonOutputTest.php index d36f96a..43ef5cd 100644 --- a/tests/JsonOutputTest.php +++ b/tests/JsonOutputTest.php @@ -46,7 +46,7 @@ public function testExcludedServices() $jo = new JsonOutput($this->config); $jo->setExcludedServices(array('test3')); - $jo->outputPackages($pl); + $jo->outputPackages($pl, null); $this->expectOutputRegex('/"deppkgs":"test1 test2 test4"/'); } @@ -61,7 +61,7 @@ public function testJsonConversion() $jo = new JsonOutput($this->config); - $jo->outputPackages($pl); + $jo->outputPackages($pl, null); $pkgMd5 = md5_file($this->tempPkg); $pkgSize = filesize($this->tempPkg);