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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Walidor
Prole
Posts: 2
Joined: Wed Jun 10, 2020 11:07 am

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

Post 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:
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

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

Post 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)
Walidor
Prole
Posts: 2
Joined: Wed Jun 10, 2020 11:07 am

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

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 86 guests