3

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!

smnk
  • 471
  • 1
  • 6
  • 25

2 Answers2

5

Not exactly that hard to find in the documentation

let locale = NSLocale.autoupdatingCurrent
let lang = locale.languageCode
let direction = NSLocale.characterDirection(forLanguage:lang!)
adamfowlerphoto
  • 2,708
  • 1
  • 11
  • 24
  • I am going to use that for now. I think I saw a WWDC video once about the locale and why you should not use it for that. Will look it up, maybe I'm wrong though. Thank you! – smnk Apr 24 '17 at 08:50
1

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.

Luca_65
  • 123
  • 9