Page 1 of 1

Getting a UTF-8 error while trying to reverse and print Hebrew text

Posted: Wed Jun 10, 2020 11:19 am
by Walidor
Hey! I'm trying to implement a Hebrew translation to my game along my English one, and because of how lua works with rtl languages (which it really isn't) I have to reverse the text to print it correctly. So I tried using lua's built in string manipulation in string.reverse, but I'm getting this error while running and trying to print with love.graphics.printf on my main file with the love.draw function: "UTF-8 decoding error: Invalid UTF-8".

To clarify, I am using a font that has Hebrew chars, and tried printing it with manually reversing the text and it worked just fine. I'm using Visual Studio Code to write the game.
I know this can be conquered by just reversing manually or using a site to reverse long strings of text, but I would really like to be able to print Hebrew text semi-native-ly.

Thanks a lot :awesome:

Re: Getting a UTF-8 error while trying to reverse and print Hebrew text

Posted: Wed Jun 10, 2020 1:22 pm
by grump
It's because you're not reversing the text, you're reversing the codes that make up each character in the text. You have to take into account that UTF-8 uses multiple bytes to encode non-ASCII chars.

Code: Select all

local utf8 = require('utf8')
local function reverse(str)
	local len, result = utf8.len(str), {}
	for _, c in utf8.codes(str) do
		result[len] = utf8.char(c)
		len = len - 1
	end
	return table.concat(result)
end
(untested)

Re: Getting a UTF-8 error while trying to reverse and print Hebrew text

Posted: Thu Jun 11, 2020 6:26 am
by Walidor
Thanks! Implemented this into a class just to test this and it worked. I wanted to do something like this if it didn't work out but wasn't sure how exactly to pull it off.