[recipes/plurals: 2/2] wip: work towards better plural handling for ingredients



commit 682e9ff6024c4677be253b42007a30bada0a2f37
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jan 15 21:12:24 2017 -0500

    wip: work towards better plural handling for ingredients
    
    The goal is to get things like:
    1     Egg
    2 1/2 Eggs
    3     Eggs
    We only want to do this for ingredients that have no units, and
    we want to use ngettext for this. Before we can do this we need
    to clean up the distinction between IDs and display labels for
    ingredients. We need to keep the ingredient list with untranslated
    IDs and only translate them for presentation.

 data/ingredients.list   |    6 ++--
 po/Makevars             |    1 +
 src/Makefile.am         |    4 +-
 src/gr-details-page.c   |   15 ++++++++++++-
 src/gr-ingredient.c     |   52 +++++++++++++++++++++++++++++++++++++++++++++++
 src/gr-ingredient.h     |    5 ++++
 src/gr-recipe-printer.c |   19 +++++++++++++++-
 7 files changed, 94 insertions(+), 8 deletions(-)
---
diff --git a/data/ingredients.list b/data/ingredients.list
index 0aaf340..1890090 100644
--- a/data/ingredients.list
+++ b/data/ingredients.list
@@ -1,7 +1,7 @@
-Almond
+Almond:Almonds
 Amaretti
 Anchovis
-Apple
+Apple:Apples
 Apricot
 Artichoke
 Asparagus
@@ -30,7 +30,7 @@ Chocolate frosting
 Cocoa powder
 Couscous
 Date
-Egg
+Egg:Eggs
 Fig
 Flour
 Garlic
diff --git a/po/Makevars b/po/Makevars
index 72a6204..83d8aac 100644
--- a/po/Makevars
+++ b/po/Makevars
@@ -11,6 +11,7 @@ top_builddir = ..
 XGETTEXT_OPTIONS = --from-code=UTF-8 \
         --keyword=_ --keyword=N_ \
         --keyword=C_:1c,2 --keyword=NC_:1c,2 \
+        --keyword=NN_:1,2 \
         --keyword=g_dngettext:2,3 \
         --flag=g_dngettext:2:pass-c-format \
         --flag=g_strdup_printf:1:c-format \
diff --git a/src/Makefile.am b/src/Makefile.am
index 2867298..354963e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -149,10 +149,10 @@ segments.c: $(top_srcdir)/data/segments.list
        $(AM_V_GEN) $(SED) -e 's/^\(.*\)$$/     N_("\1"),/' $^ > $@
 
 ingredients.c: $(top_srcdir)/data/ingredients.list
-       $(AM_V_GEN) $(SED) -e 's/^\(.*\)$$/     N_("\1"),/' $^ > $@
+       $(AM_V_GEN) $(SED) -e 's/^\([^:]*\)$$/  N_("\1")/' -e 's/^\([^:]*\):\(.*\)$$/   NN_("\1", "\2")/' $^ 
$@
 
 no-ingredients.c: $(top_srcdir)/data/ingredients.list
-       $(AM_V_GEN) $(SED) -e 's/^\(.*\)$$/     N_("no \1"),/' $^ > $@
+       $(AM_V_GEN) $(SED) -e 's/^\([^:]*\)$$/  N_("no \1"),/' -e 's/^\([^:]*\):\(.*\)$$/       N_("no 
\1"),/' $^ > $@
 
 resource_ui_files = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies 
$(srcdir)/recipes-ui.gresource.xml)
 
diff --git a/src/gr-details-page.c b/src/gr-details-page.c
index 10c4672..fe42db6 100644
--- a/src/gr-details-page.c
+++ b/src/gr-details-page.c
@@ -36,6 +36,7 @@
 #include "gr-utils.h"
 #include "gr-images.h"
 #include "gr-image-viewer.h"
+#include "gr-ingredient.h"
 #include "gr-ingredients-list.h"
 #include "gr-timer.h"
 #include "gr-recipe-printer.h"
