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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
9ca98d62
Commit
9ca98d62
authored
Oct 23, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring I18N.
parent
9f7ca5d2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
32 deletions
+8
-32
I18N.php
framework/yii/i18n/I18N.php
+5
-31
MessageFormatter.php
framework/yii/i18n/MessageFormatter.php
+3
-1
No files found.
framework/yii/i18n/I18N.php
View file @
9ca98d62
...
...
@@ -45,14 +45,14 @@ class I18N extends Component
parent
::
init
();
if
(
!
isset
(
$this
->
translations
[
'yii'
]))
{
$this
->
translations
[
'yii'
]
=
[
'class'
=>
'yii\i18n\PhpMessageSource'
,
'class'
=>
PhpMessageSource
::
className
()
,
'sourceLanguage'
=>
'en_US'
,
'basePath'
=>
'@yii/messages'
,
];
}
if
(
!
isset
(
$this
->
translations
[
'app'
]))
{
$this
->
translations
[
'app'
]
=
[
'class'
=>
'yii\i18n\PhpMessageSource'
,
'class'
=>
PhpMessageSource
::
className
()
,
'sourceLanguage'
=>
'en_US'
,
'basePath'
=>
'@app/messages'
,
];
...
...
@@ -74,33 +74,7 @@ class I18N extends Component
public
function
translate
(
$category
,
$message
,
$params
,
$language
)
{
$message
=
$this
->
getMessageSource
(
$category
)
->
translate
(
$category
,
$message
,
$language
);
$params
=
(
array
)
$params
;
if
(
$params
===
[])
{
return
$message
;
}
if
(
preg_match
(
'~{\s*[\d\w]+\s*,~u'
,
$message
))
{
$formatter
=
new
MessageFormatter
(
$language
,
$message
);
if
(
$formatter
===
null
)
{
Yii
::
warning
(
"
$language
message from category
$category
is invalid. Message is:
$message
."
);
return
$message
;
}
$result
=
$formatter
->
format
(
$params
);
if
(
$result
===
false
)
{
$errorMessage
=
$formatter
->
getErrorMessage
();
Yii
::
warning
(
"
$language
message from category
$category
failed with error:
$errorMessage
. Message is:
$message
."
);
return
$message
;
}
else
{
return
$result
;
}
}
$p
=
[];
foreach
(
$params
as
$name
=>
$value
)
{
$p
[
'{'
.
$name
.
'}'
]
=
$value
;
}
return
strtr
(
$message
,
$p
);
return
$this
->
format
(
$message
,
$params
,
$language
);
}
/**
...
...
@@ -121,13 +95,13 @@ class I18N extends Component
if
(
preg_match
(
'~{\s*[\d\w]+\s*,~u'
,
$message
))
{
$formatter
=
new
MessageFormatter
(
$language
,
$message
);
if
(
$formatter
===
null
)
{
Yii
::
warning
(
"
Message for
$language
is invalid
:
$message
."
);
Yii
::
warning
(
"
Unable to format message in language '
$language
'
:
$message
."
);
return
$message
;
}
$result
=
$formatter
->
format
(
$params
);
if
(
$result
===
false
)
{
$errorMessage
=
$formatter
->
getErrorMessage
();
Yii
::
warning
(
"Formatting message for
$language
failed with error:
$errorMessage
. Message i
s:
$message
."
);
Yii
::
warning
(
"Formatting message for
language '
$language
' failed with error:
$errorMessage
. The message being formatted wa
s:
$message
."
);
return
$message
;
}
else
{
return
$result
;
...
...
framework/yii/i18n/MessageFormatter.php
View file @
9ca98d62
...
...
@@ -13,7 +13,9 @@ if (!class_exists('MessageFormatter', false)) {
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 enhances the message formatter class provided by PHP intl extension.
*
* The following enhancements are provided:
*
* - Accepts named arguments and mixed numeric and named arguments.
* - Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
...
...
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