simnik
simnik

Reputation: 483

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!

Upvotes: 3

Views: 1566

Answers (2)

Luca_65
Luca_65

Reputation: 137

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.

Upvotes: 1

adamfowlerphoto
adamfowlerphoto

Reputation: 2751

Not exactly that hard to find in the documentation

let locale = NSLocale.autoupdatingCurrent
let lang = locale.languageCode
let direction = NSLocale.characterDirection(forLanguage:lang!)

Upvotes: 5

Related Questions