In my Mac app the language is chosen on the first screen, and I needed to add Arabic to it. Changing the app language without a restart had worked on macOS for Russian and English for a long time. With Arabic, the interface mirrored only halfway. Text moved to the right, while the sidebar, the window buttons and some of the elements stayed on the left. Switching back to Russian broke it in the opposite direction: the text and the sidebar stayed on the right. It only became correct after a restart. Below: where macOS remembers the writing direction, how to get around that, and which approaches did not work.
Why the layout mirrors only halfway#
macOS works out the app's writing direction once, when the process launches, from the AppleTextDirection and NSForceRightToLeftWritingDirection settings, and reads it from a cache after that. If you write those settings later, nothing changes until a restart. Three things take their direction from that cache:
| What | When it reads | What you see after switching |
|---|---|---|
The leading/trailing side of a constraint | at the moment the constraint is installed | the old side |
.natural alignment and input direction | on every redraw | the old side |
| Window buttons and the title | a separate system UI cache | the old side |
NSStackView and checkboxes rebuild themselves when a view's userInterfaceLayoutDirection changes. That is why the screen looks half mirrored: the stacks moved, everything else did not.
I measured this in a test window. A label with .natural alignment was drawn at x = 3 instead of 357 after switching to Arabic, and a view pinned to leading sat at x = 10 instead of 440.
There is no public way to reset this cache. Apple's standard path is different: you pick the language for an individual app in System Settings, and the app restarts. Chrome and Firefox on Mac also ask for a restart after you change the interface language.
What I did to change the app language without a restart#
An automatic restart on every LTR ↔ RTL switch works reliably but loses the screen state, so I kept it only as a fallback. The main solution has five parts.
1. The language is chosen before the app is created#
Choosing the language in applicationDidFinishLaunching is already too late: the first launch straight into Arabic also comes out half mirrored. So the language is chosen in main.swift, before NSApplication.shared:
import AppKit
LanguageStore.shared.restoreSavedLanguage() // writes AppleTextDirection and the language
WritingDirectionOverride.install()
let app = MirroringApplication.shared
let delegate = AppDelegate()
app.delegate = delegate
app.run()2. The current direction is readable from any thread#
AppKit asks for the direction from threads other than the main one, too. In my case it was PDF drawing in a background task. If the direction lives in a @MainActor object, Swift 6 stops the process on a read from a background thread. So the direction is kept in a separate cell behind a lock:
import AppKit
enum AppDirection {
private static let lock = NSLock()
nonisolated(unsafe) private static var stored: NSUserInterfaceLayoutDirection = .leftToRight
nonisolated(unsafe) private static var atLaunch: NSUserInterfaceLayoutDirection?
static var current: NSUserInterfaceLayoutDirection {
lock.lock(); defer { lock.unlock() }
return stored
}
/// The direction AppKit cached when the process launched.
static var launch: NSUserInterfaceLayoutDirection {
lock.lock(); defer { lock.unlock() }
return atLaunch ?? stored
}
static func set(_ direction: NSUserInterfaceLayoutDirection) {
lock.lock(); defer { lock.unlock() }
stored = direction
if atLaunch == nil { atLaunch = direction }
}
}An NSApplication subclass hands this direction to views that have none of their own:
final class MirroringApplication: NSApplication {
nonisolated override var userInterfaceLayoutDirection: NSUserInterfaceLayoutDirection {
AppDirection.current
}
}This subclass alone is not enough: it fixed only some of the texts, while constraints and Latin text stayed on the old side.
3. Replacing the writing direction cache#
The cache lives in the private class method +[NSParagraphStyle _defaultWritingDirection]. If you replace its implementation, it answers according to the current language, and text, input and new constraints immediately land on the right side:
import AppKit
import ObjectiveC
enum WritingDirectionOverride {
static func install() {
guard let metaclass = object_getClass(NSParagraphStyle.self),
let method = class_getInstanceMethod(metaclass, NSSelectorFromString("_defaultWritingDirection"))
else { return } // no such method in this macOS version: behave as before
let answer: @convention(block) (AnyObject) -> Int = { _ in
AppDirection.current == .rightToLeft
? NSWritingDirection.rightToLeft.rawValue
: NSWritingDirection.leftToRight.rawValue
}
method_setImplementation(method, imp_implementationWithBlock(answer))
}
}Replacing the public NSParagraphStyle.defaultWritingDirection(forLanguage:) is pointless: AppKit does not call it on these paths.
4. Reinstalling the old constraints#
The replacement helps constraints installed after the switch. The ones already in place remember the side from the moment they were installed. So after a language change I walk the view tree: I set the direction and, if it differs from the launch direction, deactivate and reactivate the active constraints.
@MainActor
enum LayoutMirroring {
static func apply(_ direction: NSUserInterfaceLayoutDirection, to view: NSView) {
setDirection(direction, on: view)
if direction != AppDirection.launch { reinstallConstraints(in: view) }
}
private static func setDirection(_ direction: NSUserInterfaceLayoutDirection, on view: NSView) {
view.userInterfaceLayoutDirection = direction
view.subviews.forEach { setDirection(direction, on: $0) }
}
private static func reinstallConstraints(in view: NSView) {
if !(view is NSStackView) { // a stack manages its own constraints
let active = view.constraints.filter(\.isActive)
NSLayoutConstraint.deactivate(active)
NSLayoutConstraint.activate(active)
}
view.subviews.forEach(reinstallConstraints(in:))
}
}Do not touch a stack's constraints: AppKit writes a warning to the log and rebuilds the whole stack, and once a test app hung on it for me.
5. Window buttons and the title#
The window title has its own cache. When the window frame lays out the title, it asks the private method -[NSThemeFrame _shouldFlipTrafficLightsForRTL]. If you replace the answer and ask the frame to lay out the title again, the window buttons move at once:
@MainActor
enum TitlebarMirroring {
private static var isInstalled = false
static func refresh(_ window: NSWindow?) {
installIfNeeded()
guard let frameView = window?.contentView?.superview else { return }
let tile = NSSelectorFromString("_tileTitlebarAndRedisplay:")
guard frameView.responds(to: tile),
let imp = class_getMethodImplementation(type(of: frameView), tile) else { return }
typealias Tile = @convention(c) (AnyObject, Selector, Bool) -> Void
unsafeBitCast(imp, to: Tile.self)(frameView, tile, true)
}
private static func installIfNeeded() {
guard !isInstalled else { return }
isInstalled = true
guard let frameClass = NSClassFromString("NSThemeFrame"),
let method = class_getInstanceMethod(frameClass, NSSelectorFromString("_shouldFlipTrafficLightsForRTL"))
else { return }
let answer: @convention(block) (AnyObject) -> Bool = { _ in
AppDirection.current == .rightToLeft
}
method_setImplementation(method, imp_implementationWithBlock(answer))
}
}LayoutMirroring.apply and TitlebarMirroring.refresh are called after every screen change and after every new window is created.
The price: private methods#
Two of these methods are private. Each replacement first checks that the method exists. If it disappears in the next macOS, the replacement is not installed, and the app behaves as before: correct after a restart, no crash.
Notarization does not check for private methods, and Chromium and Electron replace the same window buttons selector. App Store review would reject it under rule 2.5.1 on public APIs. For me this is an experiment: the solution works, but I am still looking for a way to do it without private API. If you know one, or have switched RTL on the fly in AppKit differently, please share.
What did not work#
- An explicit direction on the label.
baseWritingDirection = .rightToLefton anNSTextFieldor in the paragraph style of an attributed string: text with.naturalwas still drawn at x = 3. - Only an
NSApplicationsubclass. Constraints and Latin text stayed on the launch side. - A direction on the window frame.
windowTitlebarLayoutDirectionis read-only, and a direction set onNSThemeFramedoes not move the buttons. - Reinstalling only for views that "changed". New views already answered with the new direction through the app, so there was nothing to reinstall.
How to check without clicking#
It is easy to miss an error by eye, so I wrote an automated test. It rebuilds eight screens from scratch in LTR and RTL, as after a real switch, and checks two things:
- Every visible element in RTL sits as a mirror image of its LTR position, within 2 pt.
- Every
.naturaltext is pinned to the correct edge, which is checked from the snapshot pixels.
I checked the check too: without reinstalling constraints the test fails on two screens, and without replacing the direction it fails on two labels. But the test does not cover the window title or full-screen windows, and once it missed a live bug on the start screen, so you still have to look with your eyes.
For manual checking it is handy to simulate a launch in the other direction: defaults write <bundle-id> AppleTextDirection -bool NO before launch, then a snapshot with screencapture -x.
What Apple does not explain#
- How to reset the direction cache. There is no public API, and the documentation does not say that the direction is computed once per process. It shows only in the behavior and in the browsers' code.
- How long the private methods will live. I checked on macOS 26. On older versions Chromium has used the same window buttons selector for a long time, but I did not run them myself.
Arabic adds ordinary work too: translating every string and not missing a single one. How I catch untranslated strings in Swift code before the build is described in the article about the localization gate.



