A free open source IT asset/license management system
Fixed #18260 - informal variants of locales returned wrong plural translations
snipe3acdba789708
diff --git a/app/Services/SnipeTranslator.php b/app/Services/SnipeTranslator.phpindex eb7a9eb044..8a5b0578c0 100644--- a/app/Services/SnipeTranslator.php+++ b/app/Services/SnipeTranslator.php@@ -16,13 +16,13 @@ use Illuminate\Translation\Translator;** This method is called by the trans_choice() helper, which we *do* use a lot.***************************************************************/-class SnipeTranslator extends Translator {-- static $legacy_translation_namespaces = [- "backup::" //Spatie backup uses 'legacy' locale names+class SnipeTranslator extends Translator+{+ public static $legacy_translation_namespaces = [+ 'backup::', // Spatie backup uses 'legacy' locale names];- //This is copied-and-pasted (almost) verbatim from Illuminate\Translation\Translator+ // This is copied-and-pasted (almost) verbatim from Illuminate\Translation\Translatorpublic function choice($key, $number, array $replace = [], $locale = null){$line = $this->get(@@ -40,7 +40,18 @@ class SnipeTranslator extends Translator {$replace['count'] = $number;}- $underscored_locale = str_replace("-","_",$locale); // OUR CHANGE.+ // Snipe-IT uses a `-if` suffix on informal-variant locales (e.g.+ // `de-if` for informal German alongside `de-DE` for formal).+ // Laravel's MessageSelector plural-rule table only knows base and+ // country-scoped codes (`de`, `de_DE`, etc.), so an informal locale+ // falls through to the `default: return 0` branch, which always+ // picks the singular form. Strip the informal marker before mapping+ // dashes to underscores so the base language's plural rule kicks in+ // for the choice, without disturbing the actual locale used for+ // file lookup.+ $plural_locale = preg_replace('/-if$/', '', $locale);+ $underscored_locale = str_replace('-', '_', $plural_locale); // OUR CHANGE.+return $this->makeReplacements( // BELOW - that $underscored_locale is the *ONLY* modified part$this->getSelector()->choose($line, $number, $underscored_locale), $replace);@@ -57,7 +68,7 @@ class SnipeTranslator extends Translator {if (preg_match("/^$namespace/", $key)) {$modified_locale = Helper::mapBackToLegacyLocale($locale);$changed_fallback = true;- $this->fallback = 'en'; //TODO - should this be 'env-able'? Or do we just put our foot down and say 'en'?+ $this->fallback = 'en'; // TODO - should this be 'env-able'? Or do we just put our foot down and say 'en'?break;}}@@ -66,8 +77,7 @@ class SnipeTranslator extends Translator {if ($changed_fallback) {$this->fallback = $previous_fallback;}+return $result;}---}\ No newline at end of file+}diff --git a/resources/views/blade/info-panel/index.blade.php b/resources/views/blade/info-panel/index.blade.phpindex 834d536b19..041704d18e 100644--- a/resources/views/blade/info-panel/index.blade.php+++ b/resources/views/blade/info-panel/index.blade.php@@ -392,7 +392,7 @@@if ($infoPanelObj->depreciation && $infoPanelObj->purchase_date)<x-info-element icon_type="depreciation" title="{{ trans('general.depreciation') }}">{!! $infoPanelObj->depreciation->present()->nameUrl !!}- ({{ $infoPanelObj->depreciation->months.' '.trans('general.months')}})+ ({{ trans_choice('general.months_plural', $infoPanelObj->depreciation->months) }})</x-info-element><x-info-element icon_type="depreciation-calendar" class="{{ $infoPanelObj->depreciationProgressPercent() > 90 ? 'text-danger' : '' }}" title="{{ trans('admin/hardware/form.fully_depreciated') }}">diff --git a/resources/views/depreciations/view.blade.php b/resources/views/depreciations/view.blade.phpindex d41f5540dd..5b034e13b8 100644--- a/resources/views/depreciations/view.blade.php+++ b/resources/views/depreciations/view.blade.php@@ -3,7 +3,7 @@{{-- Page title --}}@section('title')- {{ trans('general.depreciation') }}: {{ $depreciation->name }} ({{ $depreciation->months }} {{ trans('general.months') }})+ {{ trans('general.depreciation') }}: {{ $depreciation->name }} ({{ trans_choice('general.months_plural', $depreciation->months) }})@parent@stopdiff --git a/resources/views/reports/licenses.blade.php b/resources/views/reports/licenses.blade.phpindex 5c89a0613d..f171b7bd66 100644--- a/resources/views/reports/licenses.blade.php+++ b/resources/views/reports/licenses.blade.php@@ -67,7 +67,7 @@{{ $snipeSettings->default_currency }}{{ Helper::formatCurrencyOutput($license->purchase_cost) }}</td><td>- {{ ($license->depreciation) ? e($license->depreciation->name).' ('.$license->depreciation->months.' '.trans('general.months').')' : '' }}+ {{ ($license->depreciation) ? e($license->depreciation->name).' ('.trans_choice('general.months_plural', $license->depreciation->months).')' : '' }}</td><td class="text-right">{{ $snipeSettings->default_currency }}{{ Helper::formatCurrencyOutput($license->getDepreciatedValue()) }}diff --git a/tests/Unit/SnipeTranslatorTest.php b/tests/Unit/SnipeTranslatorTest.phpindex 5d9817c50b..0dfc2ba47f 100644--- a/tests/Unit/SnipeTranslatorTest.php+++ b/tests/Unit/SnipeTranslatorTest.php@@ -91,4 +91,21 @@ class SnipeTranslatorTest extends TestCasetrans('backup::notifications.exception_message', ['message' => 'MESSAGE'], 'pt_BR'));}++ public function test_trans_choice_plural_on_informal_locale()+ {+ // Regression coverage for GH #18260: Laravel's MessageSelector doesn't+ // know the `-if` informal-variant codes, so it used to fall through to+ // `default: return 0` and always pick the singular form. SnipeTranslator+ // strips the `-if` suffix before the plural-rule lookup so the base+ // language's rule kicks in.+ $this->assertEquals(+ '1 Monat',+ trans_choice('general.months_plural', 1, [], 'de-if')+ );+ $this->assertEquals(+ '24 Monate',+ trans_choice('general.months_plural', 24, [], 'de-if')+ );+ }}