Skip to content

Commit 452df12

Browse files
committed
enable localization
Took 26 minutes
1 parent 91a7552 commit 452df12

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Laravel-livv
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/syrian-open-source/laravel-livv.svg?style=flat-square)](https://packagist.org/packages/syrian-open-source/laravel-livv)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/syrian-open-source/laravel-livv/run-tests?label=tests)](https://github.com/syrian-open-source/laravel-livv/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/syrian-open-source/laravel-livv/Check%20&%20fix%20styling?label=code%20style)](https://github.com/syrian-open-source/laravel-livv/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
64
[![Total Downloads](https://img.shields.io/packagist/dt/syrian-open-source/laravel-livv.svg?style=flat-square)](https://packagist.org/packages/syrian-open-source/laravel-livv)
75

86
A package to help you get started with a new Laravel project using `Inertia`, `Vue`, `Vuetify` & `Vite`.
@@ -17,7 +15,7 @@ composer require syrian-open-source/laravel-livv --dev
1715

1816
## Usage
1917

20-
Just run these two commands and you're good to go
18+
Just run these two commands, and you're good to go
2119

2220
```bash
2321
php artisan livv:install
@@ -28,7 +26,7 @@ npm i && npm run dev
2826
This package is designed to be used on a fresh Laravel project.
2927

3028
## Todo
31-
- [ ] Localize all files
29+
- [x] Localize all files
3230
- [ ] Use json files instead of split translations
3331
- [ ] Add an example CRUD
3432
- [ ] Add UserSeeder with a default admin account

app/Http/Middleware/SetLocale.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class SetLocale
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @return mixed
16+
*/
17+
public function handle(Request $request, Closure $next)
18+
{
19+
$locale = config('app.locale');
20+
21+
if(session()->has('locale')) {
22+
$locale = session('locale');
23+
}
24+
25+
app()->setLocale($locale);
26+
27+
return $next($request);
28+
}
29+
}

resources/js/layouts/AdminLayout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
>
2121
<v-icon>{{ selectableLocale }}</v-icon>
2222
</v-btn>
23-
<v-btn
23+
<v-btn
2424
icon
2525
@click="$vuetify.theme.dark = !$vuetify.theme.dark"
2626
>

routes/web.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
});
2626
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');
2727

28-
Route::get('/dashboard', function () {
29-
return inertia('Admin/Dashboard');
30-
})->middleware(['auth', 'verified'])->name('dashboard');
28+
Route::get('language/{lang}', function ($lang) {
29+
session(['locale' => $lang]);
30+
31+
return redirect()->back();
32+
})->name('lang.switch')->where('lang', 'ar|en');
3133

3234
require __DIR__.'/auth.php';

src/Commands/InstallCommand.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public function handle(): int
2727
shell_exec('php artisan breeze:install vue');
2828

2929
$this->updateNodePackages(function ($packages) {
30-
unset($packages['@inertiajs/inertia-vue3']);
31-
unset($packages['@vitejs/plugin-vue']);
32-
unset($packages['@tailwindcss/forms']);
33-
unset($packages['@vue/compiler-sfc']);
34-
unset($packages['tailwindcss']);
35-
unset($packages['vue']);
36-
unset($packages['vue-loader']);
30+
unset(
31+
$packages['@inertiajs/inertia-vue3'],
32+
$packages['@vitejs/plugin-vue'],
33+
$packages['@tailwindcss/forms'],
34+
$packages['@vue/compiler-sfc'],
35+
$packages['tailwindcss'],
36+
$packages['vue'],
37+
$packages['vue-loader']
38+
);
3739

3840
return [
3941
'@inertiajs/inertia-vue' => '^0.8.0',
@@ -73,11 +75,43 @@ public function handle(): int
7375
copy(__DIR__ . '/../../resources/.editorconfig', base_path('/.editorconfig'));
7476
copy(__DIR__ . '/../../resources/middleware/HandleInertiaRequests.php', base_path('app/Http/Middleware/HandleInertiaRequests.php'));
7577
copy(__DIR__ . '/../../routes/web.php', base_path('routes/web.php'));
78+
copy(__DIR__ . '/../../app/Http/Middleware/SetLocale.php', app_path('Http/Middleware/SetLocale.php'));
7679

80+
$this->installMiddlewareAfter('SubstituteBindings::class', '\App\Http\Middleware\SetLocale::class');
7781
$this->info('Your application is ready!');
7882
$this->info('Please run the following command');
7983
$this->info('npm i && npm run dev');
8084

8185
return self::SUCCESS;
8286
}
87+
88+
/**
89+
* Install the middleware to a group in the application Http Kernel.
90+
*
91+
* @param string $after
92+
* @param string $name
93+
* @param string $group
94+
* @return void
95+
*/
96+
protected function installMiddlewareAfter($after, $name, $group = 'web')
97+
{
98+
$httpKernel = file_get_contents(app_path('Http/Kernel.php'));
99+
100+
$middlewareGroups = Str::before(Str::after($httpKernel, '$middlewareGroups = ['), '];');
101+
$middlewareGroup = Str::before(Str::after($middlewareGroups, "'$group' => ["), '],');
102+
103+
if (! Str::contains($middlewareGroup, $name)) {
104+
$modifiedMiddlewareGroup = str_replace(
105+
$after.',',
106+
$after.','.PHP_EOL.' '.$name.',',
107+
$middlewareGroup,
108+
);
109+
110+
file_put_contents(app_path('Http/Kernel.php'), str_replace(
111+
$middlewareGroups,
112+
str_replace($middlewareGroup, $modifiedMiddlewareGroup, $middlewareGroups),
113+
$httpKernel
114+
), LOCK_EX);
115+
}
116+
}
83117
}

0 commit comments

Comments
 (0)