I figured out the issue. What DanielH mentioned is right, the code is also putting the key without altgr together, I need to make sure it doesn't.
The issue is this: it wasn't garbage. What happens is that in UTF-8, those non-ASCII characters are usually represented by 2 bytes, instead of just one in the case of ASCII. But then, I didn't account the extra length of such characters with the caret. In the end, the caret would then insert data IN BETWEEN the two bytes of the non-ASCII UTF-8 characters, resulting in usually unreadable characters, and corrupting the rest of the string for matters of rendering (or maybe rendering characters that the font didn't have).
I modified this code a bit too much to quote, but in short, it revolves around using al_ustr_offset. Whenever I need a specific position in the string, regarding characters, not bytes, using al_ustr_offset will count the characters in the string, not the bytes. So the caret logic stays the same, but al_ustr_insert_chr(text_typed, caret_position, event.keyboard.unichar); should use al_ustr_offset(text_typed,caret_position) instead of just caret_position.