mamaral / Neon

A powerful Swift programmatic UI layout framework.
MIT License
4.58k stars 389 forks source link

Support right-to-left languages #55

Open MontakOleg opened 7 years ago

MontakOleg commented 7 years ago

I think a little bit about that. As a basis idea we can take AutoLayout approach: .leading means .left on left-to-right languages and means .right on right-to-left languages; .trailing means .right on left-to-right languages and means .left on right-to-left languages.

We can implement this by simple adding factory functions to our enums(Edge, Corner, Align), for example for Edge:

public enum Edge {
    case top
    case left
    case bottom
    case right

    static func leading() -> Edge {
        return isRightToLeft() ? .right : .left
    }

    static func trailing() -> Edge {
        return isRightToLeft() ? .left : .right
    }
}

Then if client code wants to respect language direction, it should use .leading() and .trailing() functions instead .left and .right:

func isRightToLeft() -> Bool {
//    return UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft
    return true
}

func bar(edge: Edge) {
    switch edge {
    case .left:
        print("left")
    case .right:
        print("right")
    case .top:
        print("top")
    case .bottom:
        print("bottom")
    }
}

bar(edge: .leading()) // prints 'right'

@mamaral what you think about this approach? Any suggestions?