diff options
Diffstat (limited to 'src/lang.c')
-rw-r--r-- | src/lang.c | 40 |
1 files changed, 31 insertions, 9 deletions
@@ -1,4 +1,5 @@ | |||
1 | #include "lang.h" | 1 | #include "lang.h" |
2 | #include "embedded.h" | ||
2 | 3 | ||
3 | #include <the_Foundation/sortedarray.h> | 4 | #include <the_Foundation/sortedarray.h> |
4 | #include <the_Foundation/string.h> | 5 | #include <the_Foundation/string.h> |
@@ -7,8 +8,8 @@ iDeclareType(Lang) | |||
7 | iDeclareType(MsgStr) | 8 | iDeclareType(MsgStr) |
8 | 9 | ||
9 | struct Impl_MsgStr { | 10 | struct Impl_MsgStr { |
10 | const char *id; | 11 | const char *id; /* these point to null-terminated strings in embedded data */ |
11 | iString str; | 12 | const char *str; |
12 | }; | 13 | }; |
13 | 14 | ||
14 | int cmp_MsgStr_(const void *e1, const void *e2) { | 15 | int cmp_MsgStr_(const void *e1, const void *e2) { |
@@ -24,6 +25,24 @@ struct Impl_Lang { | |||
24 | 25 | ||
25 | static iLang lang_; | 26 | static iLang lang_; |
26 | 27 | ||
28 | static void clear_Lang_(iLang *d) { | ||
29 | clear_SortedArray(d->messages); | ||
30 | } | ||
31 | |||
32 | static void load_Lang_(iLang *d, const char *id) { | ||
33 | /* Load compiled language strings from an embedded blob. */ | ||
34 | const iBlock *data = NULL; // &blobLangEn_Embedded; | ||
35 | iMsgStr msg; | ||
36 | for (const char *ptr = constBegin_Block(data); ptr != constEnd_Block(data); ptr++) { | ||
37 | msg.id = ptr; | ||
38 | while (*++ptr) {} | ||
39 | msg.str = ++ptr; | ||
40 | while (*++ptr) {} | ||
41 | /* Allocate the string. */ | ||
42 | insert_SortedArray(d->messages, &msg); | ||
43 | } | ||
44 | } | ||
45 | |||
27 | void init_Lang(void) { | 46 | void init_Lang(void) { |
28 | iLang *d = &lang_; | 47 | iLang *d = &lang_; |
29 | d->messages = new_SortedArray(sizeof(iMsgStr), cmp_MsgStr_); | 48 | d->messages = new_SortedArray(sizeof(iMsgStr), cmp_MsgStr_); |
@@ -32,25 +51,28 @@ void init_Lang(void) { | |||
32 | 51 | ||
33 | void deinit_Lang(void) { | 52 | void deinit_Lang(void) { |
34 | iLang *d = &lang_; | 53 | iLang *d = &lang_; |
54 | clear_Lang_(d); | ||
35 | delete_SortedArray(d->messages); | 55 | delete_SortedArray(d->messages); |
36 | } | 56 | } |
37 | 57 | ||
38 | void setCurrent_Lang(const char *language) { | 58 | void setCurrent_Lang(const char *language) { |
39 | /* TODO: Load compiled language strings from an embedded blob. */ | 59 | iLang *d = &lang_; |
60 | clear_Lang_(d); | ||
61 | load_Lang_(d, language); | ||
40 | } | 62 | } |
41 | 63 | ||
42 | const iString *string_Lang(const char *msgId) { | 64 | const char *cstr_Lang(const char *msgId) { |
43 | const iLang *d = &lang_; | 65 | const iLang *d = &lang_; |
44 | size_t pos; | 66 | size_t pos; |
45 | const iMsgStr key = { .id = msgId }; | 67 | const iMsgStr key = { .id = iConstCast(char *, msgId) }; |
46 | if (locate_SortedArray(d->messages, &key, &pos)) { | 68 | if (locate_SortedArray(d->messages, &key, &pos)) { |
47 | return &((const iMsgStr *) at_SortedArray(d->messages, pos))->str; | 69 | return ((const iMsgStr *) at_SortedArray(d->messages, pos))->str; |
48 | } | 70 | } |
49 | //iAssert(iFalse); | 71 | //iAssert(iFalse); |
50 | fprintf(stderr, "[Lang] missing: %s\n", msgId); | 72 | fprintf(stderr, "[Lang] missing: %s\n", msgId); |
51 | return collectNewCStr_String(msgId); | 73 | return msgId; |
52 | } | 74 | } |
53 | 75 | ||
54 | const char *cstr_Lang(const char *msgId) { | 76 | const iString *string_Lang(const char *msgId) { |
55 | return cstr_String(string_Lang(msgId)); | 77 | return collectNewCStr_String(cstr_Lang(msgId)); |
56 | } | 78 | } |