Skip to content

Instantly share code, notes, and snippets.

@johanvanhelden
Created January 19, 2021 12:01
Show Gist options
  • Save johanvanhelden/7d9d3f2d33c1f7fafb4be13d01b150c6 to your computer and use it in GitHub Desktop.
Save johanvanhelden/7d9d3f2d33c1f7fafb4be13d01b150c6 to your computer and use it in GitHub Desktop.
lang route
<?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