ui/common/properties.c

changeset 30
34513f76d5a8
parent 29
c96169444d88
child 33
458831c574f4
equal deleted inserted replaced
29:c96169444d88 30:34513f76d5a8
38 #include "../../ucx/properties.h" 38 #include "../../ucx/properties.h"
39 39
40 static UiProperties *application_properties; 40 static UiProperties *application_properties;
41 static UiProperties *language; 41 static UiProperties *language;
42 42
43 #ifndef UI_COCOA
44
45 static char *locales_dir;
46 static char *pixmaps_dir;
47
48 #endif
49
43 char* ui_getappdir() { 50 char* ui_getappdir() {
44 if(ui_appname() == NULL) { 51 if(ui_appname() == NULL) {
45 return NULL; 52 return NULL;
46 } 53 }
47 54
174 } 181 }
175 } 182 }
176 183
177 184
178 185
186 static char* uic_concat_path(char *base, char *p, char *ext) {
187 size_t baselen = strlen(base);
188
189 UcxBuffer *buf = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
190 if(baselen > 0) {
191 ucx_buffer_write(base, 1, baselen, buf);
192 if(base[baselen - 1] != '/') {
193 ucx_buffer_putc(buf, '/');
194 }
195 }
196 ucx_buffer_write(p, 1, strlen(p), buf);
197 if(ext) {
198 ucx_buffer_write(ext, 1, strlen(ext), buf);
199 }
200
201 char *str = buf->space;
202 free(buf);
203 return str;
204 }
205
179 #ifndef UI_COCOA 206 #ifndef UI_COCOA
180 207
181 void ui_load_locale(char *locale) { 208 void ui_locales_dir(char *path) {
182 char *basedir = ui_get_locale_basedir(); 209 locales_dir = path;
183 } 210 }
184 211
185 #endif 212 void ui_pixmaps_dir(char *path) {
186 213 pixmaps_dir = path;
187 void uic_load_language_file(char *path) { 214 }
215
216 void ui_load_lang(char *locale) {
217 if(!locale) {
218 locale = "en_EN";
219 }
220
221 char *path = uic_concat_path(locales_dir, locale, ".properties");
222
223 uic_load_language_file(path);
224 free(path);
225 }
226
227 void ui_load_lang_def(char *locale, char *default_locale) {
228 char tmp[6];
229 if(!locale) {
230 char *lang = getenv("LANG");
231 if(lang && strlen(lang) >= 5) {
232 memcpy(tmp, lang, 5);
233 tmp[5] = '\0';
234 locale = tmp;
235 } else {
236 locale = default_locale;
237 }
238 }
239
240 char *path = uic_concat_path(locales_dir, locale, ".properties");
241
242 if(uic_load_language_file(path)) {
243 if(default_locale) {
244 ui_load_lang_def(default_locale, NULL);
245 } else {
246 // cannot find any language file
247 fprintf(stderr, "Ui Error: Cannot load language.\n");
248 free(path);
249 exit(-1);
250 }
251 }
252 free(path);
253 }
254
255 #endif
256
257 int uic_load_language_file(char *path) {
188 UcxMap *lang = ucx_map_new(256); 258 UcxMap *lang = ucx_map_new(256);
189 259
190 printf("path: %s\n", path);
191 FILE *file = fopen(path, "r"); 260 FILE *file = fopen(path, "r");
192 if(!file) { 261 if(!file) {
193 printf("cannot open file: %s\n", path); 262 return 1;
194 free(path);
195 return;
196 } 263 }
197 264
198 if(ucx_properties_load(lang, file)) { 265 if(ucx_properties_load(lang, file)) {
199 fprintf(stderr, "Ui Error: Cannot load locale.\n"); 266 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path);
200 } 267 }
201 268
202 fclose(file); 269 fclose(file);
203 270
204 ucx_map_rehash(lang); 271 ucx_map_rehash(lang);
205 272
206 language = lang; 273 language = lang;
274
275 return 0;
207 } 276 }
208 277
209 char* uistr(char *name) { 278 char* uistr(char *name) {
279 char *value = uistr_n(name);
280 return value ? value : "missing string";
281 }
282
283 char* uistr_n(char *name) {
210 if(!language) { 284 if(!language) {
211 return "missing string"; 285 return NULL;
212 } 286 }
213 char *value = ucx_map_cstr_get(language, name); 287 return ucx_map_cstr_get(language, name);
214 if(!value) { 288 }
215 return "missing string";
216 } else {
217 return value;
218 }
219 }

mercurial