mirror of
https://github.com/prurigro/hypothetical.git
synced 2024-11-21 23:52:31 -05:00
Add multi-language functionality and don't create unnecessary variables in the contact controller
This commit is contained in:
parent
93ac8b5697
commit
54613bf60b
8 changed files with 96 additions and 9 deletions
|
@ -1,6 +1,8 @@
|
||||||
SITE_NAME='Hypothetical Template'
|
SITE_NAME='Hypothetical Template'
|
||||||
SITE_DESC='A website template'
|
SITE_DESC='A website template'
|
||||||
|
|
||||||
|
DEFAULT_LANGUAGE=en
|
||||||
|
|
||||||
APP_ENV=local
|
APP_ENV=local
|
||||||
APP_DEBUG=true
|
APP_DEBUG=true
|
||||||
APP_LOG_LEVEL=debug
|
APP_LOG_LEVEL=debug
|
||||||
|
|
|
@ -14,14 +14,10 @@ class ContactController extends Controller {
|
||||||
'message' => 'required'
|
'message' => 'required'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$name = $request['name'];
|
|
||||||
$email = $request['email'];
|
|
||||||
$message = $request['message'];
|
|
||||||
|
|
||||||
$contact = new Contact;
|
$contact = new Contact;
|
||||||
$contact->name = $name;
|
$contact->name = $request['name'];
|
||||||
$contact->email = $email;
|
$contact->email = $request['email'];
|
||||||
$contact->message = $message;
|
$contact->message = $request['message'];
|
||||||
$contact->save();
|
$contact->save();
|
||||||
|
|
||||||
// Send the email if the MAIL_SENDTO variable is set
|
// Send the email if the MAIL_SENDTO variable is set
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Blade;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
|
@ -13,7 +14,9 @@ class AppServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
//
|
Blade::directive('lang', function($expression) {
|
||||||
|
return "<?php echo Language::select($expression); ?>";
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
57
app/Utilities/Language.php
Normal file
57
app/Utilities/Language.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Utilities;
|
||||||
|
|
||||||
|
class Language
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The language cookie name
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static $language_cookie = 'locale';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the language from the cookie or fall back on the default
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getSessionLanguage()
|
||||||
|
{
|
||||||
|
return session(self::$language_cookie, env('DEFAULT_LANGUAGE', 'en'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the configured language cookie
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function setSessionLanguage($language)
|
||||||
|
{
|
||||||
|
session([ self::$language_cookie => $language ]);
|
||||||
|
return self::getSessionLanguage() == $language;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Take an array of strings and return the string associated with
|
||||||
|
* the currently configured language or fall back on the default
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function select($string_array)
|
||||||
|
{
|
||||||
|
$session_language = self::getSessionLanguage();
|
||||||
|
$default_language = env('DEFAULT_LANGUAGE');
|
||||||
|
$string = '';
|
||||||
|
|
||||||
|
if (array_key_exists($session_language, $string_array)) {
|
||||||
|
$string = $string_array[$session_language];
|
||||||
|
} else if (array_key_exists($default_language, $string_array)) {
|
||||||
|
$string = $string_array[$default_language];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
|
@ -240,6 +240,7 @@ return [
|
||||||
'Newsletter' => Spatie\Newsletter\NewsletterFacade::class,
|
'Newsletter' => Spatie\Newsletter\NewsletterFacade::class,
|
||||||
'Image' => Intervention\Image\Facades\Image::class,
|
'Image' => Intervention\Image\Facades\Image::class,
|
||||||
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
|
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
|
||||||
|
'Language' => App\Utilities\Language::class,
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
21
readme.md
21
readme.md
|
@ -2,6 +2,27 @@
|
||||||
|
|
||||||
The Hypothetical website template
|
The Hypothetical website template
|
||||||
|
|
||||||
|
## Utilities
|
||||||
|
|
||||||
|
### Language
|
||||||
|
|
||||||
|
The default language is set by the `DEFAULT_LANGUAGE` variable in the `.env` file. This will be the language used until it is changed, which can be done using the `/language/{lang}` route or directly using `Language::setSessionLanguage($lang)` where in both cases `lang` is the language code for a given language.
|
||||||
|
|
||||||
|
In the view, a block of text can be configured with multiple languages using the following syntax:
|
||||||
|
|
||||||
|
```php
|
||||||
|
@lang([
|
||||||
|
'en' => "This is a sentence",
|
||||||
|
'fr' => "C'est une phrase"
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```php
|
||||||
|
{{ Language::select([ 'en' => "This is a sentence", 'fr' => "C'est une phrase" ]) }}
|
||||||
|
```
|
||||||
|
|
||||||
## Dashboard
|
## Dashboard
|
||||||
|
|
||||||
Unless otherwise stated all examples in this section are to be added to `app/Http/Controllers/DashboardController.php`.
|
Unless otherwise stated all examples in this section are to be added to `app/Http/Controllers/DashboardController.php`.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="{{ Language::getSessionLanguage() }}">
|
||||||
@set('page_title', (isset($title) ? $title . ' - ' : '') . env('SITE_NAME'))
|
@set('page_title', (isset($title) ? $title . ' - ' : '') . env('SITE_NAME'))
|
||||||
@set('device_mobile', preg_match('/Mobi/', Request::header('User-Agent')) || preg_match('/iP(hone|ad|od);/', Request::header('User-Agent')))
|
@set('device_mobile', preg_match('/Mobi/', Request::header('User-Agent')) || preg_match('/iP(hone|ad|od);/', Request::header('User-Agent')))
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Utilities\Language;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Web Routes
|
| Web Routes
|
||||||
|
@ -25,6 +27,11 @@ Route::get('/contact', function() {
|
||||||
return view('website.contact');
|
return view('website.contact');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::get('/language/{lang}', function($lang) {
|
||||||
|
Language::setSessionLanguage($lang);
|
||||||
|
return redirect()->back();
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Authentication Routes
|
| Authentication Routes
|
||||||
|
|
Loading…
Reference in a new issue