diff --git a/src/GettextTranslator.php b/src/GettextTranslator.php index a7a8144..3a8f2ca 100644 --- a/src/GettextTranslator.php +++ b/src/GettextTranslator.php @@ -219,7 +219,7 @@ public function encodeMessageWithContext($message, $context) return "{$context}\x04{$message}"; } - public function translate($message, $context = null) + public function translate(string $message, ?string $context = null): string { if ($context !== null) { $messageForGettext = $this->encodeMessageWithContext($message, $context); @@ -236,7 +236,7 @@ public function translate($message, $context = null) return $translation; } - public function translateInDomain($domain, $message, $context = null) + public function translateInDomain(string $domain, string $message, ?string $context = null): string { if ($context !== null) { $messageForGettext = $this->encodeMessageWithContext($message, $context); @@ -263,7 +263,7 @@ public function translateInDomain($domain, $message, $context = null) return $translation; } - public function translatePlural($singular, $plural, $number, $context = null) + public function translatePlural(string $singular, string $plural, int $number, ?string $context = null): string { if ($context !== null) { $singularForGettext = $this->encodeMessageWithContext($singular, $context); @@ -285,8 +285,13 @@ public function translatePlural($singular, $plural, $number, $context = null) return $translation; } - public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null) - { + public function translatePluralInDomain( + string $domain, + string $singular, + string $plural, + int $number, + ?string $context = null + ): string { if ($context !== null) { $singularForGettext = $this->encodeMessageWithContext($singular, $context); } else { diff --git a/src/Locale.php b/src/Locale.php index d9e6b05..51958b0 100644 --- a/src/Locale.php +++ b/src/Locale.php @@ -12,7 +12,7 @@ class Locale * * @return string */ - public function getDefaultLocale() + public function getDefaultLocale(): string { return $this->defaultLocale; } @@ -24,7 +24,7 @@ public function getDefaultLocale() * * @return $this */ - public function setDefaultLocale($defaultLocale) + public function setDefaultLocale(string $defaultLocale): self { $this->defaultLocale = $defaultLocale; @@ -39,7 +39,7 @@ public function setDefaultLocale($defaultLocale) * * @return string The browser's preferred locale code */ - public function getPreferred($header, array $available) + public function getPreferred(string $header, array $available): string { $headerValues = explode(',', $header); for ($i = 0; $i < count($headerValues); $i++) { @@ -117,7 +117,7 @@ function ($a, $b) { * * @return object Output of {@link \Locale::parseLocale()} converted to an object */ - public function parseLocale($locale) + public function parseLocale(string $locale): object { return (object) \Locale::parseLocale($locale); } diff --git a/src/NoopTranslator.php b/src/NoopTranslator.php index 1f9aab2..635ccad 100644 --- a/src/NoopTranslator.php +++ b/src/NoopTranslator.php @@ -9,23 +9,28 @@ */ class NoopTranslator implements Translator { - public function translate($message, $context = null) + public function translate(string $message, ?string $context = null): string { return $message; } - public function translateInDomain($domain, $message, $context = null) + public function translateInDomain(string $domain, string $message, ?string $context = null): string { return $message; } - public function translatePlural($singular, $plural, $number, $context = null) + public function translatePlural(string $singular, string $plural, int $number, ?string $context = null): string { return $number === 1 ? $singular : $plural; } - public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null) - { + public function translatePluralInDomain( + string $domain, + string $singular, + string $plural, + int $number, + ?string $context = null + ): string { return $number === 1 ? $singular : $plural; } } diff --git a/src/Translation.php b/src/Translation.php index eb40287..8d49ed0 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -18,12 +18,12 @@ trait Translation /** * Translate a message * - * @param string $message - * @param string $context Message context + * @param string $message + * @param ?string $context Message context * * @return string Translated message or original message if no translation is found */ - public function translate($message, $context = null) + public function translate(string $message, ?string $context = null): string { return $this->translationDomain === null ? StaticTranslator::$instance->translate($message, $context) @@ -35,13 +35,13 @@ public function translate($message, $context = null) * * If no translation is found in the specified domain, the translation is also searched for in the default domain. * - * @param string $domain - * @param string $message - * @param string $context Message context + * @param string $domain + * @param string $message + * @param ?string $context Message context * * @return string Translated message or original message if no translation is found */ - public function translateInDomain($domain, $message, $context = null) + public function translateInDomain(string $domain, string $message, ?string $context = null): string { return StaticTranslator::$instance->translateInDomain($domain, $message, $context); } @@ -52,14 +52,14 @@ public function translateInDomain($domain, $message, $context = null) * The returned message is based on the given number to decide between the singular and plural forms. * That is also the case if no translation is found. * - * @param string $singular Singular message - * @param string $plural Plural message - * @param ?int $number Number to decide between the returned singular and plural forms - * @param string $context Message context + * @param string $singular Singular message + * @param string $plural Plural message + * @param ?int $number Number to decide between the returned singular and plural forms + * @param ?string $context Message context * * @return string Translated message or original message if no translation is found */ - public function translatePlural($singular, $plural, $number, $context = null) + public function translatePlural(string $singular, string $plural, ?int $number, ?string $context = null): string { return $this->translationDomain === null ? StaticTranslator::$instance->translatePlural($singular, $plural, $number ?? 0, $context) @@ -80,21 +80,26 @@ public function translatePlural($singular, $plural, $number, $context = null) * The returned message is based on the given number to decide between the singular and plural forms. * That is also the case if no translation is found. * - * @param string $domain - * @param string $singular Singular message - * @param string $plural Plural message - * @param ?int $number Number to decide between the returned singular and plural forms - * @param string $context Message context + * @param string $domain + * @param string $singular Singular message + * @param string $plural Plural message + * @param ?int $number Number to decide between the returned singular and plural forms + * @param ?string $context Message context * * @return string Translated message or original message if no translation is found */ - public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null) - { + public function translatePluralInDomain( + string $domain, + string $singular, + string $plural, + ?int $number, + ?string $context = null + ): string { return StaticTranslator::$instance->translatePluralInDomain( $domain, $singular, $plural, - $number ?? 0, + $number ?? 0, $context ); } diff --git a/src/functions.php b/src/functions.php index 74d58df..9730bbf 100644 --- a/src/functions.php +++ b/src/functions.php @@ -5,12 +5,12 @@ /** * Translate a message * - * @param string $message - * @param string $context Message context + * @param string $message + * @param ?string $context Message context * * @return string Translated message or original message if no translation is found */ -function t($message, $context = null) +function t(string $message, ?string $context = null): string { return StaticTranslator::$instance->translate($message, $context); } @@ -21,14 +21,14 @@ function t($message, $context = null) * The returned message is based on the given number to decide between the singular and plural forms. * That is also the case if no translation is found. * - * @param string $singular Singular message - * @param string $plural Plural message - * @param int $number Number to decide between the returned singular and plural forms - * @param string $context Message context + * @param string $singular Singular message + * @param string $plural Plural message + * @param ?int $number Number to decide between the returned singular and plural forms + * @param ?string $context Message context * * @return string Translated message or original message if no translation is found */ -function tp($singular, $plural, $number, $context = null) +function tp(string $singular, string $plural, ?int $number, ?string $context = null): string { - return StaticTranslator::$instance->translatePlural($singular, $plural, $number, $context); + return StaticTranslator::$instance->translatePlural($singular, $plural, $number ?? 0, $context); }