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
2eb5abbf
Commit
2eb5abbf
authored
Oct 16, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved unit tests for ICU message formatter
parent
150b9366
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
55 deletions
+87
-55
MessageFormatter.php
framework/yii/i18n/MessageFormatter.php
+3
-2
MessageFormatterTest.php
tests/unit/framework/i18n/MessageFormatterTest.php
+84
-53
No files found.
framework/yii/i18n/MessageFormatter.php
View file @
2eb5abbf
...
@@ -15,6 +15,7 @@ namespace yii\i18n;
...
@@ -15,6 +15,7 @@ namespace yii\i18n;
* substituted.
* substituted.
*
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @author Alexander Makarov <sam@rmcreative.ru>
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
* @since 2.0
*/
*/
class
MessageFormatter
extends
\MessageFormatter
class
MessageFormatter
extends
\MessageFormatter
...
@@ -55,7 +56,7 @@ class MessageFormatter extends \MessageFormatter
...
@@ -55,7 +56,7 @@ class MessageFormatter extends \MessageFormatter
}
}
/**
/**
* Replace named placeholders with numeric placeholders.
* Replace named placeholders with numeric placeholders
and quote unused
.
*
*
* @param string $pattern The pattern string to replace things into.
* @param string $pattern The pattern string to replace things into.
* @param array $args The array of values to insert into the format string.
* @param array $args The array of values to insert into the format string.
...
@@ -65,7 +66,7 @@ class MessageFormatter extends \MessageFormatter
...
@@ -65,7 +66,7 @@ class MessageFormatter extends \MessageFormatter
{
{
$map
=
array_flip
(
array_keys
(
$args
));
$map
=
array_flip
(
array_keys
(
$args
));
// parsing pattern base on ICU grammar:
// parsing pattern base
d
on ICU grammar:
// http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
// http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
$parts
=
explode
(
'{'
,
$pattern
);
$parts
=
explode
(
'{'
,
$pattern
);
$c
=
count
(
$parts
);
$c
=
count
(
$parts
);
...
...
tests/unit/framework/i18n/MessageFormatterTest.php
View file @
2eb5abbf
...
@@ -22,18 +22,19 @@ class MessageFormatterTest extends TestCase
...
@@ -22,18 +22,19 @@ class MessageFormatterTest extends TestCase
const
SUBJECT
=
'сабж'
;
const
SUBJECT
=
'сабж'
;
const
SUBJECT_VALUE
=
'Answer to the Ultimate Question of Life, the Universe, and Everything'
;
const
SUBJECT_VALUE
=
'Answer to the Ultimate Question of Life, the Universe, and Everything'
;
public
function
testNamedArgument
s
()
public
function
pattern
s
()
{
{
$expected
=
self
::
SUBJECT_VALUE
.
' is '
.
self
::
N_VALUE
;
return
array
(
array
(
'{'
.
self
::
SUBJECT
.
'} is {'
.
self
::
N
.
', number}'
,
// pattern
self
::
SUBJECT_VALUE
.
' is '
.
self
::
N_VALUE
,
// expected
array
(
// params
self
::
N
=>
self
::
N_VALUE
,
self
::
SUBJECT
=>
self
::
SUBJECT_VALUE
,
)
),
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
'{'
.
self
::
SUBJECT
.
'} is {'
.
self
::
N
.
', number}'
,
array
(
array
(
<<<_MSG_
self
::
N
=>
self
::
N_VALUE
,
self
::
SUBJECT
=>
self
::
SUBJECT_VALUE
,
));
$this
->
assertEquals
(
$expected
,
$result
);
$pattern
=
<<<_MSG_
{gender_of_host, select,
{gender_of_host, select,
female {{num_guests, plural, offset:1
female {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=0 {{host} does not give a party.}
...
@@ -50,53 +51,83 @@ class MessageFormatterTest extends TestCase
...
@@ -50,53 +51,83 @@ class MessageFormatterTest extends TestCase
=1 {{host} invites {guest} to their party.}
=1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.}
=2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} and # other people to their party.}}}}
other {{host} invites {guest} and # other people to their party.}}}}
_MSG_;
_MSG_
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
,
'gender_of_host'
=>
'male'
,
'ralph invites beep and 3 other people to his party.'
,
'num_guests'
=>
4
,
array
(
'host'
=>
'ralph'
,
'gender_of_host'
=>
'male'
,
'guest'
=>
'beep'
'num_guests'
=>
4
,
));
'host'
=>
'ralph'
,
$this
->
assertEquals
(
'ralph invites beep and 3 other people to his party.'
,
$result
);
'guest'
=>
'beep'
)
),
$pattern
=
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
;
array
(
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
,
'name'
=>
'Alexander'
,
'Alexander is male and he loves Yii!'
,
'gender'
=>
'male'
,
array
(
));
'name'
=>
'Alexander'
,
$this
->
assertEquals
(
'Alexander is male and he loves Yii!'
,
$result
);
'gender'
=>
'male'
,
),
),
// verify pattern in select does not get replaced
// verify pattern in select does not get replaced
$pattern
=
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
;
array
(
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
,
'name'
=>
'Alexander'
,
'Alexander is male and he loves Yii!'
,
'gender'
=>
'male'
,
array
(
// following should not be replaced
'name'
=>
'Alexander'
,
'he'
=>
'wtf'
,
'gender'
=>
'male'
,
'she'
=>
'wtf'
,
// following should not be replaced
'it'
=>
'wtf'
,
'he'
=>
'wtf'
,
));
'she'
=>
'wtf'
,
$this
->
assertEquals
(
'Alexander is male and he loves Yii!'
,
$result
);
'it'
=>
'wtf'
,
)
),
// verify pattern in select message gets replaced
// verify pattern in select message gets replaced
$pattern
=
'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'
;
array
(
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'
,
'name'
=>
'Alexander'
,
'Alexander is male and wtf loves Yii!'
,
'gender'
=>
'male'
,
array
(
'he'
=>
'wtf'
,
'name'
=>
'Alexander'
,
'she'
=>
'wtf'
,
'gender'
=>
'male'
,
));
'he'
=>
'wtf'
,
$this
->
assertEquals
(
'Alexander is male and wtf loves Yii!'
,
$result
);
'she'
=>
'wtf'
,
),
),
// some parser specific verifications
// some parser specific verifications
$pattern
=
'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!'
;
array
(
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!'
,
'nr'
=>
42
,
'male and wtf loves 42 is male!'
,
'gender'
=>
'male'
,
array
(
'he'
=>
'wtf'
,
'nr'
=>
42
,
'she'
=>
'wtf'
,
'gender'
=>
'male'
,
));
'he'
=>
'wtf'
,
$this
->
assertEquals
(
'male and wtf loves 42 is male!'
,
$result
);
'she'
=>
'wtf'
,
),
),
);
}
/**
* @dataProvider patterns
*/
public
function
testNamedArgumentsStatic
(
$pattern
,
$expected
,
$args
)
{
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
$args
);
$this
->
assertEquals
(
$expected
,
$result
);
}
/**
* @dataProvider patterns
*/
public
function
testNamedArgumentsObject
(
$pattern
,
$expected
,
$args
)
{
$formatter
=
new
MessageFormatter
(
'en_US'
,
$pattern
);
$result
=
$formatter
->
format
(
$args
);
$this
->
assertEquals
(
$expected
,
$result
);
}
}
public
function
testInsufficientArguments
()
public
function
testInsufficientArguments
()
...
...
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