From 54613bf60bc63803e38fdc9bbda786c821ebca6a Mon Sep 17 00:00:00 2001 From: Kevin MacMartin Date: Mon, 6 Feb 2017 23:44:07 -0500 Subject: [PATCH] Add multi-language functionality and don't create unnecessary variables in the contact controller --- .env.example | 2 + app/Http/Controllers/ContactController.php | 10 ++-- app/Providers/AppServiceProvider.php | 5 +- app/Utilities/Language.php | 57 ++++++++++++++++++++++ config/app.php | 1 + readme.md | 21 ++++++++ resources/views/layouts/base.blade.php | 2 +- routes/web.php | 7 +++ 8 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 app/Utilities/Language.php diff --git a/.env.example b/.env.example index 82e24b5..16880dd 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,8 @@ SITE_NAME='Hypothetical Template' SITE_DESC='A website template' +DEFAULT_LANGUAGE=en + APP_ENV=local APP_DEBUG=true APP_LOG_LEVEL=debug diff --git a/app/Http/Controllers/ContactController.php b/app/Http/Controllers/ContactController.php index f323398..8dfc4b4 100644 --- a/app/Http/Controllers/ContactController.php +++ b/app/Http/Controllers/ContactController.php @@ -14,14 +14,10 @@ class ContactController extends Controller { 'message' => 'required' ]); - $name = $request['name']; - $email = $request['email']; - $message = $request['message']; - $contact = new Contact; - $contact->name = $name; - $contact->email = $email; - $contact->message = $message; + $contact->name = $request['name']; + $contact->email = $request['email']; + $contact->message = $request['message']; $contact->save(); // Send the email if the MAIL_SENDTO variable is set diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 35471f6..054ab93 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -13,7 +14,9 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - // + Blade::directive('lang', function($expression) { + return ""; + }); } /** diff --git a/app/Utilities/Language.php b/app/Utilities/Language.php new file mode 100644 index 0000000..2e7277e --- /dev/null +++ b/app/Utilities/Language.php @@ -0,0 +1,57 @@ + $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; + } +} diff --git a/config/app.php b/config/app.php index 737efa4..1033fad 100644 --- a/config/app.php +++ b/config/app.php @@ -240,6 +240,7 @@ return [ 'Newsletter' => Spatie\Newsletter\NewsletterFacade::class, 'Image' => Intervention\Image\Facades\Image::class, 'Excel' => Maatwebsite\Excel\Facades\Excel::class, + 'Language' => App\Utilities\Language::class, ] diff --git a/readme.md b/readme.md index 294cc4a..0cd416d 100644 --- a/readme.md +++ b/readme.md @@ -2,6 +2,27 @@ 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 Unless otherwise stated all examples in this section are to be added to `app/Http/Controllers/DashboardController.php`. diff --git a/resources/views/layouts/base.blade.php b/resources/views/layouts/base.blade.php index f5de55c..b5f6721 100644 --- a/resources/views/layouts/base.blade.php +++ b/resources/views/layouts/base.blade.php @@ -1,5 +1,5 @@ - + @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'))) diff --git a/routes/web.php b/routes/web.php index 624f9cb..423fce4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,7 @@ back(); +}); + /* |-------------------------------------------------------------------------- | Authentication Routes