Skip to content

Commit

Permalink
Merge pull request #120 from Laravel-Lang/5.x
Browse files Browse the repository at this point in the history
Added a little trick for documentation 😏
  • Loading branch information
andrey-helldar authored Nov 6, 2023
2 parents dd5dfda + 2e1f7e8 commit 07c1a90
Show file tree
Hide file tree
Showing 11 changed files with 777 additions and 3 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ jobs:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
coverage: xdebug

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm i
run: |
npm i
composer install
- name: Updating the available localizations page
run: php app/bin/available-locales.php

- name: Build VuePress site
run: npm run build
Expand Down
47 changes: 47 additions & 0 deletions app/Services/Locales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Services;

use DragonCode\Support\Facades\Filesystem\File;

class Locales
{
protected static string $filePath = __DIR__ . '/../../docs/usage/available-locales.md';

public static function generate(): void
{
static::store(
static::compilePage(static::compileLocales())
);
}

protected static function compileLocales(): array
{
$locales = [];

foreach (static::available() as $code => $data) {
$locales[] = Template::locale($code, $data);
}

return $locales;
}

protected static function available(): array
{
$locales = require __DIR__ . '/../../vendor/laravel-lang/locales/config/private.php';

return $locales['map'] ?? [];
}

protected static function compilePage(array $locales): string
{
return Template::page($locales);
}

protected static function store(string $content): void
{
File::store(static::$filePath, $content);
}
}
45 changes: 45 additions & 0 deletions app/Services/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Services;

use DragonCode\Support\Facades\Helpers\Str;

class Template
{
protected static ?string $localeStub = null;

protected static ?string $pageStub = null;

public static function locale(string $code, array $locale): string
{
return Str::replaceFormat(static::localeStub(), [
'code' => $code,
'locale' => $locale['name'],
'native' => Str::title($locale['native']),
], '{{%s}}');
}

public static function page(array $locales): string
{
return Str::replaceFormat(static::pageStub(), [
'content' => implode(PHP_EOL . PHP_EOL, $locales),
], '{{%s}}');
}

protected static function localeStub(): string
{
return static::$localeStub ??= static::stub('available-locale');
}

protected static function pageStub(): string
{
return static::$pageStub ??= static::stub('available-locales');
}

protected static function stub(string $filename): string
{
return trim(file_get_contents(__DIR__ . '/../../resources/stubs/' . $filename . '.stub'));
}
}
9 changes: 9 additions & 0 deletions app/bin/available-locales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use App\Services\Locales;

require __DIR__ . '/../bootstrap.php';

Locales::generate();
5 changes: 5 additions & 0 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"laravel-lang/publisher": "^15.0"
},
"require-dev": {
"dragon-code/support": "^6.11",
"orchestra/testbench": "^8.14",
"phpunit/phpunit": "^10.4.2",
"symfony/var-dumper": "^6.3.6"
Expand All @@ -63,6 +64,7 @@
"prefer-stable": true,
"autoload-dev": {
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
}
},
Expand Down
9 changes: 8 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ module.exports = {
{
text: 'Locales Management',
children: [
'/usage/general-principles.md',
{
text: 'General principles',
link: '/usage/general-principles.md',
collapsible: true,
children: [
'/usage/available-locales.md'
]
},
'/usage/add-locales.md',
'/usage/update-locales.md',
'/usage/reset-locales.md',
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ aliases using a data array.

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

```bash
```bash:no-line-numbers
php artisan lang:add de de_CH
```

Expand Down
Loading

0 comments on commit 07c1a90

Please sign in to comment.