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.

Revisions

  1. johanvanhelden created this gist Jan 19, 2021.
    34 changes: 34 additions & 0 deletions lang.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <?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();
    });