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

前端 未结 11 1864
后悔当初
后悔当初 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-05 00:15

    Ok, although it's an old question with an accepted answer, I will answer it anyway.

    For those who wants to check whether the device language is RTL, independent if your application supports or not this language, you should use [NSLocale characterDirectionForLanguage:] like this:

    + (BOOL)isDeviceLanguageRightToLeft {
    
        NSLocale *currentLocale = [NSLocale currentLocale];
        NSLocaleLanguageDirection direction = [NSLocale characterDirectionForLanguage:[currentLocale objectForKey:NSLocaleLanguageCode]];
        return (direction == NSLocaleLanguageDirectionRightToLeft);
    }
    

    The code above will return YES if your app only supports english, but your device is set to Arabic for example.

    Apple recommends that you use [UIApplication sharedApplication].userInterfaceLayoutDirection, just because it returns the direction based on the language that your app is using (has support to). Here is the code snippet:

    + (BOOL)isAppLanguageRightToLeft {
    
        NSLocaleLanguageDirection direction = [UIApplication sharedApplication].userInterfaceLayoutDirection;
        return (direction == UIUserInterfaceLayoutDirectionRightToLeft);
    }
    

    The code above will return NO when your app only supports english, but your device is set to Arabic for example.

提交回复
热议问题