Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Work on languages, new version (#9)
Browse files Browse the repository at this point in the history
* Fixed robots.txt

* Refactored config; Added safeguards for languages/versions; Allow all languages

* Work on the languages
  • Loading branch information
niden authored Nov 27, 2018
1 parent 1498d02 commit 8d5cd26
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 26 deletions.
82 changes: 64 additions & 18 deletions app/tasks/GetLanguagesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,50 +40,92 @@ 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) {
if ('branch' === $file['node_type']) {
$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,
];
}
}
16 changes: 8 additions & 8 deletions app/views/inc/header-languages.volt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% else %}
{% set suffix = '' %}
{% endif %}
{% set languages = config.path('app.languages', []) %}
<li class="nav-item dropdown">
<a class="nav-item nav-link dropdown-toggle mr-md-2"
href="{{ config.path('app.url') }}/{{ language }}/{{ version }}"
Expand All @@ -11,18 +12,17 @@
aria-haspopup="true"
aria-expanded="false">
<img class="no-shadow lang-image"
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ language }}.png">
{{ language }}
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ languages[language]['code'] }}.png">
{{ languages[language]['name'] }}
</a>
{% set languages = config.path('app.languages', []) %}
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="bd-versions">
{#{% for item in config.path('app.versions', []) %}#}
{% for item, label in languages %}
<a class="dropdown-item{% if language === item %} active{% endif %}"
href="{{ config.path('app.url') }}/{{ item }}/{{ version }}{{ suffix }}">
{% for key, data in languages %}
<a class="dropdown-item{% if language === key %} active{% endif %}"
href="{{ config.path('app.url') }}/{{ key }}/{{ version }}{{ suffix }}">
<img class="no-shadow lang-image"
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ item }}.png">
{{ label }}
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ data['code'] }}.png">
{{ data['name'] }}
</a>
{% endfor %}
</div>
Expand Down

0 comments on commit 8d5cd26

Please sign in to comment.