trungdinhtrong trungdinhtrong - 4 months ago 22
Objective-C Question

My OSX app cannot detect RTL language

I am developing an OSX app with very minimum UI. For internalization/localization, I used a 3rd party library to replace all my text with appropriate language (I detect the language using [NSLocale preferredLanguages]). The problem is my NSAlert always displays its content (including text, button layout...) in Left-to-Right direction, even when I switch the system language to, says, Arabic.

From my research it would seem that NSAlert should automatically switch the direction correctly (though no where it is explicitly said so). I also check this

NSUserInterfaceLayoutDirection direction = [NSApplication sharedApplication].userInterfaceLayoutDirection;


and
direction
would always (even in the case of system language being set to Arabic), equal to
NSUserInterfaceLayoutDirection::NSUserInterfaceLayoutDirectionLeftToRight
. I suspect this is the problem, but don't quite know how to address this.

Anybody know how to:


  • Make sure NSApplication knows the right layout direction?



or


  • Make sure NSAlert display with the correct direction (particularly in RLF language)?



Note:



https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html#//apple_ref/doc/uid/10000171i-CH1-SW1

and it still does not help, because in order to add support for languages, it looks like this guide requires that I have xib or a storyboard file. My project happens to not have either of these.

Thank you all in advance.

Answer Source

To support Arabic an empty ar-001.lproj folder in the Resources folder inside the app will do.

If you don't want to add folders for each language, you can force right to left writing direction by calling

[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"AppleTextDirection"];
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"NSForceRightToLeftWritingDirection"];

and to switch back to left to right

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleTextDirection"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"NSForceRightToLeftWritingDirection"];