1

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.

trungdinhtrong
  • 2,604
  • 2
  • 17
  • 26
  • 1
    Does your app support Arabic? – Willeke Jul 12 '17 at 14:58
  • Ah - I don't even know that I have to make my app support individual languages. I used a 3rd party library to replace all my text with appropriate language (I detect the language using [NSLocale preferredLanguages]). So now in my app, the texts are in Arabic if I switch the system to Arabic, but the direction does not change. How do I specify what language I should support? – trungdinhtrong Jul 12 '17 at 15:14
  • What do you mean by "the answer is not helpful at all for me"? The accepted solution there says that you need to create a custom alert class. So what have you done in reference to that? – El Tomato Jul 12 '17 at 21:58
  • I'm not familiar with localizations but I think you have to add an Arabic localization to your app. – Willeke Jul 13 '17 at 10:30
  • @ElTomato: I don't believe NSAlert cannot be internationalize to handle RTL language. I don't think every single OSX developer who use NSAlert has to reimplement it whenever they start internationalization. Some answer like "you have to add Arabic specifically to your app" makes more sense to me, though I was hoping to avoid doing that. A fuller answer like "either add each individual language or reimplement NSAlert" would be helpful. – trungdinhtrong Jul 13 '17 at 13:10
  • "I used a 3rd party library to replace all my text" -- this seems like a pretty important piece of information that was omitted from the question! – Ssswift Jul 13 '17 at 16:06
  • @Ssswift - thanks. I added that piece of info now into the question. – trungdinhtrong Jul 13 '17 at 16:49
  • Just an idea: subclass `NSApplication` and override `userInterfaceLayoutDirection`. – Willeke Jul 15 '17 at 11:49

1 Answers1

5

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"];
Willeke
  • 14,578
  • 4
  • 19
  • 47
  • I tried the second approach - works like a charm! Thanks a lot, Willeke. – trungdinhtrong Jul 17 '17 at 15:40
  • @Willeke , I have used this, as I want to change app from LTR to RTL when user uses Arabic language, without changing the system language, but I need to restart the app. Is there any way , we can change app from LTR to RTL without restarting the app? – Krishna Maru Aug 03 '21 at 12:17
  • @KrishnaMaru I don't know, ask a new question please. – Willeke Aug 03 '21 at 14:45
  • @KrishnaMaru have you ever found how to change to RTL at runtime? Would really appreciate your help!! – Maks Shevchenko Jun 07 '22 at 11:31