From a6795b61ce249d0f1b05330562727b2f1d682401 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 23 Jun 2023 11:03:44 -0400 Subject: [PATCH] Add feature request processwire/processwire-issues#1757 which adds support for non-plural translations of 0 quantities in the `_n('%d item', %d items', 0);` translation function. To use, translate the file wire/modules/LanguageSupport/LanguageTranslator.php and edi the "Is zero (0) plural or singular?" setting at the top. --- wire/core/LanguageFunctions.php | 12 +++++++++--- wire/modules/LanguageSupport/LanguageTranslator.php | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/wire/core/LanguageFunctions.php b/wire/core/LanguageFunctions.php index 68241156..4a0fb01e 100644 --- a/wire/core/LanguageFunctions.php +++ b/wire/core/LanguageFunctions.php @@ -284,7 +284,15 @@ function _x($text, $context, $textdomain = null) { * */ function _n($textSingular, $textPlural, $count, $textdomain = null) { - return $count == 1 ? __($textSingular, $textdomain) : __($textPlural, $textdomain); + $count = (int) $count; + $value = $count === 1 ? __($textSingular, $textdomain) : __($textPlural, $textdomain); + if($count === 0 && $value !== $textPlural && wire()->languages) { + $plural = __('0-plural', 'common'); + if(strtolower($plural) === '0-singular') { + $value = __($textSingular, $textdomain); + } + } + return $value; } /** @@ -423,5 +431,3 @@ function wireLangTranslations(array $values = array()) { function wireLangReplacements(array $values) { return __(true, 'replacements', $values); } - - diff --git a/wire/modules/LanguageSupport/LanguageTranslator.php b/wire/modules/LanguageSupport/LanguageTranslator.php index 6e6444e0..7f37197f 100644 --- a/wire/modules/LanguageSupport/LanguageTranslator.php +++ b/wire/modules/LanguageSupport/LanguageTranslator.php @@ -644,6 +644,7 @@ class LanguageTranslator extends Wire { $v = ''; switch(strtolower($str)) { + case '0-plural': $v = $this->_('0-plural'); break; // Is zero (0) plural or singular? type=radios options=[0-plural:Plural,0-singular:Singular] // i.e. would language use "0 items" (plural) or "0 item" (singular)? case 'edit': $v = $this->_('Edit'); break; case 'delete': $v = $this->_('Delete'); break; case 'save': $v = $this->_('Save'); break;