@@ -819,6 +820,10 @@ populate_ingredients (GrDetailsPage *page,
                         GtkWidget *row;
                         GtkWidget *box;
                         g_autofree char *s = NULL;
+                        const char *ing;
+                        GrNumber *amount;
+                        const char *unit;
+                        GrNumber scaled;
 
                         box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
                         gtk_widget_show (box);
@@ -834,7 +839,15 @@ populate_ingredients (GrDetailsPage *page,
                         gtk_container_add (GTK_CONTAINER (box), label);
                         gtk_size_group_add_widget (group, label);
 
-                        label = gtk_label_new (ings[i]);
+                        amount = gr_ingredients_list_get_amount (page->ingredients, segments[j], ings[i]);
+                        unit = gr_ingredients_list_get_unit (page->ingredients, segments[j], ings[i]);
+
+                        gr_number_set_fraction (&scaled, num, denom);
+                        gr_number_multiply (amount, &scaled, &scaled);
+
+                        ing = gr_ingredient_get_name (&scaled, unit, ings[i]);
+
+                        label = gtk_label_new (ing);
                         g_object_set (label,
                                       "visible", TRUE,
                                       "xalign", 0.0,
diff --git a/src/gr-ingredient.c b/src/gr-ingredient.c
index b55bd15..5a27cf1 100644
--- a/src/gr-ingredient.c
+++ b/src/gr-ingredient.c
@@ -20,6 +20,8 @@
 
 #include "config.h"
 
+#include <math.h>
+
 #include <glib.h>
 #include <glib/gi18n.h>
 
@@ -27,11 +29,27 @@
 #include "gr-utils.h"
 
 
+#undef N_
+#define N_(a) (a),
+#define NN_(a,b) (a),
+
 static const char *names_[] = {
 #include "ingredients.c"
         NULL
 };
 
+#undef N_
+#undef NN_
+#define N_(a) (a),
+#define NN_(a,b) (b),
+
+static const char *plurals_[] = {
+#include "ingredients.c"
+};
+
+#undef N_
+#define N_(a) (a)
+
 static const char *negations[] = {
 #include "no-ingredients.c"
         NULL
@@ -60,6 +78,22 @@ translate_names (void)
         }
 }
 
+static const char *
+find_plural (const char *ing, int n)
+{
+        int i;
+
+        translate_names ();
+
+        for (i = 0; names[i]; i++) {
+                if (strcmp (ing, names[i]) == 0) {
+                        return ngettext (names_[i], plurals_[i], n);
+                }
+        }
+
+        return ing;
+}
+
 const char **
 gr_ingredient_get_names (int *length)
 {
@@ -150,3 +184,21 @@ gr_ingredient_get_image (const char *name)
 
         return NULL;
 }
+
+const char *
+gr_ingredient_get_name (GrNumber   *amount,
+                        const char *unit,
+                        const char *ingredient)
+{
+        int val;
+
+        if (unit && unit[0])
+                return ingredient;
+
+        if (amount->fraction && amount->denom == 1)
+                val = amount->num;
+        else
+                val = (int) ceil (amount->value);
+
+        return find_plural (ingredient, val);
+}
diff --git a/src/gr-ingredient.h b/src/gr-ingredient.h
index a36dad2..cb4e54e 100644
--- a/src/gr-ingredient.h
+++ b/src/gr-ingredient.h
@@ -22,6 +22,8 @@
 
 #include <gtk/gtk.h>
 
+#include "gr-number.h"
+
 G_BEGIN_DECLS
 
 const char    **gr_ingredient_get_names    (int        *length);
@@ -29,5 +31,8 @@ const char     *gr_ingredient_find         (const char *text);
 const char     *gr_ingredient_get_id       (const char *name);
 const char     *gr_ingredient_get_negation (const char *name);
 char           *gr_ingredient_get_image    (const char *name);
+const char     *gr_ingredient_get_name     (GrNumber   *amount,
+                                            const char *unit,
+                                            const char *name);
 
 G_END_DECLS
diff --git a/src/gr-recipe-printer.c b/src/gr-recipe-printer.c
index a8ff5ca..290a835 100644
--- a/src/gr-recipe-printer.c
+++ b/src/gr-recipe-printer.c
@@ -24,6 +24,8 @@
 
 #include "gr-recipe-printer.h"
 #include "gr-ingredients-list.h"
+#include "gr-ingredient.h"
+#include "gr-number.h"
 #include "gr-images.h"
 #include "gr-utils.h"
 
@@ -168,6 +170,9 @@ begin_print (GtkPrintOperation *operation,
                         mid = length / 2 + length % 2;
                         for (i = 0; i < mid; i++) {
                                 char *unit;
+                                GrNumber *amount;
+                                const char *u;
+                                const char *ing;
 
                                 g_string_append (s, "\n");
 
@@ -175,14 +180,24 @@ begin_print (GtkPrintOperation *operation,
                                 g_string_append (s, unit);
                                 g_free (unit);
                                 g_string_append (s, " ");
-                                g_string_append (s, ings[i]);
+
+                                u = gr_ingredients_list_get_unit (ingredients, segs[j], ings[i]);
+                                amount = gr_ingredients_list_get_amount (ingredients, segs[j], ings[i]);
+                                ing = gr_ingredient_get_name (amount, u, ings[i]);
+
+                                g_string_append (s, ing);
                                 g_string_append (s, "\t");
                                 if (mid + i < length) {
                                         unit = gr_ingredients_list_scale_unit (ingredients, segs[j], 
ings[mid + i], 1, 1);
                                         g_string_append (s, unit);
                                         g_free (unit);
                                         g_string_append (s, " ");
-                                        g_string_append (s, ings[mid + i]);
+
+                                        u = gr_ingredients_list_get_unit (ingredients, segs[j], ings[mid + 
i]);
+                                        amount = gr_ingredients_list_get_amount (ingredients, segs[j], 
ings[mid + i]);
+                                        ing = gr_ingredient_get_name (amount, u, ings[mid + i]);
+
+                                        g_string_append (s, ing);
                                 }
                         }
                 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]