Class codemix\localeurls\UrlManager
Inheritance | codemix\localeurls\UrlManager » yii\web\UrlManager |
---|
UrlManager
This class extends Yii's UrlManager and adds features to detect the language from the URL or from browser settings transparently. It also can persist the language in the user session and optionally in a cookie. It also adds the language parameter to any created URL.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$enableDefaultLanguageUrlCode | bool | Whether the default language should use an URL code like any other configured language. | codemix\localeurls\UrlManager |
$enableLanguageDetection | bool | Whether to detect the app language from the HTTP headers (i.e. browser settings). | codemix\localeurls\UrlManager |
$enableLanguagePersistence | bool | Whether to store the detected language in session and (optionally) a cookie. | codemix\localeurls\UrlManager |
$enableLocaleUrls | bool | Whether to enable locale URL specific features | codemix\localeurls\UrlManager |
$enablePrettyUrl | codemix\localeurls\UrlManager | ||
$ignoreLanguageUrlPatterns | array | List of route and URL regex patterns to ignore during language processing. | codemix\localeurls\UrlManager |
$languageCookieDuration | int | Number of seconds how long the language information should be stored in cookie,
if $enableLanguagePersistence is true. |
codemix\localeurls\UrlManager |
$languageCookieName | string | The name of the language cookie. | codemix\localeurls\UrlManager |
$languageCookieOptions | array | Configuration options for the language cookie. | codemix\localeurls\UrlManager |
$languageParam | string | If a parameter with this name is passed to any createUrl() method, the created URL
will use the language specified there. |
codemix\localeurls\UrlManager |
$languageSessionKey | string | The name of the session key that is used to store the language. | codemix\localeurls\UrlManager |
$languages | array | List of available language codes. | codemix\localeurls\UrlManager |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$_defaultLanguage | string | The language that was initially set in the application configuration | codemix\localeurls\UrlManager |
$_processed | bool | Whether locale URL was processed | codemix\localeurls\UrlManager |
$_request | \yii\web\Request | codemix\localeurls\UrlManager |
Public Methods
Protected Methods
Method | Description | Defined By |
---|---|---|
matchCode() | Tests whether the given code matches any of the configured languages. | codemix\localeurls\UrlManager |
processLocaleUrl() | Checks for a language or locale parameter in the URL and rewrites the pathInfo if found. | codemix\localeurls\UrlManager |
redirectToLanguage() | Redirect to the current URL with given language code applied | codemix\localeurls\UrlManager |
Property Details
The language that was initially set in the application configuration
Whether locale URL was processed
Whether the default language should use an URL code like any other configured language.
By default this is false
, so for URLs without a language code the default language is assumed.
In addition any request to an URL that contains the default language code will be redirected to
the same URL without a language code. So if the default language is fr
and a user requests
/fr/some/page
he gets redirected to /some/page
. This way the persistet language can be reset
to the default language.
If this is true
, then an URL that does not contain any language code will be redirected to the
same URL with default language code. So if for example the default language is fr
, then
any request to /some/page
will be redirected to /fr/some/page
.
Whether to detect the app language from the HTTP headers (i.e. browser settings).
Default is true
.
Whether to store the detected language in session and (optionally) a cookie. If this
is true
(default) and a returning user tries to access any URL without a language prefix,
he'll be redirected to the respective stored language URL (e.g. /some/page -> /fr/some/page).
Whether to enable locale URL specific features
List of route and URL regex patterns to ignore during language processing. The keys of the array are patterns for routes, the values are patterns for URLs. Route patterns are checked during URL creation. If a pattern matches, no language parameter will be added to the created URL. URL patterns are checked during processing incoming requests. If a pattern matches, the language processing will be skipped for that URL. Examples:
[
'#^site/(login|register)#' => '#^(login|register)#'
'#^api/#' => '#^api/#',
]
Number of seconds how long the language information should be stored in cookie,
if $enableLanguagePersistence
is true. Set to false
to disable the language cookie completely.
Default is 30 days.
The name of the language cookie. Default is '_language'.
Configuration options for the language cookie. Note that $languageCookieName
and $languageCookeDuration
will override any name
and expire
settings provided here.
If a parameter with this name is passed to any createUrl()
method, the created URL
will use the language specified there. URLs created this way can be used to switch to a different
language. If no such parameter is used, the currently detected application language is used.
The name of the session key that is used to store the language. Default is '_language'.
List of available language codes. More specific patterns should come first, e.g. 'en_us'
before 'en'. This can also contain mapping of
Method Details
public void createUrl ( $params ) | ||
$params |
public string getDefaultLanguage ( ) | ||
return | string | The |
---|
public void init ( ) |
Tests whether the given code matches any of the configured languages.
If the code is a single language code, and matches either
- an exact language as configured (ll)
- a language with a country wildcard (ll-*)
this language code is returned.
If the code also contains a country code, and matches either
- an exact language/country code as configured (ll-CC)
- a language with a country wildcard (ll-*)
the code with uppercase country is returned. If only the language part matches a configured language, that language is returned.
protected array matchCode ( $code ) | ||
$code | string | The code to match |
return | array | Of [language, country] where both can be null if no match |
---|
public void parseRequest ( $request ) | ||
$request |
Checks for a language or locale parameter in the URL and rewrites the pathInfo if found.
If no parameter is found it will try to detect the language from persistent storage (session / cookie) or from browser settings.
protected void processLocaleUrl ( $request ) | ||
$request | \yii\web\Request |
Redirect to the current URL with given language code applied
protected void redirectToLanguage ( $language ) | ||
$language | string | The language code to add. Can also be empty to not add any language code. |