Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use underscores in config file
  • Loading branch information
ivanvermeyen committed Mar 11, 2023
commit 1d543a575f54862ffa7dc0e58557ff93fe1c4966
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ You will now find a `localizer.php` file in the `config` folder.
Add any locales you wish to support to your published `config/localizer.php` file:

```php
'supported-locales' => ['en', 'nl'];
'supported_locales' => ['en', 'nl'];
```

You can also use one or more custom slugs for a locale:

```php
'supported-locales' => [
'supported_locales' => [
'en' => 'english-slug',
'nl' => ['dutch-slug', 'nederlandse-slug'],
];
Expand All @@ -95,7 +95,7 @@ You can also use one or more custom slugs for a locale:
Or you can use one or more custom domains for a locale:

```php
'supported-locales' => [
'supported_locales' => [
'en' => 'english-domain.test',
'nl' => ['dutch-domain.test', 'nederlands-domain.test'],
];
Expand Down Expand Up @@ -136,16 +136,17 @@ Update the `stores` array to choose which stores to use.

## 🛠️ More Configuration (optional)

### ☑️ `omitted-locale`
### ☑️ `omitted_locale`

If you don't want your main locale to have a slug, you can set it as the `omitted_locale` (not the custom slug).

If you don't want your main locale to have a slug, you can set it as the `omitted-locale` (not the custom slug).
If you do this, no additional detectors will run after the `UrlDetector` and `OmittedLocaleDetector`.
This makes sense, because the locale will always be determined by those two in this scenario.

Example:

```php
'omitted-locale' => 'en',
'omitted_locale' => 'en',
```

Result:
Expand All @@ -155,20 +156,20 @@ Result:

Default: `null`

### ☑️ `trusted-detectors`
### ☑️ `trusted_detectors`

Add any detector class name to this array to make it trusted. (do not remove it from the `detectors` array)
When a trusted detector returns a locale, it will be used as the app locale, regardless if it's a supported locale or not.

Default: `[]`

### ☑️ `url-segment`
### ☑️ `url_segment`

The index of the URL segment that has the locale, when using the `UrlDetector`.

Default: `1`

### ☑️ `route-action`
### ☑️ `route_action`

The custom route action that holds the locale, when using the `RouteActionDetector`.

Expand All @@ -182,26 +183,26 @@ Route::group(['locale' => 'nl'], function () {
});
```

### ☑️ `user-attribute`
### ☑️ `user_attribute`

The attribute on the user model that holds the locale, when using the `UserDetector`.
If the user model does not have this attribute, this detector check will be skipped.

Default: `locale`

### ☑️ `session-key`
### ☑️ `session_key`

The session key that holds the locale, when using the `SessionDetector` and `SessionStore`.

Default: `locale`

### ☑️ `cookie-name`
### ☑️ `cookie_name`

The name of the cookie that holds the locale, when using the `CookieDetector` and `CookieStore`.

Default: `locale`

### ☑️ `cookie-minutes`
### ☑️ `cookie_minutes`

The lifetime of the cookie that holds the locale, when using the `CookieStore`.

Expand Down
18 changes: 9 additions & 9 deletions config/localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
/**
* The locales you wish to support.
*/
'supported-locales' => [],
'supported_locales' => [],

/**
* If your main locale is omitted from the URL, set it here.
* It will always be used if no supported locale is found in the URL.
* Note that no other detectors will run after the OmittedLocaleDetector!
* Setting this option to `null` will disable this detector.
*/
'omitted-locale' => null,
'omitted_locale' => null,

/**
* The detectors to use to find a matching locale.
Expand All @@ -35,7 +35,7 @@
* When a trusted detector returns a locale, it will be used
* as the app locale, regardless if it's a supported locale or not.
*/
'trusted-detectors' => [
'trusted_detectors' => [
//
],

Expand All @@ -52,36 +52,36 @@
* The index of the segment that has the locale,
* when using the UrlDetector.
*/
'url-segment' => 1,
'url_segment' => 1,

/**
* The attribute or "action" on the route that holds the locale,
* when using the RouteActionDetector.
*/
'route-action' => 'locale',
'route_action' => 'locale',

/**
* The attribute on the user model that holds the locale,
* when using the UserDetector.
*/
'user-attribute' => 'locale',
'user_attribute' => 'locale',

/**
* The session key that holds the locale,
* when using the SessionDetector and SessionStore.
*/
'session-key' => 'locale',
'session_key' => 'locale',

/**
* The name of the cookie that holds the locale,
* when using the CookieDetector and CookieStore.
*/
'cookie-name' => 'locale',
'cookie_name' => 'locale',

/**
* The lifetime of the cookie that holds the locale,
* when using the CookieStore.
*/
'cookie-minutes' => 60 * 24 * 365, // 1 year
'cookie_minutes' => 60 * 24 * 365, // 1 year

];
2 changes: 1 addition & 1 deletion src/Detectors/CookieDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CookieDetector implements Detector
*/
public function detect()
{
$key = Config::get('localizer.cookie-name');
$key = Config::get('localizer.cookie_name');

return Cookie::get($key);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Detectors/OmittedLocaleDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class OmittedLocaleDetector implements Detector
*/
public function detect()
{
return Config::get('localizer.omitted-locale') ?: null;
return Config::get('localizer.omitted_locale') ?: null;
}
}
2 changes: 1 addition & 1 deletion src/Detectors/RouteActionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RouteActionDetector implements Detector
*/
public function detect()
{
$action = Config::get('localizer.route-action');
$action = Config::get('localizer.route_action');

return Request::route()->getAction($action);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Detectors/SessionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SessionDetector implements Detector
*/
public function detect()
{
$key = Config::get('localizer.session-key');
$key = Config::get('localizer.session_key');

return Session::get($key);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Detectors/UrlDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class UrlDetector implements Detector
*/
public function detect()
{
$locales = Config::get('localizer.supported-locales');
$position = Config::get('localizer.url-segment');
$locales = Config::get('localizer.supported_locales');
$position = Config::get('localizer.url_segment');
$slug = Request::segment($position);

// If supported locales is a simple array like ['en', 'nl']
Expand Down
2 changes: 1 addition & 1 deletion src/Detectors/UserDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function detect()
return null;
}

$attribute = Config::get('localizer.user-attribute');
$attribute = Config::get('localizer.user_attribute');

return $user->getAttributeValue($attribute);
}
Expand Down
4 changes: 2 additions & 2 deletions src/LocalizerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ protected function mergeConfig()
protected function registerLocalizer()
{
$this->app->bind(Localizer::class, function ($app) {
$locales = $app['config']->get("{$this->name}.supported-locales");
$locales = $app['config']->get("{$this->name}.supported_locales");
$detectors = $app['config']->get("{$this->name}.detectors");
$stores = $app['config']->get("{$this->name}.stores");
$trustedDetectors = $app['config']->get("{$this->name}.trusted-detectors");
$trustedDetectors = $app['config']->get("{$this->name}.trusted_detectors");

return new Localizer($locales, $detectors, $stores, $trustedDetectors);
});
Expand Down
4 changes: 2 additions & 2 deletions src/Stores/CookieStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class CookieStore implements Store
*/
public function store($locale)
{
$name = Config::get('localizer.cookie-name');
$minutes = Config::get('localizer.cookie-minutes');
$name = Config::get('localizer.cookie_name');
$minutes = Config::get('localizer.cookie_minutes');

Cookie::queue($name, $locale, $minutes);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stores/SessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SessionStore implements Store
*/
public function store($locale)
{
$key = Config::get('localizer.session-key');
$key = Config::get('localizer.session_key');

Session::put($key, $locale);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/SetLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected function setUp(): void
// Remove any default browser locales
$this->setBrowserLocales(null);

$this->sessionKey = Config::get('localizer.session-key');
$this->cookieName = Config::get('localizer.cookie-name');
$this->sessionKey = Config::get('localizer.session_key');
$this->cookieName = Config::get('localizer.cookie_name');
}

/** @test */
Expand Down Expand Up @@ -78,7 +78,7 @@ public function you_can_configure_which_segment_to_use_as_locale()
$this->setSupportedLocales(['en', 'nl']);
$this->setAppLocale('en');

Config::set('localizer.url-segment', 2);
Config::set('localizer.url_segment', 2);

Route::get('some/nl/route', function () {
return App::getLocale();
Expand Down Expand Up @@ -222,7 +222,7 @@ public function it_looks_for_a_locale_on_the_authenticated_user()
$this->setSupportedLocales(['en', 'nl']);
$this->setAppLocale('en');

$attribute = Config::get('localizer.user-attribute');
$attribute = Config::get('localizer.user_attribute');
$user = new User();
$user->$attribute = 'nl';

Expand Down Expand Up @@ -363,7 +363,7 @@ public function trusted_detectors_ignore_supported_locales_and_may_set_any_local

$routeAction = ['locale' => 'nl'];

Config::set('localizer.trusted-detectors', [
Config::set('localizer.trusted_detectors', [
\CodeZero\Localizer\Detectors\RouteActionDetector::class,
]);

Expand Down Expand Up @@ -403,7 +403,7 @@ protected function setAppLocale($locale)
*/
protected function setSupportedLocales(array $locales)
{
Config::set('localizer.supported-locales', $locales);
Config::set('localizer.supported_locales', $locales);

return $this;
}
Expand All @@ -417,7 +417,7 @@ protected function setSupportedLocales(array $locales)
*/
protected function setOmittedLocale($locale)
{
Config::set('localizer.omitted-locale', $locale);
Config::set('localizer.omitted_locale', $locale);

return $this;
}
Expand Down