Unicode bidi support

My first message here.

I'm interested in the support Swift offers for RTL languages and more generally the bidi(rectional) world.

Does it offer something similar to the Android's BidiFormatter class (BidiFormatter  |  Android Developers) ?

Is the Unicode Bidirectional Algorithm (UBA) implemented in Swift? In part? Or is that purely a rendering issue? I'm interesting in knowing what level of the UBA is implemented and whether UBA 6.3 Directional isolate characters are supported. Would that be an issue which would need to be asked in some other forum? If so which one?

Swift’s String type is explicitly Unicode, so all the low level stuff is there and you don’t need to worry about things like your bidirectional control characters (U+2066, etc) getting split in half by bytewise UTF‐8 processing. When I construct a bidirectional string and print it to the macOS Terminal, it appears correctly.

However, as you suspected, bidirection is mostly a rendering issue, which is at a much higher level than the Swift language itself. The macOS and iOS user interface frameworks are tailored for use in Swift and support bidirectional display in everything from text fields to window alignment to the menu bar direction. My Mac spends one fifth of the time set to Hebrew and everything works fine. For the most part (but with exceptions), programming for a bidirectional audience is a matter of simply ignoring it and letting the operating system do it for you.

Bidirectional Swift source code (or just string literals) is at a much more dismal state. I have to write source in Hebrew from time to time and it is orders of magnitude more annoying than when I have to write German source. The necessity of mixing left‐to‐right tokens (like let or func) with right‐to‐left stuff in an environment replete with neutral punctuation and where every line is a new paragraph as far as the bidirectional algorithm is concerned... results in illegible soup. I find I am endlessly interrupting the code with comments like /*א*/ just to reorder the line and keep the source halfways readable. And even then Xcode justifies the text backwards and puts the indentation on the wrong end... It would be nice to have an IDE that first parsed the source and then applied the bidirectional algorithm as though each syntax node were an isolate, but I don’t foresee that happening any time soon.

I haven’t used the BidiFormatter class, but from the documentation it looks like it just does approximately this:

extension String {
    func preparedForUnknownInterpolation() -> String {
        return "\u{2068}\(self)\u{2069}"
    }
}

Thank you for your input, I fully sympathizes about your troubles with Bidi Swift source code.

That being said, we're interested in adding the Bidi properties to Unicode.Scalar.Properties, mentioned in this post. That's a lower-level representation, but necessary for bidi processing IIUC. If you're interested in pitching that, I can help!

1 Like