From 8d5cd2681d1f0792808e684b199d271cb5203878 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Tue, 27 Nov 2018 17:47:38 -0500 Subject: [PATCH] Work on languages, new version (#9) * Fixed robots.txt * Refactored config; Added safeguards for languages/versions; Allow all languages * Work on the languages --- app/tasks/GetLanguagesTask.php | 82 ++++++++++++++++++++++------- app/views/inc/header-languages.volt | 16 +++--- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/app/tasks/GetLanguagesTask.php b/app/tasks/GetLanguagesTask.php index b807289..35b8199 100644 --- a/app/tasks/GetLanguagesTask.php +++ b/app/tasks/GetLanguagesTask.php @@ -17,14 +17,18 @@ namespace Docs\Cli\Tasks; +use function array_multisort; use Phalcon\CLI\Task; use function asort; use function Docs\Functions\app_path; use function Docs\Functions\env; use function file_put_contents; use function json_decode; +use const SORT_ASC; +use const SORT_STRING; use function sprintf; use const PHP_EOL; +use function sprintf; /** * GetLanguagesTask @@ -36,32 +40,48 @@ class GetLanguagesTask extends Task */ public function mainAction() { + echo 'Getting Supported Languages from Crowdin...' . PHP_EOL; + $url = 'https://api.crowdin.com/api/supported-languages?json'; + $data = $this->makeCurl($url); + + $languageMap = []; + $supported = json_decode($data['results'], true); + foreach ($supported as $language) { + $languageMap[$language['crowdin_code']] = $language['locale']; + } + echo 'Getting Languages from Crowdin...' . PHP_EOL; $template = 'https://api.crowdin.com/api/project/zephir-documentation/info?key=%s&json'; $url = sprintf($template, env('CROWDIN_API_KEY'), ''); + $data = $this->makeCurl($url); + $fileName = app_path('/storage/crowdin/crowdin.json'); + $results = $data['results']; - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $url); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($handle, CURLOPT_POST, true); - curl_setopt($handle, CURLOPT_POSTFIELDS, ['json' => 1]); - - $fileName = app_path('/storage/crowdin/crowdin.json'); - $results = curl_exec($handle); - $errorNumber = curl_errno($handle); - $errorDescription = curl_error($handle); - curl_close($handle); - - if ($errorNumber > 0) { + if ($data['errorNumber'] > 0) { $results = '[]'; - echo $errorDescription . PHP_EOL; + echo $data['errorDescription'] . PHP_EOL; } $crowdin = json_decode($results, true); - $languages = []; + $languages = [ + 'en' => [ + 'name' => 'English', + 'code' => 'en', + ] + ]; foreach ($crowdin['languages'] as $language) { - $languages[$language['code']] = $language['name']; + $code = $languageMap[$language['code']] ?? 'en'; + + $languages[$code] = [ + 'name' => $language['name'], + 'code' => $language['code'], + ]; } + array_multisort(array_column($languages, 'name'), SORT_ASC, $languages); + + /** + * Now create the final array + */ $versions = []; foreach ($crowdin['files'] as $file) { @@ -69,17 +89,43 @@ public function mainAction() $versions[] = $file['name']; } } - - asort($languages); arsort($versions); $data = [ 'languages' => $languages, 'versions' => $versions, + 'map' => $languageMap, ]; file_put_contents($fileName, json_encode($data)); echo 'Updated languages.' . PHP_EOL; } + + /** + * Sends a CURL request + * + * @param string $url + * + * @return array + */ + private function makeCurl(string $url): array + { + $handle = curl_init(); + curl_setopt($handle, CURLOPT_URL, $url); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($handle, CURLOPT_POST, true); + curl_setopt($handle, CURLOPT_POSTFIELDS, ['json' => 1]); + + $results = curl_exec($handle); + $errorNumber = curl_errno($handle); + $errorDescription = curl_error($handle); + curl_close($handle); + + return [ + 'results' => $results, + 'errorNumber' => $errorNumber, + 'errorDescription' => $errorDescription, + ]; + } } diff --git a/app/views/inc/header-languages.volt b/app/views/inc/header-languages.volt index 7c23733..159dee5 100644 --- a/app/views/inc/header-languages.volt +++ b/app/views/inc/header-languages.volt @@ -3,6 +3,7 @@ {% else %} {% set suffix = '' %} {% endif %} + {% set languages = config.path('app.languages', []) %}