iosswiftxcodeautomated-testsxcode-ui-testing

Detect right to left Language in Swift UITest


I am trying to setup my UITests for localisation. Especially for Arabic and any other Right to Left (RTL) language.

In my case I have a swipe(direction: SwipeDirection) function that takes a value of SwipeDirection (.up, .down, .left, .right).

If I now want to delete let's say a cell, I'd just do cell.swipe(.left) and my UITest would delete the cell. Since the UI is flipped on RTL languages this has to change to cell.swipe(.right).

I could not find any way of detecting the current device language, nor it's UILayoutDirection.

I've tried to check for it like this already:

let isLeftToRight = UIView.userInterfaceLayoutDirection(for: UIView().semanticContentAttribute) == .leftToRight

and also

let isLeftToRight = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight

But both of them always returning true, no matter the language. Are there any other ways of checking for the current UILayoutDirection I am not aware of?

Thank you!


Solution

  • After some deprecation from Apple, this is now a valid solution in SwiftUI. Declare the environment variable use:

    @Environment(\.layoutDirection) private var layoutDirection: LayoutDirection
    

    Then use the (private) var layoutDirection.