added locale support (Motif)

Fri, 11 Apr 2014 17:02:19 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 11 Apr 2014 17:02:19 +0200
changeset 30
34513f76d5a8
parent 29
c96169444d88
child 31
34df61c6ec3c

added locale support (Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/cocoa/resource.m file | annotate | diff | comparison | revisions
ui/cocoa/stock.m file | annotate | diff | comparison | revisions
ui/common/properties.c file | annotate | diff | comparison | revisions
ui/common/properties.h file | annotate | diff | comparison | revisions
ui/gtk/menu.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/motif/stock.c file | annotate | diff | comparison | revisions
ui/motif/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/properties.h file | annotate | diff | comparison | revisions
ui/ui/stock.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Thu Apr 10 11:37:41 2014 +0200
+++ b/application/main.c	Fri Apr 11 17:02:19 2014 +0200
@@ -155,7 +155,8 @@
 
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
-    ui_load_locale(NULL);
+    ui_locales_dir("/opt/app1/locales");
+    ui_load_lang_def(NULL, "en_EN");
     ui_openfilefunc(action_new, NULL);
     
     
--- a/ui/cocoa/resource.m	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/cocoa/resource.m	Fri Apr 11 17:02:19 2014 +0200
@@ -34,7 +34,7 @@
 
 
 
-void ui_load_locale(char *locale) {
+void ui_load_lang(char *locale) {
     NSString *localeString = nil;
     if(locale) {
         localeString = [[NSString alloc]initWithUTF8String:locale];
--- a/ui/cocoa/stock.m	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/cocoa/stock.m	Fri Apr 11 17:02:19 2014 +0200
@@ -62,5 +62,13 @@
 }
 
 UiStockItem* ui_get_stock_item(char *stock_id) {
-    return ucx_map_cstr_get(stock_items, stock_id);
+    UiStockItem *item = ucx_map_cstr_get(stock_items, stock_id);
+    if(item) {
+        char *label = uistr_n(stock_id);
+        if(label) {
+            NSString *str = [[NSString alloc]initWithUTF8String:label];
+            item->label = str;
+        }
+    }
+    return item;
 }
--- a/ui/common/properties.c	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/common/properties.c	Fri Apr 11 17:02:19 2014 +0200
@@ -40,6 +40,13 @@
 static UiProperties *application_properties;
 static UiProperties *language;
 
+#ifndef UI_COCOA
+
+static char *locales_dir;
+static char *pixmaps_dir;
+
+#endif
+
 char* ui_getappdir() {
     if(ui_appname() == NULL) {
         return NULL;
@@ -176,27 +183,87 @@
 
 
 
+static char* uic_concat_path(char *base, char *p, char *ext) {
+    size_t baselen = strlen(base);
+    
+    UcxBuffer *buf = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
+    if(baselen > 0) {
+        ucx_buffer_write(base, 1, baselen, buf);
+        if(base[baselen - 1] != '/') {
+            ucx_buffer_putc(buf, '/');
+        }
+    }
+    ucx_buffer_write(p, 1, strlen(p), buf);
+    if(ext) {
+        ucx_buffer_write(ext, 1, strlen(ext), buf);
+    }
+    
+    char *str = buf->space;
+    free(buf);
+    return str;
+}
+
 #ifndef UI_COCOA
 
-void ui_load_locale(char *locale) {
-    char *basedir = ui_get_locale_basedir();
+void ui_locales_dir(char *path) {
+    locales_dir = path;
+}
+
+void ui_pixmaps_dir(char *path) {
+    pixmaps_dir = path;
+}
+
+void ui_load_lang(char *locale) {
+    if(!locale) {
+        locale = "en_EN";
+    }
+    
+    char *path = uic_concat_path(locales_dir, locale, ".properties");
+    
+    uic_load_language_file(path);
+    free(path);
+}
+
+void ui_load_lang_def(char *locale, char *default_locale) {
+    char tmp[6];
+    if(!locale) {
+        char *lang = getenv("LANG");
+        if(lang && strlen(lang) >= 5) {
+            memcpy(tmp, lang, 5);
+            tmp[5] = '\0';
+            locale = tmp;
+        } else {
+            locale = default_locale;
+        }
+    }
+    
+    char *path = uic_concat_path(locales_dir, locale, ".properties");
+    
+    if(uic_load_language_file(path)) {
+        if(default_locale) {
+            ui_load_lang_def(default_locale, NULL);
+        } else {
+            // cannot find any language file
+            fprintf(stderr, "Ui Error: Cannot load language.\n");
+            free(path);
+            exit(-1);
+        }
+    }
+    free(path);
 }
 
 #endif
 
-void uic_load_language_file(char *path) {
+int uic_load_language_file(char *path) {
     UcxMap *lang = ucx_map_new(256);
     
-    printf("path: %s\n", path);
     FILE *file = fopen(path, "r");
     if(!file) {
-        printf("cannot open file: %s\n", path);
-        free(path);
-        return;
+        return 1;
     }
     
     if(ucx_properties_load(lang, file)) {
-        fprintf(stderr, "Ui Error: Cannot load locale.\n");
+        fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path);
     }
     
     fclose(file);
@@ -204,16 +271,18 @@
     ucx_map_rehash(lang);
     
     language = lang;
+    
+    return 0;
 }
 
 char* uistr(char *name) {
+    char *value = uistr_n(name);
+    return value ? value : "missing string";
+}
+
+char* uistr_n(char *name) {
     if(!language) {
-        return "missing string";
+        return NULL;
     }
-    char *value = ucx_map_cstr_get(language, name);
-    if(!value) {
-        return "missing string";
-    } else {
-        return value;
-    }
+    return ucx_map_cstr_get(language, name);
 }
--- a/ui/common/properties.h	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/common/properties.h	Fri Apr 11 17:02:19 2014 +0200
@@ -46,7 +46,7 @@
 void uic_load_app_properties();
 void uic_store_app_properties();
 
-void uic_load_language_file(char *path);
+int uic_load_language_file(char *path);
     
 #ifdef	__cplusplus
 }
--- a/ui/gtk/menu.c	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/gtk/menu.c	Fri Apr 11 17:02:19 2014 +0200
@@ -34,6 +34,7 @@
 #include "menu.h"
 #include "toolkit.h"
 #include "../common/context.h"
+#include "../ui/properties.h"
 #include "../ui/window.h"
 
 static UcxList *menus;
--- a/ui/gtk/window.c	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/gtk/window.c	Fri Apr 11 17:02:19 2014 +0200
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "../ui/window.h"
 #include "../ui/properties.h"
--- a/ui/motif/stock.c	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/motif/stock.c	Fri Apr 11 17:02:19 2014 +0200
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 
 #include "stock.h"
+#include "../ui/properties.h"
 #include "../../ucx/map.h"
 
 static UcxMap *stock_items;
@@ -63,5 +64,12 @@
 }
 
 UiStockItem* ui_get_stock_item(char *id) {
-    return ucx_map_cstr_get(stock_items, id);
+    UiStockItem *item = ucx_map_cstr_get(stock_items, id);
+    if(item) {
+        char *label = uistr_n(id);
+        if(label) {
+            item->label = label;
+        }
+    }
+    return item;
 }
--- a/ui/motif/toolkit.c	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/motif/toolkit.c	Fri Apr 11 17:02:19 2014 +0200
@@ -59,6 +59,10 @@
     app = XtCreateApplicationContext();
     
     display =  XtOpenDisplay(app, NULL, appname, appname, NULL, 0, &argc, argv);
+    char **missing = NULL;
+    int nm = 0;
+    char *def = NULL;
+    XCreateFontSet(display, "-dt-interface system-medium-r-normal-s*utf*", &missing, &nm, &def);
     
     uic_docmgr_init();
     ui_toolbar_init();
--- a/ui/ui/properties.h	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/ui/properties.h	Fri Apr 11 17:02:19 2014 +0200
@@ -45,9 +45,14 @@
 
 void ui_set_default_property(char *name, char *value);
 
-void ui_load_locale(char *locale);
+void ui_locales_dir(char *path);
+void ui_pixmaps_dir(char *path);
+
+void ui_load_lang(char *locale);
+void ui_load_lang_def(char *locale, char *default_locale);
     
 char* uistr(char *name);
+char* uistr_n(char *name);
 
 #ifdef	__cplusplus
 }
--- a/ui/ui/stock.h	Thu Apr 10 11:37:41 2014 +0200
+++ b/ui/ui/stock.h	Fri Apr 11 17:02:19 2014 +0200
@@ -38,22 +38,22 @@
 // motif stock ids
 #if UI_MOTIF || UI_COCOA
     
-#define UI_STOCK_NEW                    "uiNew"
-#define UI_STOCK_OPEN                   "uiOpen"
-#define UI_STOCK_SAVE                   "uiSave"
-#define UI_STOCK_SAVE_AS                "uiSaveAs"
-#define UI_STOCK_REVERT_TO_SAVED        "uiRevertToSaved"
-#define UI_STOCK_GO_BACK                "uiGoBack"
-#define UI_STOCK_GO_FORWARD             "uiGoForward"
-#define UI_STOCK_ADD                    "uiAdd"
-#define UI_STOCK_CLOSE                  "uiClose"
+#define UI_STOCK_NEW                    "ui.stock.New"
+#define UI_STOCK_OPEN                   "ui.stock.Open"
+#define UI_STOCK_SAVE                   "ui.stock.Save"
+#define UI_STOCK_SAVE_AS                "ui.stock.SaveAs"
+#define UI_STOCK_REVERT_TO_SAVED        "ui.stock.RevertToSaved"
+#define UI_STOCK_GO_BACK                "ui.stock.GoBack"
+#define UI_STOCK_GO_FORWARD             "ui.stock.GoForward"
+#define UI_STOCK_ADD                    "ui.stock.Add"
+#define UI_STOCK_CLOSE                  "ui.stock.Close"
     
-#define UI_STOCK_UNDO                   "uiUndo"
-#define UI_STOCK_REDO                   "uiRedo"
-#define UI_STOCK_CUT                    "uiCut"
-#define UI_STOCK_COPY                   "uiCopy"
-#define UI_STOCK_PASTE                  "uiPaste"
-#define UI_STOCK_DELETE                 "uiDelete"
+#define UI_STOCK_UNDO                   "ui.stock.Undo"
+#define UI_STOCK_REDO                   "ui.stock.Redo"
+#define UI_STOCK_CUT                    "ui.stock.Cut"
+#define UI_STOCK_COPY                   "ui.stock.Copy"
+#define UI_STOCK_PASTE                  "ui.stock.Paste"
+#define UI_STOCK_DELETE                 "ui.stock.Delete"
     
 #endif
     

mercurial