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
fa9a975d
Commit
fa9a975d
authored
Oct 16, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
intl message parser now handles too many or too less args
parent
b8099cbf
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
4 deletions
+40
-4
MessageFormatter.php
framework/yii/i18n/MessageFormatter.php
+8
-4
MessageFormatterTest.php
tests/unit/framework/i18n/MessageFormatterTest.php
+32
-0
No files found.
framework/yii/i18n/MessageFormatter.php
View file @
fa9a975d
...
@@ -65,7 +65,8 @@ class MessageFormatter extends \MessageFormatter
...
@@ -65,7 +65,8 @@ class MessageFormatter extends \MessageFormatter
{
{
$map
=
array_flip
(
array_keys
(
$args
));
$map
=
array_flip
(
array_keys
(
$args
));
// parsing http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
// parsing pattern base on ICU grammar:
// http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
$parts
=
explode
(
'{'
,
$pattern
);
$parts
=
explode
(
'{'
,
$pattern
);
$c
=
count
(
$parts
);
$c
=
count
(
$parts
);
$pattern
=
$parts
[
0
];
$pattern
=
$parts
[
0
];
...
@@ -73,18 +74,20 @@ class MessageFormatter extends \MessageFormatter
...
@@ -73,18 +74,20 @@ class MessageFormatter extends \MessageFormatter
$stack
=
array
();
$stack
=
array
();
for
(
$i
=
1
;
$i
<
$c
;
$i
++
)
{
for
(
$i
=
1
;
$i
<
$c
;
$i
++
)
{
if
(
preg_match
(
'~^\A(\s*)([\d\w]+)(\s*)([},])(\s*)(.*)\z$~u'
,
$parts
[
$i
],
$matches
))
{
if
(
preg_match
(
'~^\A(\s*)([\d\w]+)(\s*)([},])(\s*)(.*)\z$~u'
,
$parts
[
$i
],
$matches
))
{
// if we are not inside a plural or select this is a message
if
(
!
isset
(
$stack
[
$d
])
||
$stack
[
$d
]
!=
'plural'
&&
$stack
[
$d
]
!=
'select'
)
{
$d
++
;
$d
++
;
// replace normal arg if it was set
// replace normal arg if it is available
if
(
isset
(
$map
[
$matches
[
2
]]))
{
if
(
isset
(
$map
[
$matches
[
2
]]))
{
$q
=
''
;
$q
=
''
;
$pattern
.=
'{'
.
$matches
[
1
]
.
$map
[
$matches
[
2
]]
.
$matches
[
3
];
$pattern
.=
'{'
.
$matches
[
1
]
.
$map
[
$matches
[
2
]]
.
$matches
[
3
];
}
else
{
}
else
{
// quote unused args
// quote unused args
$q
=
''
;
//($matches[4] == '}' && isset($stack[$d]) && !($stack[$d] == 'plural' || $stack[$d] == 'select')
) ? "'" : "";
$q
=
(
$matches
[
4
]
==
'}'
)
?
"'"
:
""
;
$pattern
.=
"
$q
{"
.
$matches
[
1
]
.
$matches
[
2
]
.
$matches
[
3
];
$pattern
.=
"
$q
{"
.
$matches
[
1
]
.
$matches
[
2
]
.
$matches
[
3
];
}
}
$pattern
.=
(
$term
=
$matches
[
4
]
.
$q
.
$matches
[
5
]
.
$matches
[
6
]);
$pattern
.=
(
$term
=
$matches
[
4
]
.
$q
.
$matches
[
5
]
.
$matches
[
6
]);
// check type
current level
// store type of
current level
$stack
[
$d
]
=
(
$matches
[
4
]
==
','
)
?
substr
(
$matches
[
6
],
0
,
6
)
:
'none'
;
$stack
[
$d
]
=
(
$matches
[
4
]
==
','
)
?
substr
(
$matches
[
6
],
0
,
6
)
:
'none'
;
// if it's plural or select, the next bracket is NOT begin of a message then!
// if it's plural or select, the next bracket is NOT begin of a message then!
if
(
$stack
[
$d
]
==
'plural'
||
$stack
[
$d
]
==
'select'
)
{
if
(
$stack
[
$d
]
==
'plural'
||
$stack
[
$d
]
==
'select'
)
{
...
@@ -95,6 +98,7 @@ class MessageFormatter extends \MessageFormatter
...
@@ -95,6 +98,7 @@ class MessageFormatter extends \MessageFormatter
continue
;
continue
;
}
}
}
}
}
$pattern
.=
'{'
.
$parts
[
$i
];
$pattern
.=
'{'
.
$parts
[
$i
];
$d
+=
1
-
substr_count
(
$parts
[
$i
],
'}'
);
$d
+=
1
-
substr_count
(
$parts
[
$i
],
'}'
);
}
}
...
...
tests/unit/framework/i18n/MessageFormatterTest.php
View file @
fa9a975d
...
@@ -65,6 +65,38 @@ _MSG_;
...
@@ -65,6 +65,38 @@ _MSG_;
'gender'
=>
'male'
,
'gender'
=>
'male'
,
));
));
$this
->
assertEquals
(
'Alexander is male and he loves Yii!'
,
$result
);
$this
->
assertEquals
(
'Alexander is male and he loves Yii!'
,
$result
);
// verify pattern in select does not get replaced
$pattern
=
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
;
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'name'
=>
'Alexander'
,
'gender'
=>
'male'
,
// following should not be replaced
'he'
=>
'wtf'
,
'she'
=>
'wtf'
,
'it'
=>
'wtf'
,
));
$this
->
assertEquals
(
'Alexander is male and he loves Yii!'
,
$result
);
// verify pattern in select message gets replaced
$pattern
=
'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'
;
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'name'
=>
'Alexander'
,
'gender'
=>
'male'
,
'he'
=>
'wtf'
,
'she'
=>
'wtf'
,
));
$this
->
assertEquals
(
'Alexander is male and wtf loves Yii!'
,
$result
);
// some parser specific verifications
$pattern
=
'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!'
;
$result
=
MessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
array
(
'nr'
=>
42
,
'gender'
=>
'male'
,
'he'
=>
'wtf'
,
'she'
=>
'wtf'
,
));
$this
->
assertEquals
(
'male and wtf loves 42 is male!'
,
$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