From f583810d5c2bdaf28abff8c1596bc649b848592d Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 27 Jul 2018 19:39:34 +0200 Subject: [PATCH] Properly pluralize everything (closes #19) --- grocy.openapi.json | 3 +++ helpers/extensions.php | 10 ++++++++++ localization/de.php | 13 ++++++++++++- localization/it.php | 1 - localization/no.php | 1 - migrations/0030.sql | 2 ++ public/js/grocy.js | 10 ++++++++++ public/viewjs/components/productcard.js | 5 +++-- views/batteriesoverview.blade.php | 4 ++-- views/habitsoverview.blade.php | 4 ++-- views/quantityunitform.blade.php | 7 ++++++- views/recipeform.blade.php | 2 +- views/recipes.blade.php | 2 +- views/shoppinglist.blade.php | 2 +- views/shoppinglistform.blade.php | 2 +- views/stockoverview.blade.php | 10 +++++----- 16 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 migrations/0030.sql diff --git a/grocy.openapi.json b/grocy.openapi.json index 403c536b..f72b4a09 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1244,6 +1244,9 @@ "name": { "type": "string" }, + "name_plural": { + "type": "string" + }, "description": { "type": "string" }, diff --git a/helpers/extensions.php b/helpers/extensions.php index a23a73af..82a66ff2 100644 --- a/helpers/extensions.php +++ b/helpers/extensions.php @@ -168,3 +168,13 @@ function GetUserDisplayName($user) return $displayName; } + +function Pluralize($number, $singularForm, $pluralForm) +{ + $text = $singularForm; + if ($number != 1 && $pluralForm !== null && !empty($pluralForm)) + { + $text = $pluralForm; + } + return $text; +} diff --git a/localization/de.php b/localization/de.php index db943ea8..31d303d3 100644 --- a/localization/de.php +++ b/localization/de.php @@ -2,7 +2,6 @@ return array( 'Stock overview' => 'Bestand', - '#1 products with #2 units in stock' => '#1 Produkte (#2 Einheiten) vorrätig', '#1 products expiring within the next #2 days' => '#1 Produkte laufen innerhalb der nächsten #2 Tage ab', '#1 products are already expired' => '#1 Produkte sind bereits abgelaufen', '#1 products are below defined min. stock amount' => '#1 Produkte sind unter Mindestbestand', @@ -194,6 +193,18 @@ return array( 'Price' => 'Preis', 'in #1 per purchase quantity unit' => 'in #1 pro Einkaufsmengeneinheit', 'The price cannot be lower than #1' => 'Der Preis darf nicht niedriger als #1 sein', + '#1 product expires within the next #2 days' => '#1 Produkt läuft innerhalb der nächsten #2 Tage ab', + '#1 product is already expired' => '#1 Produkt ist bereits abgelaufen', + '#1 product is below defined min. stock amount' => '#1 Produkt ist unter Mindestbestand', + 'Unit' => 'Einheit', + 'Units' => 'Einheiten', + '#1 habit is due to be done within the next #2 days' => '#1 Gewohnheit steht in den nächsten #2 Tagen an', + '#1 habit is overdue to be done' => '#1 Gewohnheit ist überfällig', + '#1 battery ist due to be charged within the next #2 days' => '#1 Batterie muss in den nächsten #2 Tagen geladen werden', + '#1 battery ist overdue to be charged' => '#1 Batterie ist überfällig', + '#1 unit was automatically added and will apply in addition to the amount entered here' => '#1 Einheit wurde automatisch hinzugefügt und gilt zusätzlich der hier eingegebenen Menge', + 'in singular form' => 'in der Einzahl', + 'in plural form' => 'in der Mehrzahl', //Constants 'manually' => 'Manuell', diff --git a/localization/it.php b/localization/it.php index 8ca0b331..7a36f846 100644 --- a/localization/it.php +++ b/localization/it.php @@ -2,7 +2,6 @@ return array( 'Stock overview' => 'Dispensa', - '#1 products with #2 units in stock' => '#1 prodotti stano per finire(#2 unità)', '#1 products expiring within the next #2 days' => '#1 prodotti scadranno tra #2 giorni', '#1 products are already expired' => '#1 prodotti scaduti', '#1 products are below defined min. stock amount' => '#1 prodotti sotto il limite minimo', diff --git a/localization/no.php b/localization/no.php index 6161a44f..261bba80 100644 --- a/localization/no.php +++ b/localization/no.php @@ -2,7 +2,6 @@ return array( 'Stock overview' => 'Husholdning', - '#1 products with #2 units in stock' => '#1 Produkter med #2 i husholdningen', '#1 products expiring within the next #2 days' => '#1 Produkter som går ut på dato innen de neste #2 dagene', '#1 products are already expired' => '#1 Produkt som har gått ut på dato', '#1 products are below defined min. stock amount' => '#1 Produkt under minimum husholdningsnivå', diff --git a/migrations/0030.sql b/migrations/0030.sql new file mode 100644 index 00000000..425795cf --- /dev/null +++ b/migrations/0030.sql @@ -0,0 +1,2 @@ +ALTER TABLE quantity_units +ADD name_plural TEXT; diff --git a/public/js/grocy.js b/public/js/grocy.js index d8e3e410..421cb200 100644 --- a/public/js/grocy.js +++ b/public/js/grocy.js @@ -19,6 +19,16 @@ U = function(relativePath) return Grocy.BaseUrl.replace(/\/$/, '') + relativePath; } +Pluralize = function(number, singularForm, pluralForm) +{ + var text = singularForm; + if (number != 1 && pluralForm !== null && !pluralForm.isEmpty()) + { + text = pluralForm; + } + return text; +} + if (!Grocy.ActiveNav.isEmpty()) { var menuItem = $('#sidebarResponsive').find("[data-nav-for-page='" + Grocy.ActiveNav + "']"); diff --git a/public/viewjs/components/productcard.js b/public/viewjs/components/productcard.js index f7acfac8..aa9fcace 100644 --- a/public/viewjs/components/productcard.js +++ b/public/viewjs/components/productcard.js @@ -5,10 +5,11 @@ Grocy.Components.ProductCard.Refresh = function(productId) Grocy.Api.Get('stock/get-product-details/' + productId, function(productDetails) { + var stockAmount = productDetails.stock_amount || '0'; $('#productcard-product-name').text(productDetails.product.name); - $('#productcard-product-stock-amount').text(productDetails.stock_amount || '0'); + $('#productcard-product-stock-amount').text(stockAmount); $('#productcard-product-stock-qu-name').text(productDetails.quantity_unit_stock.name); - $('#productcard-product-stock-qu-name2').text(productDetails.quantity_unit_stock.name); + $('#productcard-product-stock-qu-name2').text(Pluralize(stockAmount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural)); $('#productcard-product-last-purchased').text((productDetails.last_purchased || L('never')).substring(0, 10)); $('#productcard-product-last-purchased-timeago').text($.timeago(productDetails.last_purchased || '')); $('#productcard-product-last-used').text((productDetails.last_used || L('never')).substring(0, 10)); diff --git a/views/batteriesoverview.blade.php b/views/batteriesoverview.blade.php index b95119ab..440bae49 100644 --- a/views/batteriesoverview.blade.php +++ b/views/batteriesoverview.blade.php @@ -12,8 +12,8 @@

@yield('title')

-

{{ $L('#1 batteries are due to be charged within the next #2 days', $countDueNextXDays, $nextXDays) }}

-

{{ $L('#1 batteries are overdue to be charged', $countOverdue) }}

+

{{ Pluralize($countDueNextXDays, $L('#1 battery ist due to be charged within the next #2 days', $countDueNextXDays, $nextXDays), $L('#1 batteries are due to be charged within the next #2 days', $countDueNextXDays, $nextXDays)) }}

+

{{ Pluralize($countOverdue, $L('#1 battery ist overdue to be charged', $countOverdue), $L('#1 batteries are overdue to be charged', $countOverdue)) }}

diff --git a/views/habitsoverview.blade.php b/views/habitsoverview.blade.php index 931a8343..eda73ccd 100644 --- a/views/habitsoverview.blade.php +++ b/views/habitsoverview.blade.php @@ -12,8 +12,8 @@

@yield('title')

-

{{ $L('#1 habits are due to be done within the next #2 days', $countDueNextXDays, $nextXDays) }}

-

{{ $L('#1 habits are overdue to be done', $countOverdue) }}

+

{{ Pluralize($countDueNextXDays, $L('#1 habit is due to be done within the next #2 days', $countDueNextXDays, $nextXDays), $L('#1 habits are due to be done within the next #2 days', $countDueNextXDays, $nextXDays)) }}

+

{{ Pluralize($countOverdue, $L('#1 habit is overdue to be done', $countOverdue), $L('#1 habits are overdue to be done', $countOverdue)) }}

diff --git a/views/quantityunitform.blade.php b/views/quantityunitform.blade.php index 7c364ace..37740d86 100644 --- a/views/quantityunitform.blade.php +++ b/views/quantityunitform.blade.php @@ -22,11 +22,16 @@
- +
{{ $L('A name is required') }}
+
+ + +
+
diff --git a/views/recipeform.blade.php b/views/recipeform.blade.php index 9c88132a..37b0cce0 100644 --- a/views/recipeform.blade.php +++ b/views/recipeform.blade.php @@ -76,7 +76,7 @@ {{ FindObjectInArrayByPropertyValue($products, 'id', $recipePosition->product_id)->name }} - {{ $recipePosition->amount }} {{ FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $recipePosition->product_id)->qu_id_stock)->name }} + {{ $recipePosition->amount }} {{ Pluralize($recipePosition->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $recipePosition->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $recipePosition->product_id)->qu_id_stock)->name_plural) }} @if(FindObjectInArrayByPropertyValue($recipesFulfillment, 'recipe_pos_id', $recipePosition->id)->need_fulfilled == 1) {{ $L('Enough in stock') }} @else {{ $L('Not enough in stock, #1 missing, #2 already on shopping list', FindObjectInArrayByPropertyValue($recipesFulfillment, 'recipe_pos_id', $recipePosition->id)->missing_amount, FindObjectInArrayByPropertyValue($recipesFulfillment, 'recipe_pos_id', $recipePosition->id)->amount_on_shopping_list) }} @endif diff --git a/views/recipes.blade.php b/views/recipes.blade.php index 89f3ad10..f2c414a5 100644 --- a/views/recipes.blade.php +++ b/views/recipes.blade.php @@ -71,7 +71,7 @@
    @foreach($selectedRecipePositions as $selectedRecipePosition)
  • - {{ $selectedRecipePosition->amount }} {{ FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $selectedRecipePosition->product_id)->qu_id_stock)->name }} {{ FindObjectInArrayByPropertyValue($products, 'id', $selectedRecipePosition->product_id)->name }} + {{ $selectedRecipePosition->amount }} {{ Pluralize($selectedRecipePosition->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $selectedRecipePosition->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $selectedRecipePosition->product_id)->qu_id_stock)->name_plural) }} {{ FindObjectInArrayByPropertyValue($products, 'id', $selectedRecipePosition->product_id)->name }} @if(FindObjectInArrayByPropertyValue($recipesFulfillment, 'recipe_pos_id', $selectedRecipePosition->id)->need_fulfilled == 1) {{ $L('Enough in stock') }} @else {{ $L('Not enough in stock, #1 missing, #2 already on shopping list', FindObjectInArrayByPropertyValue($recipesFulfillment, 'recipe_pos_id', $selectedRecipePosition->id)->missing_amount, FindObjectInArrayByPropertyValue($recipesFulfillment, 'recipe_pos_id', $selectedRecipePosition->id)->amount_on_shopping_list) }} @endif @if(!empty($selectedRecipePosition->note)) diff --git a/views/shoppinglist.blade.php b/views/shoppinglist.blade.php index e003c75d..1b0020cc 100644 --- a/views/shoppinglist.blade.php +++ b/views/shoppinglist.blade.php @@ -50,7 +50,7 @@ @if(!empty($listItem->product_id)) {{ FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->name }}
    @endif{{ $listItem->note }} - {{ $listItem->amount + $listItem->amount_autoadded }} @if(!empty($listItem->product_id)) {{ FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->qu_id_purchase)->name }}@endif + {{ $listItem->amount + $listItem->amount_autoadded }} @if(!empty($listItem->product_id)){{ Pluralize($listItem->amount + $listItem->amount_autoadded, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->qu_id_purchase)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->qu_id_purchase)->name_plural) }}@endif @endforeach diff --git a/views/shoppinglistform.blade.php b/views/shoppinglistform.blade.php index 1a6bb56b..d698d25c 100644 --- a/views/shoppinglistform.blade.php +++ b/views/shoppinglistform.blade.php @@ -27,7 +27,7 @@ ))
    - +
    {{ $L('This cannot be negative') }}
    diff --git a/views/stockoverview.blade.php b/views/stockoverview.blade.php index 4c854616..a563209e 100644 --- a/views/stockoverview.blade.php +++ b/views/stockoverview.blade.php @@ -11,10 +11,10 @@ @section('content')
    -

    @yield('title') {{ $L('#1 products with #2 units in stock', count($currentStock), SumArrayValue($currentStock, 'amount')) }}

    -

    {{ $L('#1 products expiring within the next #2 days', $countExpiringNextXDays, $nextXDays) }}

    -

    {{ $L('#1 products are already expired', $countAlreadyExpired) }}

    -

    {{ $L('#1 products are below defined min. stock amount', count($missingProducts)) }}

    +

    @yield('title') {{ count($currentStock) . ' ' . Pluralize(count($currentStock), $L('Product'), $L('Products')) }}, {{ SumArrayValue($currentStock, 'amount') . ' ' . Pluralize(SumArrayValue($currentStock, 'amount'), $L('Unit'), $L('Units')) }}

    +

    {{ Pluralize($countExpiringNextXDays, $L('#1 product expires within the next #2 days', $countExpiringNextXDays, $nextXDays), $L('#1 products expiring within the next #2 days', $countExpiringNextXDays, $nextXDays)) }}

    +

    {{ Pluralize($countAlreadyExpired, $L('#1 product is already expired', $countAlreadyExpired), $L('#1 products are already expired', $countAlreadyExpired)) }}

    +

    {{ Pluralize(count($missingProducts), $L('#1 product is below defined min. stock amount', count($missingProducts)), $L('#1 products are below defined min. stock amount', count($missingProducts))) }}

    @@ -69,7 +69,7 @@ {{ FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->name }} - {{ $currentStockEntry->amount }} {{ FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name }} + {{ $currentStockEntry->amount }} {{ Pluralize($currentStockEntry->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name_plural) }} {{ $currentStockEntry->best_before_date }}