Skip to content

Commit 63f4d08

Browse files
committed
Add language switching support
1 parent b63fc02 commit 63f4d08

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Http\Controllers\Base;
25+
26+
use Auth;
27+
use Session;
28+
29+
use Pterodactyl\Models\User;
30+
use Illuminate\Http\Request;
31+
use Pterodactyl\Http\Controllers\Controller;
32+
33+
class LanguageController extends Controller
34+
{
35+
36+
protected $languages = [
37+
'de' => 'Danish',
38+
'en' => 'English',
39+
'es' => 'Spanish',
40+
'fr' => 'French',
41+
'it' => 'Italian',
42+
'pl' => 'Polish',
43+
'pt' => 'Portuguese',
44+
'ru' => 'Russian',
45+
'se' => 'Swedish',
46+
'zh' => 'Chinese',
47+
];
48+
49+
/**
50+
* Controller Constructor
51+
*/
52+
public function __construct()
53+
{
54+
//
55+
}
56+
57+
public function setLanguage(Request $request, $language)
58+
{
59+
if (array_key_exists($language, $this->languages)) {
60+
if (Auth::check()) {
61+
$user = User::findOrFail(Auth::user()->id);
62+
$user->language = $language;
63+
$user->save();
64+
}
65+
Session::set('applocale', $language);
66+
}
67+
return redirect()->back();
68+
}
69+
70+
}

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Kernel extends HttpKernel
1717
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
1818
\Illuminate\Session\Middleware\StartSession::class,
1919
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
20+
\Pterodactyl\Http\Middleware\LanguageMiddleware::class,
2021
];
2122

2223
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Http\Middleware;
25+
26+
use Auth;
27+
use Closure;
28+
use Session;
29+
use Settings;
30+
31+
use Illuminate\Support\Facades\App;
32+
33+
class LanguageMiddleware
34+
{
35+
36+
public function __construct()
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Handle an incoming request.
43+
*
44+
* @param \Illuminate\Http\Request $request
45+
* @param \Closure $next
46+
* @return mixed
47+
*/
48+
public function handle($request, Closure $next)
49+
{
50+
if (Session::has('applocale')) {
51+
App::setLocale(Session::get('applocale'));
52+
} else if(Auth::check() && isset(Auth::user()->language)) {
53+
Session::set('applocale', Auth::user()->language);
54+
App::setLocale(Auth::user()->language);
55+
} else {
56+
App::setLocale(Settings::get('default_language', 'en'));
57+
}
58+
return $next($request);
59+
}
60+
}

app/Http/Routes/LanguageRoutes.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Http\Routes;
25+
26+
use Illuminate\Routing\Router;
27+
28+
class LanguageRoutes {
29+
30+
public function map(Router $router)
31+
{
32+
$router->get('language/{lang}', [
33+
'as' => 'langauge.set',
34+
'uses' => 'Base\LanguageController@setLanguage'
35+
]);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)