[gnome-documents] format: support %Id conversion characters in format.js



commit 528b5f37c99e1fa0692f6394c3dcbd845f3f0634
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Thu Mar 29 15:24:57 2012 -0400

    format: support %Id conversion characters in format.js
    
    This is needed for languages which translate numbers with an alternative
    representation, such as Persian.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=673092

 src/format.js      |   13 +++++++++++--
 src/lib/gd-utils.c |   12 ++++++++++++
 src/lib/gd-utils.h |    2 ++
 3 files changed, 25 insertions(+), 2 deletions(-)
---
diff --git a/src/format.js b/src/format.js
index dcebf7e..32abd19 100644
--- a/src/format.js
+++ b/src/format.js
@@ -1,5 +1,7 @@
 /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
 
+const Gd = imports.gi.Gd;
+
 /*
  * This function is intended to extend the String object and provide
  * an String.format API for string formatting.
@@ -17,11 +19,14 @@ function format() {
     let i = 0;
     let args = arguments;
 
-    return str.replace(/%([0-9]+)?(?:\.([0-9]+))?(.)/g, function (str, widthGroup, precisionGroup, genericGroup) {
+    return str.replace(/%(I+)?([0-9]+)?(?:\.([0-9]+))?(.)/g, function (str, flagsGroup, widthGroup, precisionGroup, genericGroup) {
 
                     if (precisionGroup != '' && genericGroup != 'f')
                         throw new Error("Precision can only be specified for 'f'");
 
+                    if (flagsGroup == 'I' && genericGroup != 'd')
+                        throw new Error("Alternative output digits can only be specfied for 'd'");
+
                     let fillChar = (widthGroup[0] == '0') ? '0' : ' ';
                     let width = parseInt(widthGroup, 10) || 0;
 
@@ -41,7 +46,11 @@ function format() {
                             s = args[i++].toString();
                             break;
                         case 'd':
-                            s = parseInt(args[i++]).toString();
+                            let intV = parseInt(args[i++]);
+                            if (flagsGroup == 'I')
+                                s = Gd.format_int_alternative_output(intV);
+                            else
+                                s = intV.toString();
                             break;
                         case 'x':
                             s = parseInt(args[i++]).toString(16);
diff --git a/src/lib/gd-utils.c b/src/lib/gd-utils.c
index 35cda0e..563573d 100644
--- a/src/lib/gd-utils.c
+++ b/src/lib/gd-utils.c
@@ -687,3 +687,15 @@ gd_create_variant_from_pixbuf (GdkPixbuf *pixbuf)
                                                     g_object_ref (pixbuf)));
   return g_variant_ref_sink (variant);
 }
+
+/**
+ * gd_format_int_alternative_output:
+ * @intval:
+ *
+ * Returns: (transfer full):
+ */
+gchar *
+gd_format_int_alternative_output (gint intval)
+{
+  return g_strdup_printf ("%Id", intval);
+}
diff --git a/src/lib/gd-utils.h b/src/lib/gd-utils.h
index 0104bf0..2adbb50 100644
--- a/src/lib/gd-utils.h
+++ b/src/lib/gd-utils.h
@@ -77,5 +77,7 @@ void   gd_entry_focus_hack (GtkWidget *entry,
 
 GVariant *gd_create_variant_from_pixbuf (GdkPixbuf *pixbuf);
 
+gchar * gd_format_int_alternative_output (gint intval);
+
 #endif /* __GD_UTILS_H__ */
                                   



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