diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-03-24 11:46:19 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-03-24 11:46:19 +0200 |
commit | 5aa5f10fdb386d716d86066b5e392037ffe13914 (patch) | |
tree | 1021ef11bc7344ffcd4de752f63725cd210b8c4c /src/lang.c | |
parent | 86b220841276fc3ddcdebf1eb4bf6543e9aa39b7 (diff) |
Fixes for dynamic language switching
IssueID #192
Diffstat (limited to 'src/lang.c')
-rw-r--r-- | src/lang.c | 36 |
1 files changed, 25 insertions, 11 deletions
@@ -66,16 +66,24 @@ void setCurrent_Lang(const char *language) { | |||
66 | load_Lang_(d, language); | 66 | load_Lang_(d, language); |
67 | } | 67 | } |
68 | 68 | ||
69 | iRangecc range_Lang(iRangecc msgId) { | 69 | static iBool find_Lang_(iRangecc msgId, iRangecc *str_out) { |
70 | const iLang *d = &lang_; | 70 | const iLang *d = &lang_; |
71 | size_t pos; | 71 | size_t pos; |
72 | const iMsgStr key = { .id = msgId }; | 72 | const iMsgStr key = { .id = msgId }; |
73 | if (locate_SortedArray(d->messages, &key, &pos)) { | 73 | if (locate_SortedArray(d->messages, &key, &pos)) { |
74 | return ((const iMsgStr *) at_SortedArray(d->messages, pos))->str; | 74 | *str_out = ((const iMsgStr *) at_SortedArray(d->messages, pos))->str; |
75 | return iTrue; | ||
75 | } | 76 | } |
76 | fprintf(stderr, "[Lang] missing: %s\n", cstr_Rangecc(msgId)); fflush(stderr); | 77 | fprintf(stderr, "[Lang] missing: %s\n", cstr_Rangecc(msgId)); fflush(stderr); |
77 | // iAssert(iFalse); | 78 | // iAssert(iFalse); |
78 | return msgId; | 79 | *str_out = msgId; |
80 | return iFalse; | ||
81 | } | ||
82 | |||
83 | iRangecc range_Lang(iRangecc msgId) { | ||
84 | iRangecc str; | ||
85 | find_Lang_(msgId, &str); | ||
86 | return str; | ||
79 | } | 87 | } |
80 | 88 | ||
81 | const char *cstr_Lang(const char *msgId) { | 89 | const char *cstr_Lang(const char *msgId) { |
@@ -96,13 +104,19 @@ void translate_Lang(iString *textWithIds) { | |||
96 | id.start += 2; | 104 | id.start += 2; |
97 | id.end = strchr(id.start, '}'); | 105 | id.end = strchr(id.start, '}'); |
98 | iAssert(id.end != NULL); | 106 | iAssert(id.end != NULL); |
99 | const size_t idLen = size_Range(&id); | 107 | const size_t idLen = size_Range(&id); |
100 | const iRangecc replacement = range_Lang(id); | 108 | iRangecc replacement; |
101 | const size_t startPos = id.start - cstr_String(textWithIds) - 2; | 109 | const size_t startPos = id.start - cstr_String(textWithIds) - 2; |
102 | /* Replace it. */ | 110 | if (find_Lang_(id, &replacement)) { |
103 | remove_Block(&textWithIds->chars, startPos, idLen + 3); | 111 | /* Replace it. */ |
104 | insertData_Block(&textWithIds->chars, startPos, replacement.start, size_Range(&replacement)); | 112 | remove_Block(&textWithIds->chars, startPos, idLen + 3); |
105 | pos = cstr_String(textWithIds) + startPos + size_Range(&replacement); | 113 | insertData_Block(&textWithIds->chars, startPos, replacement.start, size_Range(&replacement)); |
114 | pos = cstr_String(textWithIds) + startPos + size_Range(&replacement); | ||
115 | } | ||
116 | else { | ||
117 | remove_Block(&textWithIds->chars, startPos, 1); /* skip on subsequent attempts */ | ||
118 | pos = cstr_String(textWithIds) + startPos + idLen; | ||
119 | } | ||
106 | } | 120 | } |
107 | } | 121 | } |
108 | 122 | ||