diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2024-05-17 14:15:56 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2024-05-17 14:15:56 +0200 |
| commit | 6139da3d41b9e1c2eebd5f323bb0126775233d1b (patch) | |
| tree | c7c089d2330307e4fdb613b4a8545e66edf9eeec /core/text | |
| parent | b51eb53d041d55da908e2d079abdf5961345a6ab (diff) | |
Fix .mo contexts
Fixes #3590
- `get("key")`
- `get("context", "key")`
Diffstat (limited to 'core/text')
| -rw-r--r-- | core/text/i18n/gettext.odin | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/core/text/i18n/gettext.odin b/core/text/i18n/gettext.odin index 6b8f52861..35d355a40 100644 --- a/core/text/i18n/gettext.odin +++ b/core/text/i18n/gettext.odin @@ -60,10 +60,6 @@ parse_mo_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTIONS, plur translation.pluralize = pluralizer strings.intern_init(&translation.intern, allocator, allocator) - // Gettext MO files only have one section. - translation.k_v[""] = {} - section := &translation.k_v[""] - for n := u32(0); n < count; n += 1 { /* Grab string's original length and offset. @@ -83,37 +79,49 @@ parse_mo_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTIONS, plur max_offset := int(max(o_offset + o_length + 1, t_offset + t_length + 1)) if len(data) < max_offset { return translation, .Premature_EOF } - key := data[o_offset:][:o_length] - val := data[t_offset:][:t_length] + key_data := data[o_offset:][:o_length] + val_data := data[t_offset:][:t_length] /* Could be a pluralized string. */ zero := []byte{0} - - keys := bytes.split(key, zero) - vals := bytes.split(val, zero) + keys := bytes.split(key_data, zero); defer delete(keys) + vals := bytes.split(val_data, zero); defer delete(vals) if (len(keys) != 1 && len(keys) != 2) || len(vals) > MAX_PLURALS { return translation, .MO_File_Incorrect_Plural_Count } + section_name := "" for k in keys { - interned_key, _ := strings.intern_get(&translation.intern, string(k)) + key := string(k) + + // Scan for <context>EOT<key> + for ch, i in k { + if ch == 0x04 { + section_name = string(k[:i]) + key = string(k[i+1:]) + break + } + } + section_name, _ = strings.intern_get(&translation.intern, section_name) + if section_name not_in translation.k_v { + translation.k_v[section_name] = {} + } + interned_key, _ := strings.intern_get(&translation.intern, string(key)) interned_vals := make([]string, len(vals)) last_val: string - i := 0 - for v in vals { + for v, i in vals { interned_vals[i], _ = strings.intern_get(&translation.intern, string(v)) last_val = interned_vals[i] - i += 1 } + + section := &translation.k_v[section_name] section[interned_key] = interned_vals } - delete(vals) - delete(keys) } return } |