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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11352342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:09:41  来源:igfitidea点击:

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

ioslocalization

提问by BarrettJ

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

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

回答by Evgenii

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

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

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

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

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

WWDC 2015 视频新的 UIKit 支持国际用户界面。31:20 分钟后。

回答by Accatyyc

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

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

回答by Lily Ballard

NSLocalehas 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].

NSLocale有两种方法+characterDirectionForLanguage:+lineDirectionForLanguage:。第一个大概是从左到右 vs 从右到左,第二个是从上到下 vs 从下到上。您可以将结果传递给它[[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 NSLocaleLanguageCodeuses the preferred language, or the set region).

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

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.

但是,很可能原始海报实际上想知道的是应用程序的界面应该以 RTL 还是 LTR 布局。这与询问语言的方向非常相似,只是它考虑了应用程序的可用本地化。如果应用程序没有本地化为用户的首选语言,它将使用非首选语言。这个问题的答案是使用[UIApplication sharedApplication].userInterfaceLayoutDirection.

回答by Rose Perrone

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.

您可能想要缓存结果,使用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.

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

回答by David Rysanek

Here is a swift 3 version:

这是一个 swift 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
        }
    }
}

回答by Gerhat

Thanks to Kevin Ballard's answer I was able to create the following utility function to do this:

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

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

回答by FormigaNinja

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:

对于那些想要检查设备语言是否为 RTL 的人,独立于您的应用程序是否支持该语言,您应该[NSLocale characterDirectionForLanguage:]像这样使用:

+ (BOOL)isDeviceLanguageRightToLeft {

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

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

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

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:

Apple 建议您使用[UIApplication sharedApplication].userInterfaceLayoutDirection,因为它会根据您的应用程序使用的语言(支持)返回方向。这是代码片段:

+ (BOOL)isAppLanguageRightToLeft {

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

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

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

回答by Ravi Kumar

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");
    }
}

must watch Advanced Topics in Internationalization wwdc2014

必看的国际化高级专题 wwdc2014

回答by Tarek hemdan

if you want to check if the device is running in RTL or LTR in swift 3

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

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

回答by Y.Z.

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 .

最后一个例子对我不起作用,但有一点变体,我得到了正确的X2。

any comments?

任何意见?