| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\I18n; |
| 4 |
|
| 5 |
/** |
| 6 |
* Provide translation helpers backed by {@see StaticTranslator::$instance} |
| 7 |
*/ |
| 8 |
trait Translation |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The domain to use in methods {@see Translation::translate()} and {@see Translation::translatePlural()} |
| 12 |
* |
| 13 |
* Set this to your desired domain and use both mentioned methods as usual, if you never require the |
| 14 |
* default translation domain. (It's still being used as a fallback if your domain doesn't provide a |
| 15 |
* particular message.) |
| 16 |
* |
| 17 |
* @var ?string |
| 18 |
*/ |
| 19 |
protected ?string $translationDomain = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Translate a message |
| 23 |
* |
| 24 |
* @param string $message |
| 25 |
* @param ?string $context Message context |
| 26 |
* |
| 27 |
* @return string Translated message or original message if no translation is found |
| 28 |
*/ |
| 29 |
public function translate(string $message, ?string $context = null): string |
| 30 |
{ |
| 31 |
return $this->translationDomain === null |
| 32 |
? StaticTranslator::$instance->translate($message, $context) |
| 33 |
: StaticTranslator::$instance->translateInDomain($this->translationDomain, $message, $context); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Translate a message in the given domain |
| 38 |
* |
| 39 |
* If no translation is found in the specified domain, the translation is also searched for in the default domain. |
| 40 |
* |
| 41 |
* @param string $domain |
| 42 |
* @param string $message |
| 43 |
* @param ?string $context Message context |
| 44 |
* |
| 45 |
* @return string Translated message or original message if no translation is found |
| 46 |
*/ |
| 47 |
public function translateInDomain(string $domain, string $message, ?string $context = null): string |
| 48 |
{ |
| 49 |
return StaticTranslator::$instance->translateInDomain($domain, $message, $context); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Translate a plural message |
| 54 |
* |
| 55 |
* The returned message is based on the given number to decide between the singular and plural forms. |
| 56 |
* That is also the case if no translation is found. |
| 57 |
* |
| 58 |
* @param string $singular Singular message |
| 59 |
* @param string $plural Plural message |
| 60 |
* @param ?int $number Number to decide between the returned singular and plural forms |
| 61 |
* @param ?string $context Message context |
| 62 |
* |
| 63 |
* @return string Translated message or original message if no translation is found |
| 64 |
*/ |
| 65 |
public function translatePlural(string $singular, string $plural, ?int $number, ?string $context = null): string |
| 66 |
{ |
| 67 |
return $this->translationDomain === null |
| 68 |
? StaticTranslator::$instance->translatePlural($singular, $plural, $number ?? 0, $context) |
| 69 |
: StaticTranslator::$instance->translatePluralInDomain( |
| 70 |
$this->translationDomain, |
| 71 |
$singular, |
| 72 |
$plural, |
| 73 |
$number ?? 0, |
| 74 |
$context |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Translate a plural message in the given domain |
| 80 |
* |
| 81 |
* If no translation is found in the specified domain, the translation is also searched for in the default domain. |
| 82 |
* |
| 83 |
* The returned message is based on the given number to decide between the singular and plural forms. |
| 84 |
* That is also the case if no translation is found. |
| 85 |
* |
| 86 |
* @param string $domain |
| 87 |
* @param string $singular Singular message |
| 88 |
* @param string $plural Plural message |
| 89 |
* @param ?int $number Number to decide between the returned singular and plural forms |
| 90 |
* @param ?string $context Message context |
| 91 |
* |
| 92 |
* @return string Translated message or original message if no translation is found |
| 93 |
*/ |
| 94 |
public function translatePluralInDomain( |
| 95 |
string $domain, |
| 96 |
string $singular, |
| 97 |
string $plural, |
| 98 |
?int $number, |
| 99 |
?string $context = null |
| 100 |
): string { |
| 101 |
return StaticTranslator::$instance->translatePluralInDomain( |
| 102 |
$domain, |
| 103 |
$singular, |
| 104 |
$plural, |
| 105 |
$number ?? 0, |
| 106 |
$context |
| 107 |
); |
| 108 |
} |
| 109 |
} |