Skip to content

Commit

Permalink
User now could change his own language on the profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanhao Li authored and ifox committed Mar 2, 2020
1 parent 01f629d commit becebc5
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 1 deletion.
40 changes: 40 additions & 0 deletions migrations/add_language_column_to_twill_users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddLanguageColumnToTwillUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$twillUsersTable = config('twill.users_table', 'twill_users');

if (Schema::hasTable($twillUsersTable) && !Schema::hasColumn($twillUsersTable, 'language')) {
Schema::table($twillUsersTable, function (Blueprint $table) {
$table->string('language')->nullable();
});
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$twillUsersTable = config('twill.users_table', 'twill_users');

if (Schema::hasTable($twillUsersTable) && Schema::hasColumn($twillUsersTable, 'language')) {
Schema::table($twillUsersTable, function (Blueprint $table) {
$table->dropColumn('language');
});
}
}
}
159 changes: 159 additions & 0 deletions src/Helpers/i18n_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,162 @@ function camelCaseToWords($camelCaseString)
return ucfirst(strtolower($words));
}
}

if (!function_exists('getLanguageNativeNameFromCode')) {
/**
* @param string $code
* @return string
*/
function getLanguageNativeNameFromCode($code)
{
$codeToLanguageMappings = [
'ar' =>
[
'english_name' => 'Arabic',
'native_name' => 'العربية',
],
'bg' =>
[
'english_name' => 'Bulgarian',
'native_name' => 'български език',
],
'zh-CN' =>
[
'english_name' => 'Chinese (Simplified)',
'native_name' => '简体中文',
],
'zh-TW' =>
[
'english_name' => 'Chinese (Traditional)',
'native_name' => '繁體中文',
],
'cs' =>
[
'english_name' => 'Czech',
'native_name' => 'čeština',
],
'da' =>
[
'english_name' => 'Danish',
'native_name' => 'Dansk',
],
'nl' =>
[
'english_name' => 'Dutch',
'native_name' => 'Nederlands',
],
'en' =>
[
'english_name' => 'English',
'native_name' => 'English',
],
'fi' =>
[
'english_name' => 'Finnish',
'native_name' => 'Suomi',
],
'fr' =>
[
'english_name' => 'French',
'native_name' => 'Français',
],
'de' =>
[
'english_name' => 'German',
'native_name' => 'Deutsch',
],
'el' =>
[
'english_name' => 'Greek',
'native_name' => 'Ελληνικά',
],
'hu' =>
[
'english_name' => 'Hungarian',
'native_name' => 'Magyar',
],
'it' =>
[
'english_name' => 'Italian',
'native_name' => 'Italiano',
],
'ja' =>
[
'english_name' => 'Japanese',
'native_name' => '日本語',
],
'ko' =>
[
'english_name' => 'Korean',
'native_name' => '한국어',
],
'no' =>
[
'english_name' => 'Norwegian',
'native_name' => 'Norsk',
],
'pl' =>
[
'english_name' => 'Polish',
'native_name' => 'Polski',
],
'pt' =>
[
'english_name' => 'Portuguese',
'native_name' => 'Português',
],
'pt-BR' =>
[
'english_name' => 'Portuguese-Brazil',
'native_name' => 'Português-Brasil',
],
'ro' =>
[
'english_name' => 'Romanian',
'native_name' => 'Română',
],
'ru' =>
[
'english_name' => 'Russian',
'native_name' => 'Русский',
],
'es' =>
[
'english_name' => 'Spanish-Spain',
'native_name' => 'Español-España',
],
'es-419' =>
[
'english_name' => 'Spanish-Latin America',
'native_name' => 'Español-Latinoamérica',
],
'sv' =>
[
'english_name' => 'Swedish',
'native_name' => 'Svenska',
],
'th' =>
[
'english_name' => 'Thai',
'native_name' => 'ไทย',
],
'tr' =>
[
'english_name' => 'Turkish',
'native_name' => 'Türkçe',
],
'uk' =>
[
'english_name' => 'Ukrainian',
'native_name' => 'Українська',
],
'vn' =>
[
'english_name' => 'Vietnamese',
'native_name' => 'Tiếng Việt',
],
];

return $codeToLanguageMappings[$code]['native_name'] ?? $code ;
}
}
24 changes: 24 additions & 0 deletions src/Http/Middleware/Localization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace A17\Twill\Http\Middleware;

use Closure;

class Localization
{
/**
* Handles an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->user()->language) {
app()->setLocale($request->user()->language);
}

return $next($request);
}
}
1 change: 1 addition & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class User extends AuthenticatableContract
'description',
'google_2fa_enabled',
'google_2fa_secret',
'language'
];

protected $dates = [
Expand Down
7 changes: 7 additions & 0 deletions src/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace A17\Twill\Repositories;

use App;
use A17\Twill\Models\User;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Repositories\Behaviors\HandleOauth;
Expand Down Expand Up @@ -133,6 +134,12 @@ public function prepareFieldsBeforeSave($user, $fields)
public function afterSave($user, $fields)
{
$this->sendWelcomeEmail($user);
$language = $fields['language'];
if ($language !== App::getLocale()) {
$user->language = $language;
session()->put('twill_locale', $language);
$user->save();
}
parent::afterSave($user, $fields);
}

Expand Down
20 changes: 19 additions & 1 deletion views/users/form.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@extends('twill::layouts.form', [
'contentFieldsetLabel' => __('twill::lang.user-management.content-fieldset-label'),
'editModalTitle' => __('twill::lang.user-management.edit-modal-title'),
'reloadOnSuccess' => true
'reloadOnSuccess' => false
])

@php
Expand Down Expand Up @@ -44,6 +44,7 @@
'label' => 'Description'
])
@endif

@if($with2faSettings ?? false)
@formField('checkbox', [
'name' => 'google_2fa_enabled',
Expand Down Expand Up @@ -76,6 +77,23 @@
@endcomponent
@endunless
@endif

@formField('select', [
'name' => 'language',
'label' => 'Language',
'placeholder' => 'Select a language',
'default' => App::getLocale(),
'options' => [
[
'value' => 'en',
'label' => getLanguageNativeNameFromCode('en')
],
[
'value' => 'ja',
'label' => getLanguageNativeNameFromCode('ja')
]
]
])
@stop

@push('vuexStore')
Expand Down

0 comments on commit becebc5

Please sign in to comment.