iOS:确定设备语言是否从右到左(RTL)

时间:2012-07-05 20:48:51

标签: ios localization

有没有办法轻松确定设备设置的语言是从右到左(RTL)?

14 个答案:

答案 0 :(得分:52)

在iOS 9中,可以确定每个视图的当前方向。

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
    }
}

这是在iOS 9中确定布局方向的推荐方法。

WWDC 2015视频New UIKit Support for International User Interfaces。分钟后31:20。

答案 1 :(得分:39)

有一种官方方式可以做到:

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

我建议不要使用其他一些解决方案,因为它们不会总是返回正确的语言环境。仅仅因为它位于首选语言之上并不意味着应用程序支持它。

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

答案 2 :(得分:17)

NSLocale有两种方法+characterDirectionForLanguage:+lineDirectionForLanguage:。第一个可能是从左到右与从右到左,第二个是从上到下与从下到上。您可以将结果传递给[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]

<强>更新

最初提出的问题是如何确定设备语言是否为RTL。使用+[NSLocale characterDirectionForLanguage:]+[NSLocale lineDirectionForLanguage:]毫无疑问是正确的;您可以将[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode][NSLocale preferredLanguages][0]传递给它以获取相关信息(我不确定NSLocaleLanguageCode是否使用首选语言或设置区域。

然而,原始海报实际上想知道的很可能是应用程序的界面是否应该用RTL或LTR进行布局。这非常类似于询问语言的方向,除了它将应用程序的可用本地化考虑在内。如果应用程序未本地化为用户的首选语言,则它将使用非首选语言。这个问题的答案是使用[UIApplication sharedApplication].userInterfaceLayoutDirection

答案 3 :(得分:10)

确保返回当前选定的语言,而不是设备的当前区域。区域和语言通常是相同的。但是,如果我在北美并将语言设为日语,我的地区仍将是英语(美国)。要检查当前所选语言的字符方向,您可以执行以下操作:

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

您可能希望使用dispatch_once缓存结果。

请记住,这是用户首选的语言方向,而不一定是文本的语言方向。为此,请使用基于u_charDirection

的C函数

答案 4 :(得分:5)

这是一个快速的3版本:

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
        }
    }
}

答案 5 :(得分:4)

感谢Kevin Ballard的回答,我能够创建以下实用程序功能来执行此操作:

+ (BOOL)isDeviceLanguageRTL {
   return [NSLocale characterDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]==NSLocaleLanguageDirectionRightToLeft;
}

答案 6 :(得分:2)

如果您只想知道 iOS 10+ 上的特定视图布局方向,您可以使用:

view.effectiveUserInterfaceLayoutDirection == .rightToLeft

答案 7 :(得分:1)

以下是我如何使用它:

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

最后一个例子对我不起作用,但是有一个小变体,我把它弄好了.X2。

有任何意见吗?

答案 8 :(得分:1)

好的,虽然这是一个老问题并且已经接受了答案,但无论如何我都会回答。

对于那些想要检查设备语言是否为RTL 的人,如果您的应用程序支持或不支持此语言,则应该是独立的,您应该使用[NSLocale characterDirectionForLanguage:],如下所示:

+ (BOOL)isDeviceLanguageRightToLeft {

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

如果您的应用仅支持英语,则上述代码将返回YES,但您的设备例如设置为阿拉伯语。

Apple建议您使用[UIApplication sharedApplication].userInterfaceLayoutDirection,因为它会根据您的应用使用的语言(支持)返回指示。以下是代码段:

+ (BOOL)isAppLanguageRightToLeft {

    NSLocaleLanguageDirection direction = [UIApplication sharedApplication].userInterfaceLayoutDirection;
    return (direction == UIUserInterfaceLayoutDirectionRightToLeft);
}

当您的应用仅支持英语时,上面的代码将返回NO,但您的设备例如设置为阿拉伯语。

答案 9 :(得分:1)

if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
    if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:self.view.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft) {
        NSLog(@"Right to left");
    }
    else{
        NSLog(@"left to Right");
    }
} else {
    /* Use the previous technique */
    //Work for earlier ios 6 to ios 10
    if ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft) {
        NSLog(@"Right to left");
    }
    else{
        NSLog(@"left to Right");
    }
}

必须观看Advanced Topics in Internationalization wwdc2014

答案 10 :(得分:1)

如果你想检查设备是否在swift 3中以RTL或LTR运行

if(UIApplication.shared.userInterfaceLayoutDirection == UIUserInterfaceLayoutDirection.rightToLeft) {
    //RTL
} else {
   //LTR
}

答案 11 :(得分:0)

Rose Perrone是完全正确的。然而,在getter中使用dispatch_once作为一个简单的布尔值 - 实际上是太多的开销。不必要的一次使用派遣。 因为您可能希望在布局或绘图功能中多次使用它。

所以你有两个更快的选择:

+ (BOOL)isRtl
{
    static BOOL isRtl = NO;
    static BOOL isRtlFound = NO;
    if (!isRtlFound)
    { // This is "safe enough". Worst case is this code will be called twice in the app's lifecycle...
        isRtl = [NSLocale characterDirectionForLanguage:[NSBundle mainBundle].preferredLocalizations[0]] == NSLocaleLanguageDirectionRightToLeft;
        isRtlFound = YES;
    }
    return isRtl;
}

或者只是使用静态构造函数将其缓存在静态变量中:

static BOOL s_isRtl = NO;
+ initialize
{
    s_isRtl = [NSLocale characterDirectionForLanguage:[NSBundle mainBundle].preferredLocalizations[0]] == NSLocaleLanguageDirectionRightToLeft;
}

请注意,这实际上将在使用此代码的任何类之间共享静态变量。

答案 12 :(得分:0)

<块引用>

iOS 9 及以上版本



extension UIView {

    var isLayoutDirectionRightToLeft: Bool {
        UIView.userInterfaceLayoutDirection(for: semanticContentAttribute) == .rightToLeft
    }
    
}

    

答案 13 :(得分:0)

你可以这样检查RTL

- (BOOL)isDeviceLanguageRTL {
  return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
}
if ([self isDeviceLanguageRTL]) {
    //RTL
}
 else
{
   //LRT
}