adding a bunch of wip: i18n stuff

This commit is contained in:
2022-11-28 12:32:48 -07:00
parent 995cc32578
commit 3f340d57fc
10 changed files with 301 additions and 53 deletions

View File

@ -0,0 +1,83 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class TranslationCheckerCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'i18n:check-json {--l|locale= : The source of truth locale}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Uses one JSON file as the source of truth to check other lang files against.';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$masterLocale = 'en';
if (!empty($this->option('locale'))) {
$masterLocale = trim($this->option('locale'));
}
$langDir = base_path('lang');
$langFiles = scandir($langDir);
$bigCount = count($langFiles);
for ($i = 0; $i < $bigCount; $i++) {
if (! preg_match('/.*\.json$/', $langFiles[$i]) || "{$masterLocale}.json" === $langFiles[$i]) {
unset($langFiles[$i]);
}
}
$langFilesCount = count($langFiles);
$this->info("Checking {$langFilesCount} file(s) against {$masterLocale}...");
$masterLocaleTranslations = translations(base_path("lang/{$masterLocale}.json"));
foreach ($langFiles as $localeFile) {
$otherLocaleTranslations = translations(base_path("lang/{$localeFile}"));
$counts = 0;
$mergedTranslations = $this->recursiveMergeArray($masterLocaleTranslations, $otherLocaleTranslations, $counts);
file_put_contents(base_path("lang/{$localeFile}"), json_encode($mergedTranslations, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
$this->info("{$localeFile} had {$counts} missing translations.");
}
return Command::SUCCESS;
}
public function recursiveMergeArray(array $firstArray, array $secondArray, int &$counts)
{
$mergedArray = $secondArray;
foreach ($firstArray as $key => $value) {
if (is_array($value)) {
if (!array_key_exists($key, $secondArray)) {
$mergedArray[$key] = $value;
} else {
$subDiff = $this->recursiveMergeArray($value, $secondArray[$key], $counts);
if (!empty($subDiff)) {
$mergedArray[$key] = $subDiff;
}
}
} else {
if (!array_key_exists($key, $secondArray)) {
$counts++;
$mergedArray[$key] = $value;
}
}
}
return $mergedArray;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Middleware;
use App\Models\Language;
use Illuminate\Http\Request;
use Inertia\Middleware;
@ -41,19 +42,56 @@ class HandleInertiaRequests extends Middleware
*/
public function share(Request $request): array
{
$localeFields = ['locale', 'iso_code', 'name', 'localized_name'];
$currentLocale = $request->session()->get('locale', null);
if (is_null($currentLocale)) {
$currentLocale = Language::where(['locale' => 'en', 'iso_code' => 'en_US'])->get($localeFields)[0]->toArray();
$request->session()->put('locale', [
'locale' => $currentLocale['locale'],
'iso_code' => $currentLocale['iso_code'],
'name' => $currentLocale['name'],
'localized_name' => $currentLocale['localized_name'],
]);
}
$localeFilePath = base_path("lang/{$currentLocale['locale']}.json");
$notifications = [];
$notificationsCount = count($notifications);
$unreadNotifications = false;
if (! is_null($request->user())) {
$notifications = $request->user()->notifications;
$notificationsCount = count($notifications);
for ($i = 0; $i < $notificationsCount; $i++) {
$newData = $notifications[$i]->data;
$createdAt = carbon($notifications[$i]->created_at);
$dateFormat = 'F j';
if (!$createdAt->is(gmdate('Y'))) {
$dateFormat = 'F j, Y';
}
$newData['created_at_date'] = $createdAt->copy()->format($dateFormat);
$newData['created_at_time'] = $createdAt->copy()->format('H:i');
$notifications[$i]->data = $newData;
}
if (count($request->user()->unreadNotifications) > 0) {
$unreadNotifications = true;
}
}
return array_merge(parent::share($request), [
'appName' => config('app.name'),
$additionalData = [
'appName' => config('app.name'),
'availableLocales' => Language::get($localeFields),
'currentLocale' => $currentLocale,
'language' => translations($localeFilePath),
'notifications' => $notifications,
'unreadNotifications' => $unreadNotifications,
]);
];
if ($request->user()->email === env('ADMIN_EMAIL')) {
$additionalData['is_admin_user'] = true;
}
return array_merge(parent::share($request), $additionalData);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SetLocale
{
/**
* Handle an incoming request.
*
* @package App\Http\Middleware\SetLocale
* @since 1.0.0
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (session()->has('locale')) {
app()->setLocale(session('locale')['locale']);
}
return $next($request);
}
}

View File

@ -37,6 +37,7 @@ class User extends Authenticatable
'name',
'surname',
'timezone_name',
'language_id',
'current_team_id',
'profile_photo_path',
'email',
@ -183,4 +184,17 @@ class User extends Authenticatable
{
return $this->morphOne(Address::class, 'addressable');
}
/**
* Language relationship.
*
* @package App\Models\User
* @since 1.0.0
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function language(): BelongsTo
{
return $this->belongsTo(Language::class);
}
}