0%

Before I wrote an post for the same topic, but it was written for Laravel 8. Now, I have updated it for Laravel 11.

Route set up

Open your route file routes\web.php and add the following code:

1
2
3
4
Route::get('/lang/{locale}', function ($locale) {
session()->put('locale', $locale);
return redirect()->back();
});

Add middleware

1
php artisan make:middleware Localization

Open the file app/Http/Middleware/Localization.php and edit as the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\App;

class Localization
{
/**
* Handle an incoming request.
*
* @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)
{
//check if session has locale
if (session()->has('locale')) {

//set locale
App::setLocale(session('locale'));
}
return $next($request);
}
}

Then add the Localization middleware to the middlewareGroups section of bootstrap/app.php file as the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\Localization;

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->appendToGroup('web', Localization::class);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
Read more »


机动车车牌管理作为城市交通管理的重要一环,直接影响着城市的交通状况、环境质量以及居民的生活品质。中国和新加坡作为两个发展程度不同的国家,在机动车车牌管理上采用了截然不同的模式。本文将以新加坡和中国上海市为例,深入探讨中新两国在车牌管理方面的异同,并分析两种模式背后的逻辑和优缺点。

Read more »

此文针对使用澎湃OS国行版本的小米手机,你无需刷机

此方法在我的小米12s,HyperOS 1.0.3.0 ULTCNXM 版本测试通过

步骤 1 - 启用谷歌基础服务

打开小米设置,在搜索框中输入google,在下面列出的选项中选择“谷歌基础服务”,然后点击选择按钮打开服务。

Read more »

When you try to directly parse a server side responed json to a JSON object ,it will popup an err.

1
2
3
4
5
const message = {"code":"200","msg":"Sample movement recorded succsfully"} 

var msg = JSON.parse(message);
// Log to console
console.log(msg.msg)
1
2
3
SyntaxError: "[object Object]" is not valid JSON
at <anonymous>:6:16
at mn (<anonymous>:16:5455)

you need to use JSON.stringify() method first, then parse the data to JSON object

1
2
3
4
5
6
const message = {"code":"200","msg":"Sample movement recorded succsfully"} 

var obj = JSON.stringify(message);
var msg = JSON.parse(obj);
// Log to console
console.log(msg.msg)

  1. Before you clone your repository or just copy your source code folder to a new windows computer, you need to install git and Node.js first.
  2. Clone your hexo repository to your local folder
    1
    git clone [email protected]:yourname/your_website.git
  3. Run the following command in your source folder
    1
    2
    3
    npm install hexo-cli -g
    npm install
    hexo clean

Now you can use your Hexo to continue your blogging.