Skip to content
This repository has been archived by the owner on Feb 8, 2025. It is now read-only.

Commit

Permalink
feat: detect language from browser (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored May 30, 2021
1 parent 59f17d3 commit 880b9ee
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
5 changes: 3 additions & 2 deletions app/Helpers/LocaleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Vluzrmos\LanguageDetector\Facades\LanguageDetector;

class LocaleHelper
{
Expand All @@ -18,7 +19,7 @@ public static function getLocale()
{
$locale = Auth::check() ? Auth::user()->locale : null;
if (!$locale) {
$locale = config('app.locale');
$locale = LanguageDetector::detect() ?: config('app.locale');
}

return $locale;
Expand Down Expand Up @@ -48,7 +49,7 @@ public static function getLang($locale = null)
*/
public static function getLocaleList()
{
return collect(config('app.languages'))->map(function ($lang) {
return collect(config('lang-detector.languages'))->map(function ($lang) {
return [
'lang' => $lang,
'name' => self::getLocaleName($lang),
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"spatie/laravel-activitylog": "^3.1",
"spatie/simple-excel": "^1.13",
"tightenco/ziggy": "^1.0",
"uploadcare/uploadcare-php": "^3.0"
"uploadcare/uploadcare-php": "^3.0",
"vluzrmos/language-detector": "^2.3"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.2",
Expand Down
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Sentry\Laravel\ServiceProvider::class,
],
Expand Down Expand Up @@ -237,12 +238,4 @@

],

/*
|--------------------------------------------------------------------------
| Available languages
|--------------------------------------------------------------------------
*/

'languages' => ['en', 'fr'],

];
46 changes: 46 additions & 0 deletions config/lang-detector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

return [
/*
* Indicates whenever should autodetect and apply the language of the request.
*/
'autodetect' => env('LANG_DETECTOR_AUTODETECT', false),

/*
* Default driver to use to detect the request language.
*
* Available: browser, subdomain, uri.
*/
'driver' => env('LANG_DETECTOR_DRIVER', 'browser'),

/*
* Used on subdomain and uri drivers. That indicates which segment should be used
* to verify the language.
*/
'segment' => env('LANG_DETECTOR_SEGMENT', 0),

/*
* Languages available on the application.
*
* You could use parse_langs_to_array to use the string syntax
* or just use the array of languages with its aliases.
*/
'languages' => parse_langs_to_array(
env('LANG_DETECTOR_LANGUAGES', ['en', 'fr'])
),

/*
* Indicates if should store detected locale on cookies
*/
'cookie' => (bool) env('LANG_DETECTOR_COOKIE', true),

/*
* Indicates if should encrypt cookie
*/
'cookie_encrypt' => (bool) env('LANG_DETECTOR_COOKIE_ENCRYPT', false),

/*
* Cookie name
*/
'cookie_name' => env('LANG_DETECTOR_COOKIE', 'locale'),
];

0 comments on commit 880b9ee

Please sign in to comment.