Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
3e641910
Commit
3e641910
authored
Jun 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented language negotiation.
Implemented Request::getAcceptedContentTypes().
parent
4782b197
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
114 additions
and
21 deletions
+114
-21
Request.php
framework/yii/web/Request.php
+114
-21
No files found.
framework/yii/web/Request.php
View file @
3e641910
...
...
@@ -658,40 +658,133 @@ class Request extends \yii\base\Request
}
}
private
$_
preferredLanguag
es
;
private
$_
contentTyp
es
;
/**
* Returns the
user preferred languages
.
* Th
e languages returned are ordered by user's preference, starting with the language that the user
*
prefers the most.
*
@return string the user preferred languages. An empty array may be returned if the user has no preferenc
e.
* Returns the
content types accepted by the end user
.
* Th
is is determined by the `Accept` HTTP header.
*
@return array the content types ordered by the preference level. The first element
*
represents the most preferred content typ
e.
*/
public
function
get
PreferredLanguag
es
()
public
function
get
AcceptedContentTyp
es
()
{
if
(
$this
->
_preferredLanguages
===
null
)
{
if
(
isset
(
$_SERVER
[
'HTTP_ACCEPT_LANGUAGE'
])
&&
(
$n
=
preg_match_all
(
'/([\w\-_]+)\s*(;\s*q\s*=\s*(\d*\.\d*))?/'
,
$_SERVER
[
'HTTP_ACCEPT_LANGUAGE'
],
$matches
))
>
0
)
{
$languages
=
array
();
for
(
$i
=
0
;
$i
<
$n
;
++
$i
)
{
$languages
[
$matches
[
1
][
$i
]]
=
empty
(
$matches
[
3
][
$i
])
?
1.0
:
floatval
(
$matches
[
3
][
$i
]);
if
(
$this
->
_contentTypes
===
null
)
{
if
(
isset
(
$_SERVER
[
'HTTP_ACCEPT'
]))
{
$this
->
_contentTypes
=
$this
->
parseAcceptHeader
(
$_SERVER
[
'HTTP_ACCEPT'
]);
}
else
{
$this
->
_contentTypes
=
array
();
}
}
return
$this
->
_contentTypes
;
}
/**
* @param array $value the content types that are accepted by the end user. They should
* be ordered by the preference level.
*/
public
function
setAcceptedContentTypes
(
$value
)
{
$this
->
_contentTypes
=
$value
;
}
private
$_languages
;
/**
* Returns the languages accepted by the end user.
* This is determined by the `Accept-Language` HTTP header.
* @return array the languages ordered by the preference level. The first element
* represents the most preferred language.
*/
public
function
getAcceptedLanguages
()
{
if
(
$this
->
_languages
===
null
)
{
if
(
isset
(
$_SERVER
[
'HTTP_ACCEPT_LANGUAGE'
]))
{
$this
->
_languages
=
$this
->
parseAcceptHeader
(
$_SERVER
[
'HTTP_ACCEPT_LANGUAGE'
]);
}
else
{
$this
->
_languages
=
array
();
}
}
arsort
(
$languages
);
$this
->
_preferredLanguages
=
array_keys
(
$languages
);
return
$this
->
_languages
;
}
/**
* @param array $value the languages that are accepted by the end user. They should
* be ordered by the preference level.
*/
public
function
setAcceptedLanguages
(
$value
)
{
$this
->
_languages
=
$value
;
}
/**
* Parses the given `Accept` (or `Accept-Language`) header.
* This method will return the accepted values ordered by their preference level.
* @param string $header the header to be parsed
* @return array the accept values ordered by their preference level.
*/
protected
function
parseAcceptHeader
(
$header
)
{
$accepts
=
array
();
$n
=
preg_match_all
(
'/\s*([\w\/\-\*]+)\s*(?:;\s*q\s*=\s*([\d\.]+))?[^,]*/'
,
$header
,
$matches
,
PREG_SET_ORDER
);
for
(
$i
=
0
;
$i
<
$n
;
++
$i
)
{
if
(
!
empty
(
$matches
[
$i
][
1
]))
{
$accepts
[]
=
array
(
$matches
[
$i
][
1
],
isset
(
$matches
[
$i
][
2
])
?
(
float
)
$matches
[
$i
][
2
]
:
1
,
$i
);
}
}
usort
(
$accepts
,
function
(
$a
,
$b
)
{
if
(
$a
[
1
]
>
$b
[
1
])
{
return
-
1
;
}
elseif
(
$a
[
1
]
<
$b
[
1
])
{
return
1
;
}
elseif
(
$a
[
0
]
===
$b
[
0
])
{
return
$a
[
2
]
>
$b
[
2
]
?
1
:
-
1
;
}
elseif
(
$a
[
0
]
===
'*/*'
)
{
return
1
;
}
elseif
(
$b
[
0
]
===
'*/*'
)
{
return
-
1
;
}
else
{
$this
->
_preferredLanguages
=
array
();
$wa
=
$a
[
0
][
strlen
(
$a
[
0
])
-
1
]
===
'*'
;
$wb
=
$b
[
0
][
strlen
(
$b
[
0
])
-
1
]
===
'*'
;
if
(
$wa
xor
$wb
)
{
return
$wa
?
1
:
-
1
;
}
else
{
return
$a
[
2
]
>
$b
[
2
]
?
1
:
-
1
;
}
}
});
$result
=
array
();
foreach
(
$accepts
as
$accept
)
{
$result
[]
=
$accept
[
0
];
}
return
$this
->
_preferredLanguages
;
return
array_unique
(
$result
)
;
}
/**
* Returns the language most preferred by the user.
* @return string|boolean the language most preferred by the user. If the user has no preference, false
* will be returned.
* Returns the user-preferred language that should be used by this application.
* The language resolution is based on the user preferred languages and the languages
* supported by the application. The method will try to find the best match.
* @param array $languages a list of the languages supported by the application.
* If empty, this method will return the first language returned by [[getAcceptedLanguages()]].
* @return string the language that the application should use. Null is returned if both [[getAcceptedLanguages()]]
* and `$languages` are empty.
*/
public
function
getPreferredLanguage
()
public
function
getPreferredLanguage
(
$languages
=
array
()
)
{
$languages
=
$this
->
getPreferredLanguages
();
return
isset
(
$languages
[
0
])
?
$languages
[
0
]
:
false
;
$acceptedLanguages
=
$this
->
getAcceptedLanguages
();
if
(
empty
(
$languages
))
{
return
isset
(
$acceptedLanguages
[
0
])
?
$acceptedLanguages
[
0
]
:
null
;
}
foreach
(
$acceptedLanguages
as
$acceptedLanguage
)
{
$acceptedLanguage
=
str_replace
(
'-'
,
'_'
,
strtolower
(
$acceptedLanguage
));
foreach
(
$languages
as
$language
)
{
$language
=
str_replace
(
'-'
,
'_'
,
strtolower
(
$language
));
// en_us==en_us, en==en_us, en_us==en
if
(
$language
===
$acceptedLanguage
||
strpos
(
$acceptedLanguage
,
$language
.
'_'
)
===
0
||
strpos
(
$language
,
$acceptedLanguage
.
'_'
)
===
0
)
{
return
$language
;
}
}
}
return
reset
(
$languages
);
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment