ui/common/properties.c

changeset 30
34513f76d5a8
parent 29
c96169444d88
child 33
458831c574f4
--- 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);
 }

mercurial