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

Commit 8d5cd26

Browse files
authored
Work on languages, new version (#9)
* Fixed robots.txt * Refactored config; Added safeguards for languages/versions; Allow all languages * Work on the languages
1 parent 1498d02 commit 8d5cd26

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed

app/tasks/GetLanguagesTask.php

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717

1818
namespace Docs\Cli\Tasks;
1919

20+
use function array_multisort;
2021
use Phalcon\CLI\Task;
2122
use function asort;
2223
use function Docs\Functions\app_path;
2324
use function Docs\Functions\env;
2425
use function file_put_contents;
2526
use function json_decode;
27+
use const SORT_ASC;
28+
use const SORT_STRING;
2629
use function sprintf;
2730
use const PHP_EOL;
31+
use function sprintf;
2832

2933
/**
3034
* GetLanguagesTask
@@ -36,50 +40,92 @@ class GetLanguagesTask extends Task
3640
*/
3741
public function mainAction()
3842
{
43+
echo 'Getting Supported Languages from Crowdin...' . PHP_EOL;
44+
$url = 'https://api.crowdin.com/api/supported-languages?json';
45+
$data = $this->makeCurl($url);
46+
47+
$languageMap = [];
48+
$supported = json_decode($data['results'], true);
49+
foreach ($supported as $language) {
50+
$languageMap[$language['crowdin_code']] = $language['locale'];
51+
}
52+
3953
echo 'Getting Languages from Crowdin...' . PHP_EOL;
4054
$template = 'https://api.crowdin.com/api/project/zephir-documentation/info?key=%s&json';
4155
$url = sprintf($template, env('CROWDIN_API_KEY'), '');
56+
$data = $this->makeCurl($url);
57+
$fileName = app_path('/storage/crowdin/crowdin.json');
58+
$results = $data['results'];
4259

43-
$handle = curl_init();
44-
curl_setopt($handle, CURLOPT_URL, $url);
45-
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
46-
curl_setopt($handle, CURLOPT_POST, true);
47-
curl_setopt($handle, CURLOPT_POSTFIELDS, ['json' => 1]);
48-
49-
$fileName = app_path('/storage/crowdin/crowdin.json');
50-
$results = curl_exec($handle);
51-
$errorNumber = curl_errno($handle);
52-
$errorDescription = curl_error($handle);
53-
curl_close($handle);
54-
55-
if ($errorNumber > 0) {
60+
if ($data['errorNumber'] > 0) {
5661
$results = '[]';
57-
echo $errorDescription . PHP_EOL;
62+
echo $data['errorDescription'] . PHP_EOL;
5863
}
5964

6065
$crowdin = json_decode($results, true);
61-
$languages = [];
66+
$languages = [
67+
'en' => [
68+
'name' => 'English',
69+
'code' => 'en',
70+
]
71+
];
6272
foreach ($crowdin['languages'] as $language) {
63-
$languages[$language['code']] = $language['name'];
73+
$code = $languageMap[$language['code']] ?? 'en';
74+
75+
$languages[$code] = [
76+
'name' => $language['name'],
77+
'code' => $language['code'],
78+
];
6479
}
80+
array_multisort(array_column($languages, 'name'), SORT_ASC, $languages);
81+
82+
/**
83+
* Now create the final array
84+
*/
6585

6686
$versions = [];
6787
foreach ($crowdin['files'] as $file) {
6888
if ('branch' === $file['node_type']) {
6989
$versions[] = $file['name'];
7090
}
7191
}
72-
73-
asort($languages);
7492
arsort($versions);
7593

7694
$data = [
7795
'languages' => $languages,
7896
'versions' => $versions,
97+
'map' => $languageMap,
7998
];
8099

81100
file_put_contents($fileName, json_encode($data));
82101

83102
echo 'Updated languages.' . PHP_EOL;
84103
}
104+
105+
/**
106+
* Sends a CURL request
107+
*
108+
* @param string $url
109+
*
110+
* @return array
111+
*/
112+
private function makeCurl(string $url): array
113+
{
114+
$handle = curl_init();
115+
curl_setopt($handle, CURLOPT_URL, $url);
116+
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
117+
curl_setopt($handle, CURLOPT_POST, true);
118+
curl_setopt($handle, CURLOPT_POSTFIELDS, ['json' => 1]);
119+
120+
$results = curl_exec($handle);
121+
$errorNumber = curl_errno($handle);
122+
$errorDescription = curl_error($handle);
123+
curl_close($handle);
124+
125+
return [
126+
'results' => $results,
127+
'errorNumber' => $errorNumber,
128+
'errorDescription' => $errorDescription,
129+
];
130+
}
85131
}

app/views/inc/header-languages.volt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% else %}
44
{% set suffix = '' %}
55
{% endif %}
6+
{% set languages = config.path('app.languages', []) %}
67
<li class="nav-item dropdown">
78
<a class="nav-item nav-link dropdown-toggle mr-md-2"
89
href="{{ config.path('app.url') }}/{{ language }}/{{ version }}"
@@ -11,18 +12,17 @@
1112
aria-haspopup="true"
1213
aria-expanded="false">
1314
<img class="no-shadow lang-image"
14-
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ language }}.png">
15-
{{ language }}
15+
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ languages[language]['code'] }}.png">
16+
{{ languages[language]['name'] }}
1617
</a>
17-
{% set languages = config.path('app.languages', []) %}
1818
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="bd-versions">
1919
{#{% for item in config.path('app.versions', []) %}#}
20-
{% for item, label in languages %}
21-
<a class="dropdown-item{% if language === item %} active{% endif %}"
22-
href="{{ config.path('app.url') }}/{{ item }}/{{ version }}{{ suffix }}">
20+
{% for key, data in languages %}
21+
<a class="dropdown-item{% if language === key %} active{% endif %}"
22+
href="{{ config.path('app.url') }}/{{ key }}/{{ version }}{{ suffix }}">
2323
<img class="no-shadow lang-image"
24-
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ item }}.png">
25-
{{ label }}
24+
src="https://d2srrzh48sp2nh.cloudfront.net/31cd209e/images/flags/{{ data['code'] }}.png">
25+
{{ data['name'] }}
2626
</a>
2727
{% endfor %}
2828
</div>

0 commit comments

Comments
 (0)