How would I re-order text so that this:
would be look like this:
السفر عبر الزمن إلى 31/12/1999
I know there’s (U+200E) right-to-left mark character in the string.
Here’s what I’ve tried
function m.Text(txt)
local ret = '';
local rtl = false;
for _, c in utf8.codes(txt) do
if c == 8207 then
rtl = not rtl;
else
if rtl then
ret = utf8.char(c) .. ret;
else
ret = ret .. utf8.char(c);
end;
end;
end;
return ret;
end;
Would using string.reverse work? Never used it, so I’m not sure.
string.reverse("السفر عبر الزمن إلى 31/12/1999") --Try this
Edit: Oops, I put the backwards version that you want in here. I cannot copy and paste an image into text, so just replace the normal version with the string I put in the code.
I’m not so sure about the Arabic text but at the very least the “13 21/9991/” part is in reverse, so string.reverse would switch the order for it: only problem is that it doesn’t support utf8 so the Arabic text would end up getting obscured into unrecognised symbols.
Out of curiosity, how are you ending up with a situation like this where input text is backwards? Is this arbitrary or do you have some kind of system that intentionally generates it backwards and you need to flip it around for? It seems like you could just fix this by typing it out properly.
Alright. Circumstances aside, I think I have figured something out. Since the raw text was in a screenshot therefore I couldn’t copy, I tried the reversing the proper text. If it works one way, it will the other.
After working with (and learning about, since I have never touched the utf8 library!) some sample code, I think I’ve figured things out. The key here is that you ideally want to be doing your operations in reverse, whether that’s iterating or stacking characters up.
Essentially what I ended up doing was going through all the codepoints in the text (all characters are identified as utf8, I thought it’d just be the Arabic text initially!) and I did backwards concatenation. That is, for every code point, I set a variable to that and then concatenate the rest of the string on.
local raw = "السفر عبر الزمن إلى 31/12/1999"
local reversed = ""
for _, codepoint in utf8.codes(raw) do
reversed = utf8.char(codepoint) .. reversed
end
print(raw) -- السفر عبر الزمن إلى 31/12/1999
print(reversed) -- 9991/21/13 ىلإ نمزلا ربع رفسلا
This looks to be what you are attempting to achieve, so give that one a shot. The thing here though is that the text you have in the box and what the text should be are not equal to each other. It’s not a reversed operation, so the output you get will be different.
I gave a shot at this, but the reversed it returns this in the UI:
Numbers go like 9 then 01 then 11 thern 21 then 31… 91 then 02 when reading right to left but goes like 9 then 10 then 11 then 12 then 13… 19 then 20, when reading left to right thus The RTL mark is inserted (or at least according to CLDR, that’s how date should output in Arabic). Now the word is in the correct word order but not the characters.
I am attemping to achieve that so text will be readable by both directions.
Perhaps splitting text by LTR characters, but how would I know which is LTR or RTL?
The problem gets even worse as I want CJK lanauges dialogues for my Roblox game to look like this in some cases:
You need to flip that operation around in your Gui. As you did not provide the text you were trying to reverse as copyable text and only what it should have looked like, I did the operation in order by reversing the proper text as a test.
I was expecting that you would salvage the code, not copy it, and apply it to your code. That is, when faced with that unarranged text, that becomes “raw” and the reversed text is what it the text should come out to be. So as a result, your text is still coming out in the reversed order, not the proper one.
Is your text in proper letter ordering but not the proper word order? Is that 9991 intended or were you writing out a date that should be 1999? This is where I’m getting confused which is why I thought you were trying to do a reversal of text more than anything.
In ar_001 locale, the text originally (the raw text, not revsered) are in the correct letter order (e.g. AB) but not the word order (e.g. CD AB instead of AB CD)
I’m trying to fix this UI issue:
until Roblox fixes this, reversing text seems to be the only solution for this.
In a nutshell:
I want to keep the text-direction to LTR if it’s a LTR character (including numbers)
Thank you for the clarification! It might’ve been me who got you confused because of my own confusion, so I apologise for that. In that case, though: I’ve got nothing.
The next suggestion I intended to offer was to split the text up into chunks with whitespace ignored and just getting all parts of the text so you can control ordering via iteration, but considering this seems to be arbitrary, that suggestion may not work out.
I’m not fully sure how you would accomplish cases 1 and 3, let alone case 2.