Defining the default language for ContentLanguage
aromot opened this issue · 5 comments
Hello,
If my ContentLanguage middleware is defined as follows:
(new Middlewares\ContentLanguage(['ru', 'en]))->usePath()
and that the requested URI is /jp/any-path
, the Accept-Language is ru
by default (from the code, it looks are the URI then the Request Headers but uses the first language defined as accepted/available).
I couldn't find a way to define my own default language. Is there a way?
Indeed, the default language is the first one defined in the languages list. So if you want to use en
as the default language, just change the order of the languages:
(new Middlewares\ContentLanguage(['en', 'ru']))->usePath()
Wouldn't it be more explicit to have a method to define the default language, e.g:
(new Middlewares\ContentLanguage(['en', 'ru']))->->defaultLang('en')->usePath()
What do you think?
This introduces some undesired issues:
- What happens if the defaultLang is not in the available languages?
- What happens if
Accept-Language
header is*
? (the negotiator returns the first value of the array, because has the highter priority, and may not be the defaultLang)
The decision to use the first value of the array was to have the same behaviour selecting the language using the path or the headers.
The decision to use the first value of the array was to have the same behaviour selecting the language using the path or the headers.
I understand. I think an explicit mention of that should be mentioned in the readme. Either it is clearly specified the default language should be the first of the list, either a method enables to define it.
What happens if the defaultLang is not in the available languages?
An exception could be thrown. It doesn't seem logical to have a default language not included in available languages isn't it?
What happens if Accept-Language header is *? (the negotiator returns the first value of the array, because has the highter priority, and may not be the defaultLang)
The same applies here. * = anything, thus we can define a list of available languages and one of them is the default.
Added a clarification in the readme.