iOS: Determine if device language is Right to Left (RTL)

前端 未结 11 1835
后悔当初
后悔当初 2020-12-04 23:20

Is there a way to easily determine if the language the device is set to is right to left (RTL)?

相关标签:
11条回答
  • 2020-12-04 23:59

    Here is how i Used it :

    +(NSTextAlignment) alignmentOfLanguage {
    if ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]]==NSLocaleLanguageDirectionRightToLeft){
            return NSTextAlignmentRight;
        }
       return NSTextAlignmentLeft;  
    }
    

    The Last example didn't work for me , but with a little variant , i got it rightX2 .

    any comments?

    0 讨论(0)
  • 2020-12-05 00:02

    Make sure you return the currently selected language, not the current region of the device. The region and language are often the same. However, if I am in North America and I set my language to Japanese, my region will still be English (United States). In order to check the character direction of the currently selected language, you can do:

    + (BOOL)isDeviceLanguageRTL {
      return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
    }
    

    You may likely want to cache the result, using dispatch_once.

    Keep in mind that this is the user's preferred language direction, and not necessarily the language direction of the text. For that, use a C function that is based on u_charDirection.

    0 讨论(0)
  • 2020-12-05 00:06

    Here is a swift 3 version:

    import UIKit
    
    extension UIView
    {
        /// Returns text and UI direction based on current view settings
        var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection
        {
            if #available(iOS 9.0, *) {
                return UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute)
            } else {
                return UIApplication.shared.userInterfaceLayoutDirection
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 00:08

    In iOS 9 one can determine the current direction for each individual view.

    if #available(iOS 9.0, *) {
      if UIView.userInterfaceLayoutDirection(
        for: myView.semanticContentAttribute) == .rightToLeft {
    
          // The view is shown in right-to-left mode right now.
      }
    } else {
        // Use the previous technique
        if UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft {
    
            // The app is in right-to-left mode
        }
    }
    

    This is the recommended way of determining the layout direction in iOS 9.

    WWDC 2015 video New UIKit Support for International User Interfaces. After minute 31:20.

    0 讨论(0)
  • 2020-12-05 00:12

    There is an official way to do it:

    if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
    }
    

    I would recommend against using some of the other solutions, because they will not always return the correct locale. Just because it's on the top of preferred languages doesn't mean that the application supports it.

    Source: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/SupportingRight-To-LeftLanguages/SupportingRight-To-LeftLanguages.html

    0 讨论(0)
  • 2020-12-05 00:13

    NSLocale has two methods +characterDirectionForLanguage: and +lineDirectionForLanguage:. The first is presumably Left-to-Right vs Right-to-Left and the second is Top-to-Bottom vs Bottom-to-Top. You can pass it the result of [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode].

    Update:

    The original question asked was how to determine whether the device language is RTL. Using +[NSLocale characterDirectionForLanguage:] and +[NSLocale lineDirectionForLanguage:] is unambiguously correct for that; you can pass either [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] or [NSLocale preferredLanguages][0] to that to get the relevant info (I'm not sure offhand whether the NSLocaleLanguageCode uses the preferred language, or the set region).

    However, it's very likely that what the original poster actually wanted to know is whether the application's interface should be laid out in RTL or LTR. This is very similar to asking what the direction of the language is, except it takes the application's available localizations into account. If the application is not localized into the user's preferred language, it will use a non-preferred language instead. And the answer to this question is to use [UIApplication sharedApplication].userInterfaceLayoutDirection.

    0 讨论(0)
提交回复
热议问题