Skip to content

Commit 07c1a90

Browse files
Merge pull request #120 from Laravel-Lang/5.x
Added a little trick for documentation 😏
2 parents dd5dfda + 2e1f7e8 commit 07c1a90

File tree

11 files changed

+777
-3
lines changed

11 files changed

+777
-3
lines changed

.github/workflows/docs.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ jobs:
2323
fetch-depth: 0
2424
token: ${{ secrets.GITHUB_TOKEN }}
2525

26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php }}
30+
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
31+
coverage: xdebug
32+
2633
- name: Setup Node.js
2734
uses: actions/setup-node@v4
2835
with:
2936
node-version: 20
3037

3138
- name: Install dependencies
32-
run: npm i
39+
run: |
40+
npm i
41+
composer install
42+
43+
- name: Updating the available localizations page
44+
run: php app/bin/available-locales.php
3345

3446
- name: Build VuePress site
3547
run: npm run build

app/Services/Locales.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Services;
6+
7+
use DragonCode\Support\Facades\Filesystem\File;
8+
9+
class Locales
10+
{
11+
protected static string $filePath = __DIR__ . '/../../docs/usage/available-locales.md';
12+
13+
public static function generate(): void
14+
{
15+
static::store(
16+
static::compilePage(static::compileLocales())
17+
);
18+
}
19+
20+
protected static function compileLocales(): array
21+
{
22+
$locales = [];
23+
24+
foreach (static::available() as $code => $data) {
25+
$locales[] = Template::locale($code, $data);
26+
}
27+
28+
return $locales;
29+
}
30+
31+
protected static function available(): array
32+
{
33+
$locales = require __DIR__ . '/../../vendor/laravel-lang/locales/config/private.php';
34+
35+
return $locales['map'] ?? [];
36+
}
37+
38+
protected static function compilePage(array $locales): string
39+
{
40+
return Template::page($locales);
41+
}
42+
43+
protected static function store(string $content): void
44+
{
45+
File::store(static::$filePath, $content);
46+
}
47+
}

app/Services/Template.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Services;
6+
7+
use DragonCode\Support\Facades\Helpers\Str;
8+
9+
class Template
10+
{
11+
protected static ?string $localeStub = null;
12+
13+
protected static ?string $pageStub = null;
14+
15+
public static function locale(string $code, array $locale): string
16+
{
17+
return Str::replaceFormat(static::localeStub(), [
18+
'code' => $code,
19+
'locale' => $locale['name'],
20+
'native' => Str::title($locale['native']),
21+
], '{{%s}}');
22+
}
23+
24+
public static function page(array $locales): string
25+
{
26+
return Str::replaceFormat(static::pageStub(), [
27+
'content' => implode(PHP_EOL . PHP_EOL, $locales),
28+
], '{{%s}}');
29+
}
30+
31+
protected static function localeStub(): string
32+
{
33+
return static::$localeStub ??= static::stub('available-locale');
34+
}
35+
36+
protected static function pageStub(): string
37+
{
38+
return static::$pageStub ??= static::stub('available-locales');
39+
}
40+
41+
protected static function stub(string $filename): string
42+
{
43+
return trim(file_get_contents(__DIR__ . '/../../resources/stubs/' . $filename . '.stub'));
44+
}
45+
}

app/bin/available-locales.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Services\Locales;
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
Locales::generate();

app/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require __DIR__ . '/../vendor/autoload.php';

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"laravel-lang/publisher": "^15.0"
5656
},
5757
"require-dev": {
58+
"dragon-code/support": "^6.11",
5859
"orchestra/testbench": "^8.14",
5960
"phpunit/phpunit": "^10.4.2",
6061
"symfony/var-dumper": "^6.3.6"
@@ -63,6 +64,7 @@
6364
"prefer-stable": true,
6465
"autoload-dev": {
6566
"psr-4": {
67+
"App\\": "app/",
6668
"Tests\\": "tests/"
6769
}
6870
},

docs/.vuepress/config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ module.exports = {
107107
{
108108
text: 'Locales Management',
109109
children: [
110-
'/usage/general-principles.md',
110+
{
111+
text: 'General principles',
112+
link: '/usage/general-principles.md',
113+
collapsible: true,
114+
children: [
115+
'/usage/available-locales.md'
116+
]
117+
},
111118
'/usage/add-locales.md',
112119
'/usage/update-locales.md',
113120
'/usage/reset-locales.md',

docs/usage/aliases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ aliases using a data array.
2525

2626
After this, you can, for example, add new localizations by specifying both the main code and its alias:
2727

28-
```bash
28+
```bash:no-line-numbers
2929
php artisan lang:add de de_CH
3030
```
3131

0 commit comments

Comments
 (0)