Skip to content

Commit

Permalink
add ability to use behavior owner as fallback message source
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jun 20, 2023
1 parent 5222493 commit 72a9f68
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<br>
</p>

This extension Provides support for Yii1 translation fallback to another message source.
This extension provides support for Yii1 translation fallback to another message source.

For license information check the [LICENSE](LICENSE.md)-file.

Expand Down
25 changes: 16 additions & 9 deletions src/MessageSourceFallbackBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
18 changes: 18 additions & 0 deletions tests/MessageSourceFallbackBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ public function testFallbackToDifferentLanguage(): void
$this->assertSame('title-main-es', Yii::t('content', 'title'));
$this->assertSame('description-main-en_us', Yii::t('content', 'description'));
}

public function testFallbackToDifferentLanguageWithinSameMessageSource(): void
{
$this->attachMessageSourceBehavior([
'class' => MessageSourceFallbackBehavior::class,
'fallbackLanguage' => 'en_us',
]);

/** @var \CMessageSource|MessageSourceFallbackBehavior $messageSource */
$messageSource = Yii::app()->getComponent('messages');

$this->assertSame($messageSource, $messageSource->getFallbackMessageSource());

Yii::app()->setLanguage('es');

$this->assertSame('title-main-es', Yii::t('content', 'title'));
$this->assertSame('unexisting', Yii::t('content', 'unexisting'));
}
}

0 comments on commit 72a9f68

Please sign in to comment.