Skip to content

Commit

Permalink
(Feature) User Language System
Browse files Browse the repository at this point in the history
- Need more lang files translated
- Closes #25
  • Loading branch information
HDVinnie committed Dec 28, 2017
1 parent 961ffa8 commit e1934a2
Show file tree
Hide file tree
Showing 41 changed files with 682 additions and 428 deletions.
23 changes: 23 additions & 0 deletions app/Helpers/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://choosealicense.com/licenses/gpl-3.0/ GNU General Public License v3.0
* @author HDVinnie
*/

if (!function_exists('language')) {
/**
* Get the language instance.
*
* @return App\Language
*/
function language()
{
return app('language');
}
}
72 changes: 72 additions & 0 deletions app/Http/Controllers/LanguageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://choosealicense.com/licenses/gpl-3.0/ GNU General Public License v3.0
* @author HDVinnie
*/

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;

use App\Language;

class LanguageController extends Controller
{
/**
* Set locale if it's allowed.
*
* @param string $locale
* @param \Illuminate\Http\Request $request
**/
private function setLocale($locale, $request)
{
// Check if is allowed and set default locale if not
if (!Language::allowed($locale)) {
$locale = config('app.locale');
}

if (Auth::check()) {
Auth::user()->setAttribute('locale', $locale)->save();
} else {
$request->session()->put('locale', $locale);
}
}

/**
* Set locale and return home url.
*
* @param string $locale
* @param \Illuminate\Http\Request $request
*
* @return string
**/
public function home($locale, Request $request)
{
$this->setLocale($locale, $request);

return redirect(url('/'));
}

/**
* Set locale and return back.
*
* @param string $locale
* @param \Illuminate\Http\Request $request
*
* @return string
**/
public function back($locale, Request $request)
{
$this->setLocale($locale, $request);

return redirect()->back();
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@ class Kernel extends HttpKernel
'lock' => \App\Http\Middleware\LockAccount::class,
'immune' => \App\Http\Middleware\CheckForImmunity::class,
'check_ip' => \App\Http\Middleware\CheckIfAlreadyVoted::class,
'language' => \App\Http\Middleware\SetLanguage::class,
];
}
104 changes: 104 additions & 0 deletions app/Http/Middleware/SetLanguage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://choosealicense.com/licenses/gpl-3.0/ GNU General Public License v3.0
* @author HDVinnie
*/

namespace App\Http\Middleware;

use App;
use Auth;
use Closure;

use App\Language;

use Carbon\Carbon;

class SetLanguage
{
/**
* This function checks if language to set is an allowed lang of config.
*
* @param string $locale
**/
private function setLocale($locale)
{
// Check if is allowed and set default locale if not
if (!Language::allowed($locale)) {
$locale = config('app.locale');
}

// Set app language
App::setLocale($locale);

// Set carbon language
if (config('language.carbon')) {
// Carbon uses only language code
if (config('language.mode.code') == 'long') {
$locale = explode('-', $locale)[0];
}

Carbon::setLocale($locale);
}

// Set date language
if (config('language.date')) {
// Date uses only language code
if (config('language.mode.code') == 'long') {
$locale = explode('-', $locale)[0];
}

\Date::setLocale($locale);
}
}

public function setDefaultLocale()
{
$this->setLocale(config('app.locale'));
}

public function setUserLocale()
{
$user = Auth::user();

if ($user->locale) {
$this->setLocale($user->locale);
} else {
$this->setDefaultLocale();
}
}

public function setSystemLocale($request)
{
if ($request->session()->has('locale')) {
$this->setLocale(session('locale'));
} else {
$this->setDefaultLocale();
}
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::check()) {
$this->setUserLocale();
} else {
$this->setSystemLocale($request);
}

return $next($request);
}
}

0 comments on commit e1934a2

Please sign in to comment.