Skip to content

Commit

Permalink
Add translator aware & ReturnKeyTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Nov 10, 2023
1 parent fafbce2 commit 78b4cc9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": ">=7.1.0",
"php": ">=7.4",
"packaged/helpers": "~1.0||^2.0"
},
"require-dev": {
Expand Down
14 changes: 14 additions & 0 deletions src/TranslatorAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Packaged\I18n;

use Packaged\I18n\Translators\Translator;

interface TranslatorAware
{
public function setTranslator(Translator $translator): self;

public function getTranslator(): Translator;

public function hasTranslator(): bool;
}
27 changes: 27 additions & 0 deletions src/TranslatorAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Packaged\I18n;

use Packaged\I18n\Translators\ReplacementsOnlyTranslator;
use Packaged\I18n\Translators\Translator;

trait TranslatorAwareTrait
{
private ?Translator $_translator;

public function setTranslator(Translator $translator): self
{
$this->_translator = $translator;
return $this;
}

public function getTranslator(): Translator
{
return $this->_translator ?? new ReplacementsOnlyTranslator();
}

public function hasTranslator(): bool
{
return $this->_translator !== null;
}
}
11 changes: 11 additions & 0 deletions src/Translators/ReturnKeyTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Packaged\I18n\Translators;

class ReturnKeyTranslator implements Translator
{
public function _($msgId, $default, array $replacements = null, $choice = null)
{
return $msgId;
}
}

0 comments on commit 78b4cc9

Please sign in to comment.