[nautilus/wip/coreyberla/app-chooser-fixes: 16/16] eel-string: Support Unicode when capitalizing first character




commit 0c0e92a0ea900ab3075961c8e8a28592a1664aa9
Author: António Fernandes <antoniof gnome org>
Date:   Mon Aug 8 22:10:14 2022 +0100

    eel-string: Support Unicode when capitalizing first character
    
    And use this instead of g_ascii_to_upper() in app-chooser.c
    
    This is more i18n-friendly.

 eel/eel-string.c           | 18 +++++++++++++++---
 src/nautilus-app-chooser.c |  7 +++++--
 2 files changed, 20 insertions(+), 5 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index 5d70089a8..5cd705f3d 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -95,16 +95,28 @@ eel_str_double_underscores (const char *string)
 char *
 eel_str_capitalize (const char *string)
 {
-    char *capitalized;
+    char *capitalized = NULL;
 
     if (string == NULL)
     {
         return NULL;
     }
 
-    capitalized = g_strdup (string);
+    if (g_utf8_validate (string, -1, NULL))
+    {
+        g_autofree gunichar *ucs4 = NULL;
+        ucs4 = g_utf8_to_ucs4 (string, -1, NULL, NULL, NULL);
+        if (ucs4 != NULL)
+        {
+            ucs4[0] = g_unichar_toupper (ucs4[0]);
+            capitalized = g_ucs4_to_utf8 (ucs4, -1, NULL, NULL, NULL);
+        }
+    }
 
-    capitalized[0] = g_ascii_toupper (capitalized[0]);
+    if (capitalized == NULL)
+    {
+        return g_strdup (string);
+    }
 
     return capitalized;
 }
diff --git a/src/nautilus-app-chooser.c b/src/nautilus-app-chooser.c
index af9b127d0..e15abc3ae 100644
--- a/src/nautilus-app-chooser.c
+++ b/src/nautilus-app-chooser.c
@@ -9,6 +9,8 @@
 #include <libadwaita-1/adwaita.h>
 #include <glib/gi18n.h>
 
+#include <eel/eel-string.h>
+
 #include "nautilus-file.h"
 #include "nautilus-signaller.h"
 
@@ -218,8 +220,9 @@ nautilus_app_chooser_constructed (GObject *object)
         content_type_description = g_content_type_get_description (self->content_type);
         if (content_type_description != NULL)
         {
-            content_type_description[0] = g_ascii_toupper (content_type_description[0]);
-            adw_action_row_set_subtitle (ADW_ACTION_ROW (self->set_default_row), content_type_description);
+            g_autofree gchar *capitalized = NULL;
+            capitalized = eel_str_capitalize (content_type_description);
+            adw_action_row_set_subtitle (ADW_ACTION_ROW (self->set_default_row), capitalized);
         }
     }
     else


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