From d95de8ed752f4d8afdbf6705ba094979114c05de Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Mon, 3 Sep 2018 09:15:11 +0200 Subject: [PATCH 1/5] Add language selection in registration page --- app/Http/Controllers/Auth/RegisterController.php | 11 ++++++++--- app/Http/Middleware/CheckLocale.php | 6 +++++- app/Models/Account/Account.php | 4 ++-- app/Models/User/User.php | 4 ++-- resources/lang/en/auth.php | 1 + resources/views/auth/register.blade.php | 14 ++++++++++++++ 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 8d6de902ed5..970bf0f718c 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -4,8 +4,10 @@ use App\Models\User\User; use Illuminate\Http\Request; +use App\Helpers\LocaleHelper; use App\Jobs\SendNewUserAlert; use App\Models\Account\Account; +use App\Helpers\CollectionHelper; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; @@ -48,14 +50,16 @@ public function __construct() * * @return \Illuminate\Http\Response */ - public function showRegistrationForm() + public function showRegistrationForm(Request $request) { $first = ! Account::hasAny(); if (config('monica.disable_signup') == 'true' && ! $first) { abort(403, trans('auth.signup_disabled')); } - return view('auth.register', ['first' => $first]); + return view('auth.register') + ->withFirst($first) + ->withLocales(CollectionHelper::sortByCollator(LocaleHelper::getLocaleList(), 'lang')); } /** @@ -89,7 +93,8 @@ protected function create(array $data) $data['last_name'], $data['email'], $data['password'], - \Request::ip() + \Request::ip(), + $data['lang'] ); $user = $account->users()->first(); diff --git a/app/Http/Middleware/CheckLocale.php b/app/Http/Middleware/CheckLocale.php index b48134cbe00..61b5062f0d1 100644 --- a/app/Http/Middleware/CheckLocale.php +++ b/app/Http/Middleware/CheckLocale.php @@ -18,7 +18,11 @@ class CheckLocale */ public function handle($request, Closure $next) { - $locale = LocaleHelper::getLocale(); + $locale = $request->query('lang'); + + if (empty($locale)) { + $locale = LocaleHelper::getLocale(); + } App::setLocale($locale); DateHelper::setLocale($locale); diff --git a/app/Models/Account/Account.php b/app/Models/Account/Account.php index c804eb9a47d..3c4f6e542e3 100644 --- a/app/Models/Account/Account.php +++ b/app/Models/Account/Account.php @@ -784,7 +784,7 @@ public static function hasAny() * @param string $ipAddress * @return $this */ - public static function createDefault($first_name, $last_name, $email, $password, $ipAddress = null) + public static function createDefault($first_name, $last_name, $email, $password, $ipAddress = null, $lang = null) { // create new account $account = new self; @@ -795,7 +795,7 @@ public static function createDefault($first_name, $last_name, $email, $password, $account->populateDefaultFields(); // create the first user for this account - User::createDefault($account->id, $first_name, $last_name, $email, $password, $ipAddress); + User::createDefault($account->id, $first_name, $last_name, $email, $password, $ipAddress, $lang); return $account; } diff --git a/app/Models/User/User.php b/app/Models/User/User.php index 4406caa4952..eead93db9d0 100644 --- a/app/Models/User/User.php +++ b/app/Models/User/User.php @@ -67,7 +67,7 @@ class User extends Authenticatable * @param string $ipAddress * @return $this */ - public static function createDefault($account_id, $first_name, $last_name, $email, $password, $ipAddress = null) + public static function createDefault($account_id, $first_name, $last_name, $email, $password, $ipAddress = null, $lang = null) { // create the user $user = new self; @@ -78,7 +78,7 @@ public static function createDefault($account_id, $first_name, $last_name, $emai $user->password = bcrypt($password); $user->timezone = config('app.timezone'); $user->created_at = now(); - $user->locale = App::getLocale(); + $user->locale = $lang ?: App::getLocale(); $user->save(); $user->acceptPolicy($ipAddress); diff --git a/resources/lang/en/auth.php b/resources/lang/en/auth.php index 516e770eb78..350381d7266 100644 --- a/resources/lang/en/auth.php +++ b/resources/lang/en/auth.php @@ -38,6 +38,7 @@ 'signup_no_account' => 'Don’t have an account?', 'signup' => 'Sign up', 'create_account' => 'Create the first account by signing up', + 'switch_lang' => 'Switch to :lang', 'password_reset_title' => 'Reset Password', 'password_reset_email' => 'E-Mail Address', diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php index 9bd19fc6aaa..604bd3da6c6 100644 --- a/resources/views/auth/register.blade.php +++ b/resources/views/auth/register.blade.php @@ -5,6 +5,19 @@
+
+
    + @foreach($locales as $locale) +
  • + @if (App::getLocale() == $locale['lang']) + {{ $locale['lang'] }} + @else + {{ $locale['lang'] }} + @endif +
  • + @endforeach +
+