Created
January 19, 2021 12:01
-
-
Save johanvanhelden/7d9d3f2d33c1f7fafb4be13d01b150c6 to your computer and use it in GitHub Desktop.
lang route
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use Illuminate\Support\Facades\App; | |
use Illuminate\Support\Facades\Cache; | |
use Illuminate\Support\Facades\Route; | |
// Localization | |
Route::get('/js/lang-messages.js', function (): void { | |
$lang = config('app.locale'); | |
$cacheKey = $lang . '.lang-messages.js'; | |
if (App::environment('local', 'testing')) { | |
Cache::forget($cacheKey); | |
} | |
$strings = Cache::rememberForever($cacheKey, function () use ($lang) { | |
$files = glob(resource_path('lang/' . $lang . '/*.php')); | |
$strings = []; | |
foreach ($files as $file) { | |
$name = basename($file, '.php'); | |
$strings[$lang . '.' . $name] = require $file; | |
} | |
return $strings; | |
}); | |
header('Content-Type: text/javascript'); | |
echo 'window.i18n = ' . json_encode($strings) . ';'; | |
exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment