Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
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
夏彬
yii2bootstrap
Commits
5b9b9727
Commit
5b9b9727
authored
Oct 23, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implemented plural and select for fallback intl message formatter
parent
553ac1f1
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
468 additions
and
3 deletions
+468
-3
FallbackMessageFormatter.php
framework/yii/i18n/FallbackMessageFormatter.php
+306
-0
MessageFormatter.php
framework/yii/i18n/MessageFormatter.php
+7
-3
FallbackMessageFormatterTest.php
tests/unit/framework/i18n/FallbackMessageFormatterTest.php
+155
-0
No files found.
framework/yii/i18n/
Base
MessageFormatter.php
→
framework/yii/i18n/
Fallback
MessageFormatter.php
View file @
5b9b9727
This diff is collapsed.
Click to expand it.
framework/yii/i18n/MessageFormatter.php
View file @
5b9b9727
...
@@ -8,8 +8,9 @@
...
@@ -8,8 +8,9 @@
namespace
yii\i18n
;
namespace
yii\i18n
;
if
(
!
class_exists
(
'MessageFormatter'
,
false
))
{
if
(
!
class_exists
(
'MessageFormatter'
,
false
))
{
require_once
(
__DIR__
.
'/
Base
MessageFormatter.php'
);
require_once
(
__DIR__
.
'/
Fallback
MessageFormatter.php'
);
}
}
defined
(
'YII_INTL_MESSAGE_FALLBACK'
)
||
define
(
'YII_INTL_MESSAGE_FALLBACK'
,
false
);
/**
/**
* MessageFormatter is an enhanced version of PHP intl class that no matter which PHP and ICU versions are used:
* MessageFormatter is an enhanced version of PHP intl class that no matter which PHP and ICU versions are used:
...
@@ -18,6 +19,9 @@ if (!class_exists('MessageFormatter', false)) {
...
@@ -18,6 +19,9 @@ if (!class_exists('MessageFormatter', false)) {
* - Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
* - Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
* substituted.
* substituted.
* - Fixes PHP 5.5 weird placeholder replacement in case no arguments are provided at all (https://bugs.php.net/bug.php?id=65920).
* - Fixes PHP 5.5 weird placeholder replacement in case no arguments are provided at all (https://bugs.php.net/bug.php?id=65920).
* - Offers limited support for message formatting in case PHP intl extension is not installed.
* However it is highly recommended that you install [PHP intl extension](http://php.net/manual/en/book.intl.php) if you want
* to use MessageFormatter features.
*
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @author Alexander Makarov <sam@rmcreative.ru>
* @author Carsten Brandt <mail@cebe.cc>
* @author Carsten Brandt <mail@cebe.cc>
...
@@ -38,7 +42,7 @@ class MessageFormatter extends \MessageFormatter
...
@@ -38,7 +42,7 @@ class MessageFormatter extends \MessageFormatter
return
$this
->
getPattern
();
return
$this
->
getPattern
();
}
}
if
(
version_compare
(
PHP_VERSION
,
'5.5.0'
,
'<'
))
{
if
(
version_compare
(
PHP_VERSION
,
'5.5.0'
,
'<'
)
&&
!
YII_INTL_MESSAGE_FALLBACK
)
{
$pattern
=
self
::
replaceNamedArguments
(
$this
->
getPattern
(),
$args
);
$pattern
=
self
::
replaceNamedArguments
(
$this
->
getPattern
(),
$args
);
$this
->
setPattern
(
$pattern
);
$this
->
setPattern
(
$pattern
);
$args
=
array_values
(
$args
);
$args
=
array_values
(
$args
);
...
@@ -61,7 +65,7 @@ class MessageFormatter extends \MessageFormatter
...
@@ -61,7 +65,7 @@ class MessageFormatter extends \MessageFormatter
return
$pattern
;
return
$pattern
;
}
}
if
(
version_compare
(
PHP_VERSION
,
'5.5.0'
,
'<'
))
{
if
(
version_compare
(
PHP_VERSION
,
'5.5.0'
,
'<'
)
&&
!
YII_INTL_MESSAGE_FALLBACK
)
{
$pattern
=
self
::
replaceNamedArguments
(
$pattern
,
$args
);
$pattern
=
self
::
replaceNamedArguments
(
$pattern
,
$args
);
$args
=
array_values
(
$args
);
$args
=
array_values
(
$args
);
}
}
...
...
tests/unit/framework/i18n/FallbackMessageFormatterTest.php
0 → 100644
View file @
5b9b9727
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\i18n
;
use
yii\i18n\FallbackMessageFormatter
;
use
yiiunit\TestCase
;
/**
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
* @group i18n
*/
class
FallbackMessageFormatterTest
extends
TestCase
{
const
N
=
'n'
;
const
N_VALUE
=
42
;
const
SUBJECT
=
'сабж'
;
const
SUBJECT_VALUE
=
'Answer to the Ultimate Question of Life, the Universe, and Everything'
;
public
function
patterns
()
{
return
[
[
'{'
.
self
::
SUBJECT
.
'} is {'
.
self
::
N
.
'}'
,
// pattern
self
::
SUBJECT_VALUE
.
' is '
.
self
::
N_VALUE
,
// expected
[
// params
self
::
N
=>
self
::
N_VALUE
,
self
::
SUBJECT
=>
self
::
SUBJECT_VALUE
,
]
],
// This one was provided by Aura.Intl. Thanks!
[
<<<_MSG_
{gender_of_host, select,
female {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to her party.}
=2 {{host} invites {guest} and one other person to her party.}
other {{host} invites {guest} and # other people to her party.}}}
male {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to his party.}
=2 {{host} invites {guest} and one other person to his party.}
other {{host} invites {guest} and # other people to his party.}}}
other {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} and # other people to their party.}}}}
_MSG_
,
'ralph invites beep and 3 other people to his party.'
,
[
'gender_of_host'
=>
'male'
,
'num_guests'
=>
4
,
'host'
=>
'ralph'
,
'guest'
=>
'beep'
]
],
[
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
,
'Alexander is male and he loves Yii!'
,
[
'name'
=>
'Alexander'
,
'gender'
=>
'male'
,
],
],
// verify pattern in select does not get replaced
[
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'
,
'Alexander is male and he loves Yii!'
,
[
'name'
=>
'Alexander'
,
'gender'
=>
'male'
,
// following should not be replaced
'he'
=>
'wtf'
,
'she'
=>
'wtf'
,
'it'
=>
'wtf'
,
]
],
// verify pattern in select message gets replaced
[
'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'
,
'Alexander is male and wtf loves Yii!'
,
[
'name'
=>
'Alexander'
,
'gender'
=>
'male'
,
'he'
=>
'wtf'
,
'she'
=>
'wtf'
,
],
],
// some parser specific verifications
[
'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr} is {gender}!'
,
'male and wtf loves 42 is male!'
,
[
'nr'
=>
42
,
'gender'
=>
'male'
,
'he'
=>
'wtf'
,
'she'
=>
'wtf'
,
],
],
];
}
/**
* @dataProvider patterns
*/
public
function
testNamedArgumentsStatic
(
$pattern
,
$expected
,
$args
)
{
$result
=
FallbackMessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
$args
);
$this
->
assertEquals
(
$expected
,
$result
);
}
/**
* @dataProvider patterns
*/
public
function
testNamedArgumentsObject
(
$pattern
,
$expected
,
$args
)
{
$formatter
=
new
FallbackMessageFormatter
(
'en_US'
,
$pattern
);
$result
=
$formatter
->
format
(
$args
);
$this
->
assertEquals
(
$expected
,
$result
,
$formatter
->
getErrorMessage
());
}
public
function
testInsufficientArguments
()
{
$expected
=
'{'
.
self
::
SUBJECT
.
'} is '
.
self
::
N_VALUE
;
$result
=
FallbackMessageFormatter
::
formatMessage
(
'en_US'
,
'{'
.
self
::
SUBJECT
.
'} is {'
.
self
::
N
.
'}'
,
[
self
::
N
=>
self
::
N_VALUE
,
]);
$this
->
assertEquals
(
$expected
,
$result
);
}
public
function
testNoParams
()
{
$pattern
=
'{'
.
self
::
SUBJECT
.
'} is '
.
self
::
N
;
$result
=
FallbackMessageFormatter
::
formatMessage
(
'en_US'
,
$pattern
,
[]);
$this
->
assertEquals
(
$pattern
,
$result
);
$formatter
=
new
FallbackMessageFormatter
(
'en_US'
,
$pattern
);
$result
=
$formatter
->
format
([]);
$this
->
assertEquals
(
$pattern
,
$result
,
$formatter
->
getErrorMessage
());
}
}
\ No newline at end of file
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