diff options
Diffstat (limited to 'src/lang.c')
-rw-r--r-- | src/lang.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lang.c b/src/lang.c new file mode 100644 index 00000000..b5f80fd1 --- /dev/null +++ b/src/lang.c | |||
@@ -0,0 +1,56 @@ | |||
1 | #include "lang.h" | ||
2 | |||
3 | #include <the_Foundation/sortedarray.h> | ||
4 | #include <the_Foundation/string.h> | ||
5 | |||
6 | iDeclareType(Lang) | ||
7 | iDeclareType(MsgStr) | ||
8 | |||
9 | struct Impl_MsgStr { | ||
10 | const char *id; | ||
11 | iString str; | ||
12 | }; | ||
13 | |||
14 | int cmp_MsgStr_(const void *e1, const void *e2) { | ||
15 | const iMsgStr *a = e1, *b = e2; | ||
16 | return iCmpStr(a->id, b->id); | ||
17 | } | ||
18 | |||
19 | /*----------------------------------------------------------------------------------------------*/ | ||
20 | |||
21 | struct Impl_Lang { | ||
22 | iSortedArray *messages; | ||
23 | }; | ||
24 | |||
25 | static iLang lang_; | ||
26 | |||
27 | void init_Lang(void) { | ||
28 | iLang *d = &lang_; | ||
29 | d->messages = new_SortedArray(sizeof(iMsgStr), cmp_MsgStr_); | ||
30 | setCurrent_Lang("en"); | ||
31 | } | ||
32 | |||
33 | void deinit_Lang(void) { | ||
34 | iLang *d = &lang_; | ||
35 | delete_SortedArray(d->messages); | ||
36 | } | ||
37 | |||
38 | void setCurrent_Lang(const char *language) { | ||
39 | /* TODO: Load compiled language strings from an embedded blob. */ | ||
40 | } | ||
41 | |||
42 | const iString *string_Lang(const char *msgId) { | ||
43 | const iLang *d = &lang_; | ||
44 | size_t pos; | ||
45 | const iMsgStr key = { .id = msgId }; | ||
46 | if (locate_SortedArray(d->messages, &key, &pos)) { | ||
47 | return &((const iMsgStr *) at_SortedArray(d->messages, pos))->str; | ||
48 | } | ||
49 | //iAssert(iFalse); | ||
50 | fprintf(stderr, "[Lang] missing: %s\n", msgId); | ||
51 | return collectNewCStr_String(msgId); | ||
52 | } | ||
53 | |||
54 | const char *cstr_Lang(const char *msgId) { | ||
55 | return cstr_String(string_Lang(msgId)); | ||
56 | } | ||