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
b8099cbf
Commit
b8099cbf
authored
Oct 16, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a parser that is able to deal with the right number of args
parent
6fc5f0a4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
8 deletions
+33
-8
MessageFormatter.php
framework/yii/i18n/MessageFormatter.php
+33
-8
No files found.
framework/yii/i18n/MessageFormatter.php
View file @
b8099cbf
...
@@ -64,16 +64,41 @@ class MessageFormatter extends \MessageFormatter
...
@@ -64,16 +64,41 @@ class MessageFormatter extends \MessageFormatter
private
static
function
replaceNamedArguments
(
$pattern
,
$args
)
private
static
function
replaceNamedArguments
(
$pattern
,
$args
)
{
{
$map
=
array_flip
(
array_keys
(
$args
));
$map
=
array_flip
(
array_keys
(
$args
));
return
preg_replace_callback
(
'~({\s*)([\d\w]+)(\s*[,}])~u'
,
function
(
$input
)
use
(
$map
)
{
$name
=
$input
[
2
];
// parsing http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
if
(
isset
(
$map
[
$name
]))
{
$parts
=
explode
(
'{'
,
$pattern
);
return
$input
[
1
]
.
$map
[
$name
]
.
$input
[
3
];
$c
=
count
(
$parts
);
$pattern
=
$parts
[
0
];
$d
=
0
;
$stack
=
array
();
for
(
$i
=
1
;
$i
<
$c
;
$i
++
)
{
if
(
preg_match
(
'~^\A(\s*)([\d\w]+)(\s*)([},])(\s*)(.*)\z$~u'
,
$parts
[
$i
],
$matches
))
{
$d
++
;
// replace normal arg if it was set
if
(
isset
(
$map
[
$matches
[
2
]]))
{
$q
=
''
;
$pattern
.=
'{'
.
$matches
[
1
]
.
$map
[
$matches
[
2
]]
.
$matches
[
3
];
}
else
{
// quote unused args
$q
=
''
;
//($matches[4] == '}' && isset($stack[$d]) && !($stack[$d] == 'plural' || $stack[$d] == 'select')) ? "'" : "";
$pattern
.=
"
$q
{"
.
$matches
[
1
]
.
$matches
[
2
]
.
$matches
[
3
];
}
$pattern
.=
(
$term
=
$matches
[
4
]
.
$q
.
$matches
[
5
]
.
$matches
[
6
]);
// check type current level
$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
(
$stack
[
$d
]
==
'plural'
||
$stack
[
$d
]
==
'select'
)
{
$i
++
;
$d
-=
substr_count
(
$term
,
'}'
);
}
else
{
$d
-=
substr_count
(
$term
,
'}'
);
continue
;
}
}
}
else
{
$pattern
.=
'{'
.
$parts
[
$i
];
//return $input[1] . $name . $input[3];
$d
+=
1
-
substr_count
(
$parts
[
$i
],
'}'
);
return
"'"
.
$input
[
1
]
.
$name
.
$input
[
3
]
.
"'"
;
}
}
},
$pattern
)
;
return
$pattern
;
}
}
/**
/**
...
...
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