-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to use behavior owner as fallback message source
- Loading branch information
1 parent
5222493
commit 72a9f68
Showing
3 changed files
with
35 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,18 +34,19 @@ | |
* ]; | ||
* ``` | ||
* | ||
* @property \CMessageSource $owner | ||
* @property \CMessageSource|array $fallbackMessageSource fallback message source. | ||
* @property \CMessageSource $owner the owner message source that this behavior is attached to. | ||
* @property \CMessageSource|array|null $fallbackMessageSource fallback message source. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class MessageSourceFallbackBehavior extends CBehavior | ||
{ | ||
/** | ||
* @var \CMessageSource|array fallback message source or its array configuration. | ||
* @var \CMessageSource|array|null fallback message source or its array configuration. | ||
* If `null` - owner instance will be used. | ||
*/ | ||
private $_fallbackMessageSource = []; | ||
private $_fallbackMessageSource = null; | ||
|
||
/** | ||
* @var string|null language which should be used to retrieve missing language source. | ||
|
@@ -56,7 +57,7 @@ class MessageSourceFallbackBehavior extends CBehavior | |
/** | ||
* Sets the message source for the translation fallback. | ||
* | ||
* @param \CMessageSource|array $fallbackMessageSource message source instance or its array configuration. | ||
* @param \CMessageSource|array|null $fallbackMessageSource message source instance or its array configuration, `null` means owner instance usage. | ||
* @return static self reference. | ||
*/ | ||
public function setFallbackMessageSource($fallbackMessageSource): self | ||
|
@@ -73,6 +74,10 @@ public function setFallbackMessageSource($fallbackMessageSource): self | |
*/ | ||
public function getFallbackMessageSource() | ||
{ | ||
if ($this->_fallbackMessageSource === null) { | ||
return $this->owner; | ||
} | ||
|
||
if (!is_object($this->_fallbackMessageSource)) { | ||
$this->_fallbackMessageSource = $this->createFallbackMessageSource($this->_fallbackMessageSource); | ||
} | ||
|
@@ -112,11 +117,13 @@ public function events() | |
*/ | ||
public function missingTranslationHandler($event): void | ||
{ | ||
$language = $this->fallbackLanguage; | ||
if ($language === null) { | ||
$language = $event->language; | ||
$language = $this->fallbackLanguage ?? $event->language; | ||
|
||
$fallbackMessageSource = $this->getFallbackMessageSource(); | ||
if ($fallbackMessageSource === $event->sender && $language === $event->language) { | ||
return; // avoid infinite recursion | ||
} | ||
|
||
$event->message = $this->getFallbackMessageSource()->translate($event->category, $event->message, $language); | ||
$event->message = $fallbackMessageSource->translate($event->category, $event->message, $language); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters