[Bf-blender-cvs] [54d156288d6] filebrowser_redesign: Correct string translation logic

Julian Eisel noreply at git.blender.org
Tue Aug 27 15:30:46 CEST 2019


Commit: 54d156288d607df76941082f7cba60d33542ae30
Author: Julian Eisel
Date:   Tue Aug 27 15:21:55 2019 +0200
Branches: filebrowser_redesign
https://developer.blender.org/rB54d156288d607df76941082f7cba60d33542ae30

Correct string translation logic

Refactors BLI_filelist_entry_datetime_to_string so that user message
strings are created in UI code, not BLI. That way we can also handle
translations correctly and better.

===================================================================

M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/intern/BLI_filelist.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/filesel.c

===================================================================

diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index bc9e646cb3b..d78f167a8fd 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -102,10 +102,10 @@ void BLI_filelist_entry_owner_to_string(const struct stat *st, const bool compac
 void BLI_filelist_entry_datetime_to_string(const struct stat *st,
                                            const int64_t ts,
                                            const bool compact,
-                                           const bool use_relative_str,
                                            char r_time[],
                                            char r_date[],
-                                           bool *r_date_needs_translate);
+                                           bool *r_is_today,
+                                           bool *r_is_yesterday);
 
 /* Files */
 
diff --git a/source/blender/blenlib/intern/BLI_filelist.c b/source/blender/blenlib/intern/BLI_filelist.c
index 0ee9760120e..68794e2d820 100644
--- a/source/blender/blenlib/intern/BLI_filelist.c
+++ b/source/blender/blenlib/intern/BLI_filelist.c
@@ -348,30 +348,23 @@ void BLI_filelist_entry_owner_to_string(const struct stat *st,
 /**
  * Convert given entry's time into human-readable strings.
  *
- * \param use_relative_string: Return "Today" and "Yesterday" as date string when applicable.
- * \param *r_date_needs_translate: optional, \a r_date should be translated if this returns true.
- *                                 Note that we don't translate in here directly because this
- *                                 function doesn't know where the string will be displayed (i.e.
- *                                 IFACE_ vs. TIP_)
+ * \param r_is_today: optional, returns true if the date matches today's.
+ * \param r_is_yesterday: optional, returns true if the date matches yesterday's.
  */
 void BLI_filelist_entry_datetime_to_string(const struct stat *st,
                                            const int64_t ts,
                                            const bool compact,
-                                           const bool use_relative_str,
                                            char r_time[FILELIST_DIRENTRY_TIME_LEN],
                                            char r_date[FILELIST_DIRENTRY_DATE_LEN],
-                                           bool *r_date_needs_translate)
+                                           bool *r_is_today,
+                                           bool *r_is_yesterday)
 {
   int today_year = 0;
   int today_yday = 0;
   int yesterday_year = 0;
   int yesterday_yday = 0;
 
-  if (r_date_needs_translate) {
-    *r_date_needs_translate = false;
-  }
-
-  if (use_relative_str) {
+  if (r_is_today || r_is_yesterday) {
     /* Localtime() has only one buffer so need to get data out before called again. */
     const time_t ts_now = time(NULL);
     struct tm *today = localtime(&ts_now);
@@ -383,6 +376,13 @@ void BLI_filelist_entry_datetime_to_string(const struct stat *st,
     mktime(today);
     yesterday_year = today->tm_year;
     yesterday_yday = today->tm_yday;
+
+    if (r_is_today) {
+      *r_is_today = false;
+    }
+    if (r_is_yesterday) {
+      *r_is_yesterday = false;
+    }
   }
 
   const time_t ts_mtime = ts;
@@ -397,26 +397,19 @@ void BLI_filelist_entry_datetime_to_string(const struct stat *st,
   if (r_time) {
     strftime(r_time, sizeof(*r_time) * FILELIST_DIRENTRY_TIME_LEN, "%H:%M", tm);
   }
+
   if (r_date) {
-    if (use_relative_str && (tm->tm_year == today_year) && (tm->tm_yday == today_yday)) {
-      BLI_strncpy(r_date, "Today", sizeof(*r_date) * FILELIST_DIRENTRY_DATE_LEN);
-      if (r_date_needs_translate) {
-        *r_date_needs_translate = true;
-      }
-    }
-    else if (use_relative_str && (tm->tm_year == yesterday_year) &&
-             (tm->tm_yday == yesterday_yday)) {
-      BLI_strncpy(r_date, "Yesterday", sizeof(*r_date) * FILELIST_DIRENTRY_DATE_LEN);
-      if (r_date_needs_translate) {
-        *r_date_needs_translate = true;
-      }
-    }
-    else {
-      strftime(r_date,
-               sizeof(*r_date) * FILELIST_DIRENTRY_DATE_LEN,
-               compact ? "%d/%m/%y" : "%d %b %Y",
-               tm);
-    }
+    strftime(r_date,
+             sizeof(*r_date) * FILELIST_DIRENTRY_DATE_LEN,
+             compact ? "%d/%m/%y" : "%d %b %Y",
+             tm);
+  }
+
+  if (r_is_today && (tm->tm_year == today_year) && (tm->tm_yday == today_yday)) {
+    *r_is_today = true;
+  }
+  else if (r_is_yesterday && (tm->tm_year == yesterday_year) && (tm->tm_yday == yesterday_yday)) {
+    *r_is_yesterday = true;
   }
 }
 
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index 36c04ccacef..c61d0e1e248 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -26,6 +26,7 @@
 #include <errno.h>
 
 #include "BLI_blenlib.h"
+#include "BLI_fileops_types.h"
 #include "BLI_utildefines.h"
 #include "BLI_math.h"
 
@@ -492,7 +493,7 @@ static void draw_columnheader_columns(const FileSelectParams *params,
 
     file_draw_string(sx + DETAILS_COLUMN_PADDING,
                      sy - layout->tile_border_y,
-                     column->name,
+                     IFACE_(column->name),
                      column->width - 2 * DETAILS_COLUMN_PADDING,
                      layout->columnheader_h - layout->tile_border_y,
                      UI_STYLE_TEXT_LEFT,
@@ -542,16 +543,17 @@ static const char *filelist_get_details_column_string(FileListColumns column,
     case COLUMN_DATETIME:
       if (!(file->typeflag & FILE_TYPE_BLENDERLIB) && !FILENAME_IS_CURRPAR(file->relpath)) {
         if ((file->entry->datetime_str[0] == '\0') || update_stat_strings) {
-          char date[16], time[8];
-          bool date_translate;
+          char date[FILELIST_DIRENTRY_DATE_LEN], time[FILELIST_DIRENTRY_TIME_LEN];
+          bool is_today, is_yesterday;
 
           BLI_filelist_entry_datetime_to_string(
-              NULL, file->entry->time, small_size, true, time, date, &date_translate);
-          BLI_snprintf(file->entry->datetime_str,
-                       sizeof(file->entry->datetime_str),
-                       "%s %s",
-                       date_translate ? IFACE_(date) : date,
-                       time);
+              NULL, file->entry->time, small_size, time, date, &is_today, &is_yesterday);
+
+          if (is_today || is_yesterday) {
+            BLI_strncpy(date, is_today ? N_("Today") : N_("Yesterday"), sizeof(date));
+          }
+          BLI_snprintf(
+              file->entry->datetime_str, sizeof(file->entry->datetime_str), "%s %s", date, time);
         }
 
         return file->entry->datetime_str;
@@ -604,7 +606,7 @@ static void draw_details_columns(const FileSelectParams *params,
     if (str) {
       file_draw_string(sx + DETAILS_COLUMN_PADDING,
                        sy - layout->tile_border_y,
-                       str,
+                       IFACE_(str),
                        column->width - 2 * DETAILS_COLUMN_PADDING,
                        layout->tile_h,
                        column->text_align,
diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c
index 7ae326eecd0..aaaf0703216 100644
--- a/source/blender/editors/space_file/filesel.c
+++ b/source/blender/editors/space_file/filesel.c
@@ -649,13 +649,13 @@ static void details_columns_init(const FileSelectParams *params, FileLayout *lay
 {
   details_columns_widths(params, layout);
 
-  layout->details_columns[COLUMN_NAME].name = IFACE_("Name");
+  layout->details_columns[COLUMN_NAME].name = N_("Name");
   layout->details_columns[COLUMN_NAME].sort_type = FILE_SORT_ALPHA;
   layout->details_columns[COLUMN_NAME].text_align = UI_STYLE_TEXT_LEFT;
-  layout->details_columns[COLUMN_DATETIME].name = IFACE_("Date Modified");
+  layout->details_columns[COLUMN_DATETIME].name = N_("Date Modified");
   layout->details_columns[COLUMN_DATETIME].sort_type = FILE_SORT_TIME;
   layout->details_columns[COLUMN_DATETIME].text_align = UI_STYLE_TEXT_LEFT;
-  layout->details_columns[COLUMN_SIZE].name = IFACE_("Size");
+  layout->details_columns[COLUMN_SIZE].name = N_("Size");
   layout->details_columns[COLUMN_SIZE].sort_type = FILE_SORT_SIZE;
   layout->details_columns[COLUMN_SIZE].text_align = UI_STYLE_TEXT_RIGHT;
 }



More information about the Bf-blender-cvs mailing list