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
c229ba72
Commit
c229ba72
authored
Feb 23, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed a bug in MessageSource and missingtranslation event
fixes #2519: MessageSource removed translation messages when event handler was bound to `missingTranslation`-event
parent
3a6b9341
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
9 deletions
+47
-9
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
MessageSource.php
framework/i18n/MessageSource.php
+10
-7
MissingTranslationEvent.php
framework/i18n/MissingTranslationEvent.php
+8
-2
I18NTest.php
tests/unit/framework/i18n/I18NTest.php
+28
-0
No files found.
framework/CHANGELOG.md
View file @
c229ba72
...
...
@@ -47,6 +47,7 @@ Yii Framework 2 Change Log
-
Bug #2324: Fixed QueryBuilder bug when building a query with "query" option (mintao)
-
Bug #2399: Fixed the bug that AssetBundle did not handle relative URLs correctly (qiangxue)
-
Bug #2502: Unclear error message when
`$_SERVER['DOCUMENT_ROOT']`
is empty (samdark)
-
Bug #2519: MessageSource removed translation messages when event handler was bound to
`missingTranslation`
-event (cebe)
-
Bug: Fixed
`Call to a member function registerAssetFiles() on a non-object`
in case of wrong
`sourcePath`
for an asset bundle (samdark)
-
Bug: Fixed incorrect event name for
`yii\jui\Spinner`
(samdark)
-
Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe)
...
...
framework/i18n/MessageSource.php
View file @
c229ba72
...
...
@@ -91,12 +91,13 @@ class MessageSource extends Component
/**
* Translates the specified message.
* If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered
* and the original message will be returned.
* @param string $category the category that the message belongs to
* @param string $message the message to be translated
* @param string $language the target language
* @return string|boolean the translated message or false if translation wasn't found
* If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
* If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
* If no fallback translation is provided this method will return `false`.
* @param string $category the category that the message belongs to.
* @param string $message the message to be translated.
* @param string $language the target language.
* @return string|boolean the translated message or false if translation wasn't found.
*/
protected
function
translateMessage
(
$category
,
$message
,
$language
)
{
...
...
@@ -113,7 +114,9 @@ class MessageSource extends Component
'language'
=>
$language
,
]);
$this
->
trigger
(
self
::
EVENT_MISSING_TRANSLATION
,
$event
);
$this
->
_messages
[
$key
]
=
$event
->
message
;
if
(
$event
->
translatedMessage
!==
false
)
{
return
$this
->
_messages
[
$key
][
$message
]
=
$event
->
translatedMessage
;
}
}
return
false
;
}
...
...
framework/i18n/MissingTranslationEvent.php
View file @
c229ba72
...
...
@@ -18,11 +18,17 @@ use yii\base\Event;
class
MissingTranslationEvent
extends
Event
{
/**
* @var string the message to be translated. An event handler may
overwrite this property
*
with a translated version
if possible.
* @var string the message to be translated. An event handler may
use this to provide a fallback translation
*
and set [[translatedMessage]]
if possible.
*/
public
$message
;
/**
* @var string|boolean the translated message. An event handler may overwrite this property
* with a translated version of [[message]] if possible. Defaults to false meaning no translation
* is available.
*/
public
$translatedMessage
=
false
;
/**
* @var string the category that the message belongs to
*/
public
$category
;
...
...
tests/unit/framework/i18n/I18NTest.php
View file @
c229ba72
...
...
@@ -7,6 +7,7 @@
namespace
yiiunit\framework\i18n
;
use
yii\base\Event
;
use
yii\base\Model
;
use
yii\i18n\I18N
;
use
yii\i18n\PhpMessageSource
;
...
...
@@ -98,6 +99,33 @@ class I18NTest extends TestCase
{
$this
->
assertEquals
(
'1 item'
,
$this
->
i18n
->
translate
(
'test'
,
'{0, number} {0, plural, one{item} other{items}}'
,
1
,
'hu'
));
}
/**
* https://github.com/yiisoft/yii2/issues/2519
*/
public
function
testMissingTranslationEvent
()
{
$this
->
assertEquals
(
'Hallo Welt!'
,
$this
->
i18n
->
translate
(
'test'
,
'Hello world!'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'Missing translation message.'
,
$this
->
i18n
->
translate
(
'test'
,
'Missing translation message.'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'Hallo Welt!'
,
$this
->
i18n
->
translate
(
'test'
,
'Hello world!'
,
[],
'de-DE'
));
Event
::
on
(
PhpMessageSource
::
className
(),
PhpMessageSource
::
EVENT_MISSING_TRANSLATION
,
function
(
$event
)
{});
$this
->
assertEquals
(
'Hallo Welt!'
,
$this
->
i18n
->
translate
(
'test'
,
'Hello world!'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'Missing translation message.'
,
$this
->
i18n
->
translate
(
'test'
,
'Missing translation message.'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'Hallo Welt!'
,
$this
->
i18n
->
translate
(
'test'
,
'Hello world!'
,
[],
'de-DE'
));
Event
::
off
(
PhpMessageSource
::
className
(),
PhpMessageSource
::
EVENT_MISSING_TRANSLATION
);
Event
::
on
(
PhpMessageSource
::
className
(),
PhpMessageSource
::
EVENT_MISSING_TRANSLATION
,
function
(
$event
)
{
if
(
$event
->
message
==
'Missing translation message.'
)
{
$event
->
translatedMessage
=
'TRANSLATION MISSING HERE!'
;
}
});
$this
->
assertEquals
(
'Hallo Welt!'
,
$this
->
i18n
->
translate
(
'test'
,
'Hello world!'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'Another missing translation message.'
,
$this
->
i18n
->
translate
(
'test'
,
'Another missing translation message.'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'TRANSLATION MISSING HERE!'
,
$this
->
i18n
->
translate
(
'test'
,
'Missing translation message.'
,
[],
'de-DE'
));
$this
->
assertEquals
(
'Hallo Welt!'
,
$this
->
i18n
->
translate
(
'test'
,
'Hello world!'
,
[],
'de-DE'
));
Event
::
off
(
PhpMessageSource
::
className
(),
PhpMessageSource
::
EVENT_MISSING_TRANSLATION
);
}
}
class
ParamModel
extends
Model
...
...
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