问题
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:
I look at this question, NSAlert for right to left languages (reversed layout), but the answer is not helpful at all for me.
I also look at this guide here:
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.
回答1:
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"];
来源:https://stackoverflow.com/questions/45060626/my-osx-app-cannot-detect-rtl-language