1010use yii \web \UrlNormalizerRedirectException ;
1111
1212/**
13- * UrlManager
14- *
15- * This class extends Yii's UrlManager and adds features to detect the language from the URL
16- * or from browser settings transparently. It also can persist the language in the user session
17- * and optionally in a cookie. It also adds the language parameter to any created URL.
13+ * This class extends Yii's UrlManager and adds features to detect the language
14+ * from the URL or from browser settings transparently. It also can persist the
15+ * language in the user session and optionally in a cookie. It also adds the
16+ * language parameter to any created URL.
1817 */
1918class UrlManager extends BaseUrlManager
2019{
2120 /**
22- * @var array list of available language codes. More specific patterns should come first, e.g. 'en_us'
23- * before 'en'. This can also contain mapping of <url_value> => <language>, e.g. 'english' => 'en'.
21+ * @var array list of available language codes. More specific patterns
22+ * should come first, e.g. 'en_us' before 'en'. This can also contain
23+ * mapping of <url_value> => <language>, e.g. 'english' => 'en'.
2424 */
2525 public $ languages = [];
2626
@@ -30,42 +30,48 @@ class UrlManager extends BaseUrlManager
3030 public $ enableLocaleUrls = true ;
3131
3232 /**
33- * @var bool whether the default language should use an URL code like any other configured language.
33+ * @var bool whether the default language should use an URL code like any
34+ * other configured language.
3435 *
35- * By default this is `false`, so for URLs without a language code the default language is assumed.
36- * In addition any request to an URL that contains the default language code will be redirected to
37- * the same URL without a language code. So if the default language is `fr` and a user requests
38- * `/fr/some/page` he gets redirected to `/some/page`. This way the persistet language can be reset
39- * to the default language.
36+ * By default this is `false`, so for URLs without a language code the
37+ * default language is assumed. In addition any request to an URL that
38+ * contains the default language code will be redirected to the same URL
39+ * without a language code. So if the default language is `fr` and a user
40+ * requests `/fr/some/page` he gets redirected to `/some/page`. This way
41+ * the persistet language can be reset to the default language.
4042 *
41- * If this is `true`, then an URL that does not contain any language code will be redirected to the
42- * same URL with default language code. So if for example the default language is `fr`, then
43- * any request to `/some/page` will be redirected to `/fr/some/page`.
43+ * If this is `true`, then an URL that does not contain any language code
44+ * will be redirected to the same URL with default language code. So if for
45+ * example the default language is `fr`, then any request to `/some/page`
46+ * will be redirected to `/fr/some/page`.
4447 *
4548 */
4649 public $ enableDefaultLanguageUrlCode = false ;
4750
4851 /**
49- * @var bool whether to detect the app language from the HTTP headers (i.e. browser settings).
50- * Default is `true`.
52+ * @var bool whether to detect the app language from the HTTP headers (i.e.
53+ * browser settings). Default is `true`.
5154 */
5255 public $ enableLanguageDetection = true ;
5356
5457 /**
55- * @var bool whether to store the detected language in session and (optionally) a cookie. If this
56- * is `true` (default) and a returning user tries to access any URL without a language prefix,
57- * he'll be redirected to the respective stored language URL (e.g. /some/page -> /fr/some/page).
58+ * @var bool whether to store the detected language in session and
59+ * (optionally) a cookie. If this is `true` (default) and a returning user
60+ * tries to access any URL without a language prefix, he'll be redirected
61+ * to the respective stored language URL (e.g. /some/page ->
62+ * /fr/some/page).
5863 */
5964 public $ enableLanguagePersistence = true ;
6065
6166 /**
62- * @var bool whether to keep upper case language codes in URL. Default is `false` wich will e.g.
63- * redirect `de-AT` to `de-at`.
67+ * @var bool whether to keep upper case language codes in URL. Default is
68+ * `false` wich will e.g. redirect `de-AT` to `de-at`.
6469 */
6570 public $ keepUppercaseLanguageCode = false ;
6671
6772 /**
68- * @var string the name of the session key that is used to store the language. Default is '_language'.
73+ * @var string the name of the session key that is used to store the
74+ * language. Default is '_language'.
6975 */
7076 public $ languageSessionKey = '_language ' ;
7177
@@ -75,24 +81,27 @@ class UrlManager extends BaseUrlManager
7581 public $ languageCookieName = '_language ' ;
7682
7783 /**
78- * @var int number of seconds how long the language information should be stored in cookie,
79- * if `$enableLanguagePersistence` is true. Set to `false` to disable the language cookie completely.
80- * Default is 30 days.
84+ * @var int number of seconds how long the language information should be
85+ * stored in cookie, if `$enableLanguagePersistence` is true. Set to
86+ * `false` to disable the language cookie completely. Default is 30 days.
8187 */
8288 public $ languageCookieDuration = 2592000 ;
8389
8490 /**
85- * @var array configuration options for the language cookie. Note that `$languageCookieName`
86- * and `$languageCookeDuration` will override any `name` and `expire` settings provided here.
91+ * @var array configuration options for the language cookie. Note that
92+ * `$languageCookieName` and `$languageCookeDuration` will override any
93+ * `name` and `expire` settings provided here.
8794 */
8895 public $ languageCookieOptions = [];
8996
9097 /**
91- * @var array list of route and URL regex patterns to ignore during language processing. The keys
92- * of the array are patterns for routes, the values are patterns for URLs. Route patterns are checked
93- * during URL creation. If a pattern matches, no language parameter will be added to the created URL.
94- * URL patterns are checked during processing incoming requests. If a pattern matches, the language
95- * processing will be skipped for that URL. Examples:
98+ * @var array list of route and URL regex patterns to ignore during
99+ * language processing. The keys of the array are patterns for routes, the
100+ * values are patterns for URLs. Route patterns are checked during URL
101+ * creation. If a pattern matches, no language parameter will be added to
102+ * the created URL. URL patterns are checked during processing incoming
103+ * requests. If a pattern matches, the language processing will be skipped
104+ * for that URL. Examples:
96105 *
97106 * ~~~php
98107 * [
@@ -104,7 +113,8 @@ class UrlManager extends BaseUrlManager
104113 public $ ignoreLanguageUrlPatterns = [];
105114
106115 /**
107- * @var string the language that was initially set in the application configuration
116+ * @var string the language that was initially set in the application
117+ * configuration
108118 */
109119 protected $ _defaultLanguage ;
110120
@@ -114,9 +124,11 @@ class UrlManager extends BaseUrlManager
114124 public $ enablePrettyUrl = true ;
115125
116126 /**
117- * @var string if a parameter with this name is passed to any `createUrl()` method, the created URL
118- * will use the language specified there. URLs created this way can be used to switch to a different
119- * language. If no such parameter is used, the currently detected application language is used.
127+ * @var string if a parameter with this name is passed to any `createUrl()`
128+ * method, the created URL will use the language specified there. URLs
129+ * created this way can be used to switch to a different language. If no
130+ * such parameter is used, the currently detected application language is
131+ * used.
120132 */
121133 public $ languageParam = 'language ' ;
122134
@@ -145,8 +157,8 @@ public function init()
145157 }
146158
147159 /**
148- * @return string the `language` option that was initially set in the application config file,
149- * before it was modified by this component.
160+ * @return string the `language` option that was initially set in the
161+ * application config file, before it was modified by this component.
150162 */
151163 public function getDefaultLanguage ()
152164 {
@@ -165,7 +177,8 @@ public function parseRequest($request)
165177 $ pathInfo = $ request ->getPathInfo ();
166178 foreach ($ this ->ignoreLanguageUrlPatterns as $ k => $ pattern ) {
167179 if (preg_match ($ pattern , $ pathInfo )) {
168- Yii::trace ("Ignore pattern ' $ pattern' matches ' $ pathInfo.' Skipping language processing. " , __METHOD__ );
180+ $ message = "Ignore pattern ' $ pattern' matches ' $ pathInfo.' Skipping language processing. " ;
181+ Yii::trace ($ message , __METHOD__ );
169182 $ process = false ;
170183 }
171184 }
@@ -222,8 +235,9 @@ public function createUrl($params)
222235 // ... it's not the default language or default language uses URL code ...
223236 !$ isDefaultLanguage || $ this ->enableDefaultLanguageUrlCode ||
224237
225- // ... or if a language is explicitely given, but only if either persistence or detection is enabled.
226- // This way a "reset URL" can be created for the default language.
238+ // ... or if a language is explicitely given, but only if
239+ // either persistence or detection is enabled. This way a
240+ // "reset URL" can be created for the default language.
227241 $ isLanguageGiven && ($ this ->enableLanguagePersistence || $ this ->enableLanguageDetection )
228242 )
229243 ) {
@@ -265,7 +279,8 @@ public function createUrl($params)
265279 }
266280 }
267281
268- // If we have an absolute URL the length of the host URL has to be added:
282+ // If we have an absolute URL the length of the host URL has to
283+ // be added:
269284 //
270285 // - http://www.example.com
271286 // - http://www.example.com?x=y
@@ -293,9 +308,10 @@ public function createUrl($params)
293308 }
294309
295310 /**
296- * Checks for a language or locale parameter in the URL and rewrites the pathInfo if found.
297- * If no parameter is found it will try to detect the language from persistent storage (session /
298- * cookie) or from browser settings.
311+ * Checks for a language or locale parameter in the URL and rewrites the
312+ * pathInfo if found. If no parameter is found it will try to detect the
313+ * language from persistent storage (session / cookie) or from browser
314+ * settings.
299315 *
300316 * @param bool $normalized whether a UrlNormalizer tried to redirect
301317 */
@@ -445,6 +461,8 @@ protected function matchCode($code)
445461 }
446462 if (in_array ($ language . '-* ' , $ this ->languages )) {
447463 if ($ hasDash ) {
464+ // TODO: Make wildcards work with script codes
465+ // like `sr-Latn`
448466 return [$ language , strtoupper ($ country )];
449467 } else {
450468 return [$ language , null ];
@@ -484,7 +502,8 @@ protected function matchCode($code)
484502 /**
485503 * Redirect to the current URL with given language code applied
486504 *
487- * @param string $language the language code to add. Can also be empty to not add any language code.
505+ * @param string $language the language code to add. Can also be empty to
506+ * not add any language code.
488507 * @throws \yii\base\Exception
489508 * @throws \yii\web\NotFoundHttpException
490509 */
@@ -524,8 +543,9 @@ protected function redirectToLanguage($language)
524543 Yii::trace ("Redirecting to $ url. " , __METHOD__ );
525544 Yii::$ app ->getResponse ()->redirect ($ url );
526545 if (YII2_LOCALEURLS_TEST ) {
527- // Response::redirect($url) above will call `Url::to()` internally. So to really
528- // test for the same final redirect URL here, we need to call Url::to(), too.
546+ // Response::redirect($url) above will call `Url::to()` internally.
547+ // So to really test for the same final redirect URL here, we need
548+ // to call Url::to(), too.
529549 throw new \yii \base \Exception (\yii \helpers \Url::to ($ url ));
530550 } else {
531551 Yii::$ app ->end ();
0 commit comments