Skip to content

Commit 676608c

Browse files
niels-numbersMarc Cámara
authored andcommitted
Add common issues to read.me (#668)
* Add common issues to read.me The issue that a POST is not working, seems to came up very offten in the issue section. Therefore this commit integrates that into the readme.md I also replaced `getLocalizedURL` with `localizeURL` because its shorter, and I think most people just want to pass the `url` anyway. * Added not obvious duplicates of post-issue * grammatically adjustments
1 parent 3cb8c56 commit 676608c

File tree

1 file changed

+67
-21
lines changed

1 file changed

+67
-21
lines changed

README.md

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ Ask [mcamara](https://github.com/mcamara) if you want to be one of them!
2828
- <a href="#route-model-binding">Route Model Binding</a>
2929
- <a href="#translated-routes">Translated Routes</a>
3030
- <a href="#caching-routes">Caching routes</a>
31-
- <a href="#testing">Testing</a>
3231
- <a href="#changelog">Changelog</a>
32+
- <a href="#testing">Testing</a>
33+
- <a href="#common-issues">Common Issues</a>
34+
- <a href="#post-is-not-working">POST is not working</a>
35+
- <a href="#methodnotallowedhttpexception">MethodNotAllowedHttpException</a>
36+
- <a href="#validation-message-is-only-in-default-locale">Validation message is always in default locale</a>
3337
- <a href="#license">License</a>
3438

3539
## Laravel compatibility
@@ -252,29 +256,31 @@ LaravelLocalization::getLocalizedURL('uk', 'a/b/c'); // http://url-to-laravel/uk
252256

253257
This package comes with some useful functions, like:
254258

255-
### Get URL for an specific locale
259+
260+
### Get localized url
256261

257262
```php
258-
/**
259-
* Returns an URL adapted to $locale
260-
*
261-
* @param string|boolean $locale Locale to adapt, false to remove locale
262-
* @param string|false $url URL to adapt in the current language. If not passed, the current url would be taken.
263-
* @param array $attributes Attributes to add to the route, if empty, the system would try to extract them from the url.
264-
*
265-
* @throws UnsupportedLocaleException
266-
*
267-
* @return string|false URL translated, False if url does not exist
268-
*/
269-
public function getLocalizedURL($locale = null, $url = null, $attributes = array())
263+
/**
264+
* Returns an URL adapted to $locale or current locale.
265+
*
266+
* @param string $url URL to adapt. If not passed, the current url would be taken.
267+
* @param string|bool $locale Locale to adapt, false to remove locale
268+
*
269+
* @throws UnsupportedLocaleException
270+
*
271+
* @return string URL translated
272+
*/
273+
public function localizeURL($url = null, $locale = null)
274+
```
270275

271276
//Should be called in a view like this:
272-
{{ LaravelLocalization::getLocalizedURL(optional string $locale, optional string $url, optional array $attributes) }}
273-
```
277+
{{ LaravelLocalization::localizeURL('/about') }}
278+
274279

275-
It returns a URL localized to the desired locale.
280+
It returns a URL localized to the desired locale (if no locale is given, it uses current locale).
276281

277-
##### Route Model Binding
282+
283+
#### Route Model Binding
278284

279285
Note that [route model binding]([https://laravel.com/docs/master/routing#route-model-binding]) is taken into account when generating the localized route.
280286

@@ -293,7 +299,7 @@ Note that [route model binding]([https://laravel.com/docs/master/routing#route-m
293299
public function getNonLocalizedURL($url = null)
294300

295301
//Should be called in a view like this:
296-
{{ LaravelLocalization::getNonLocalizedURL(optional string $url) }}
302+
{{ LaravelLocalization::getNonLocalizedURL('/es/about') }}
297303
```
298304

299305
It returns a URL clean of any localization.
@@ -315,7 +321,7 @@ It returns a URL clean of any localization.
315321
public function getURLFromRouteNameTranslated($locale, $transKeyName, $attributes = array())
316322

317323
//Should be called in a view like this:
318-
{{ LaravelLocalization::getURLFromRouteNameTranslated(string $locale, optional array $transKeyNames, optional array $attributes) }}
324+
{{ LaravelLocalization::getURLFromRouteNameTranslated('es', 'routes.about') }}
319325
```
320326

321327
It returns a route, localized to the desired locale using the locale passed. If the translation key does not exist in the locale given, this function will return false.
@@ -567,10 +573,50 @@ More information on support on [cached (translated) routes here](CACHING.md).
567573

568574
Note that the separate [czim/laravel-localization-route-cache](https://github.com/czim/laravel-localization-route-cache) package is no longer required.
569575

576+
## Common Issues
577+
578+
### POST is not working
579+
580+
This may happen if you do not localize your action route that is inside your `Routes::group`.
581+
This may cause a redirect, which then changes the post request into a get request.
582+
To prevent that, simply use the [localize helper](#get-localized-url).
583+
584+
For example, if you use `Auth::routes()` and put them into your `Route::group` Then
585+
586+
```
587+
<form action="/logout" method="POST">
588+
<button>Logout</button>
589+
</form>
590+
```
591+
592+
will not work. Instead, one has to use
593+
594+
```php
595+
<form action="{{ \LaravelLocalization::localizeURL('/logout') }} " method="POST">
596+
<button>Logout</button>
597+
</form>
598+
```
599+
600+
### MethodNotAllowedHttpException
601+
602+
If you do not localize your post url and use a redirect middleware,
603+
then the post request gets redirected as a get request.
604+
If you have not defined such a get route, you will cause this exception.
605+
606+
To localize your post url see the example in [POST is not working](#post-is-not-working).
607+
608+
### Validation message is only in default locale
609+
610+
This also happens if you did not localize your post url.
611+
If you don't localize your post url, the default locale is set while validating,
612+
and when returning to `back()` it shows the validation message in default locale.
613+
614+
To localize your post url see the example in [POST is not working](#post-is-not-working).
615+
570616
## Testing
571617

572618
During the test setup, the called route is not yet known. This means no language can be set.
573-
When a request is made during a test, this results in a 404 - without the prefix set the localised route does not seem to exist.
619+
When a request is made during a test, this results in a 404 - without the prefix set the localized route does not seem to exist.
574620

575621
To fix this, you can use this function to manually set the language prefix:
576622
```php

0 commit comments

Comments
 (0)