add locale manager
This commit is contained in:
101
app/Repos/System/Locale/LocaleManagerRepo.php
Normal file
101
app/Repos/System/Locale/LocaleManagerRepo.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repos\System\Locale;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class LocaleManagerRepo
|
||||
{
|
||||
/**
|
||||
* Locale app path
|
||||
*/
|
||||
protected string $localeAppPath;
|
||||
|
||||
/**
|
||||
* Locale app url
|
||||
*/
|
||||
protected string $localeAppUrl;
|
||||
|
||||
/**
|
||||
* Locale app api token
|
||||
*/
|
||||
protected string $localeAppApiToken;
|
||||
|
||||
/**
|
||||
* Files system
|
||||
*/
|
||||
protected Filesystem $fileSystem;
|
||||
|
||||
/**
|
||||
* Locale manager app
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->localeAppPath = config('app.locale_app.path');
|
||||
$this->localeAppUrl = config('app.locale_app.url');
|
||||
$this->localeAppApiToken = config('app.locale_app.api_token');
|
||||
|
||||
$this->fileSystem = new Filesystem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic way of new
|
||||
*/
|
||||
public static function make(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run locale manager
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$this->exportTranslations()
|
||||
->syncTranslationsWithLocaleApp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Locale app translations directory
|
||||
*/
|
||||
public function localeAppTranslationsDirectory(): string
|
||||
{
|
||||
return $this->localeAppPath . DIRECTORY_SEPARATOR . 'lang';
|
||||
}
|
||||
|
||||
/**
|
||||
* Export translations to translate app
|
||||
*/
|
||||
public function exportTranslations(): self
|
||||
{
|
||||
$this->fileSystem->mirror(lang_path(), $this->localeAppTranslationsDirectory());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync translations with locale app
|
||||
*/
|
||||
public function syncTranslationsWithLocaleApp(): self
|
||||
{
|
||||
$response = Http::acceptJson()->withHeaders([
|
||||
'Api-Token' => $this->localeAppApiToken,
|
||||
])
|
||||
->retry(
|
||||
times: 3,
|
||||
sleepMilliseconds: 50,
|
||||
throw: false,
|
||||
when: function (Exception $exception, PendingRequest $request) {
|
||||
return true;
|
||||
})
|
||||
->post(
|
||||
url: $this->localeAppUrl . '/api/import-translations',
|
||||
data: []
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user