阿拉伯语变化方向的本地化

时间:2019-04-02 17:55:38

标签: ios swift localization

您好,在我的应用程序中,我使用的是https://github.com/Abedalkareem/LanguageManager-iOS窗格,并且已注释了所有更改视图方向的代码,但是当我将语言从英语更改为阿拉伯语时,重新打开菜单时它将更改文本的方向应用程序,它可以从左到右改变方向,当我再次选择英语时,一切都很好,我想禁用,当语言改变时我什么也不想做。

这是我的语言人的代码

ager
//
//  LanguageManger.swift
//
//  Created by abedalkareem omreyh on 10/23/17.
//  Copyright © 2017 abedlkareem omreyh. All rights reserved.
//  GitHub: https://github.com/Abedalkareem/LanguageManger-iOS
//

import UIKit

class LanguageManger {

    static let shared: LanguageManger = LanguageManger()


    /// Returns the currnet language
    var currentLanguage: Languages {
        get {

            guard let currentLang = UserDefaults.standard.string(forKey: "selectedLanguage")
                else {
                    fatalError("Did you set the default language for the app ?")
            }
            return Languages(rawValue: currentLang)!
        }
        set {
            print("raw value\(newValue.rawValue)")
            UserDefaults.standard.set(newValue.rawValue, forKey: "selectedLanguage")
        }
    }

    /// Returns the default language that the app will run first time
    var defaultLanguage: Languages {
        get {

            guard let defaultLanguage = UserDefaults.standard.string(forKey: "defaultLanguage") else {
                fatalError("Did you set the default language for the app ?")
            }
            return Languages(rawValue: defaultLanguage)!
        }
        set {

            // swizzle the awakeFromNib from nib and localize the text in the new awakeFromNib
            UIView.localize()

            let defaultLanguage = UserDefaults.standard.string(forKey: "defaultLanguage")
            guard defaultLanguage == nil else {
                return
            }

            UserDefaults.standard.set(newValue.rawValue, forKey: "defaultLanguage")
            UserDefaults.standard.set(newValue.rawValue, forKey: "selectedLanguage")
            setLanguage(language: newValue)
        }
    }


    // Returns the diriction of the language
        var isRightToLeft: Bool {
            get {
                let lang = currentLanguage.rawValue
                return lang.contains("s") || lang.contains("he") || lang.contains("ur") || lang.contains("fa")
            }
        }

    /// Returns the app locale for use it in dates and currency
    var appLocale: Locale {
        get {
            return Locale(identifier: currentLanguage.rawValue)
        }
    }

    ///
    /// Set the current language for the app
    ///
    /// - parameter language: The language that you need from the app to run with
    ///
    func setLanguage(language: Languages) {

      //   change the dircation of the views
//        let semanticContentAttribute:UISemanticContentAttribute = language == .ar ? .forceRightToLeft : .forceRightToLeft
//                UIView.appearance().semanticContentAttribute = semanticContentAttribute
//                UINavigationBar.appearance().semanticContentAttribute = semanticContentAttribute
//                UITextField.appearance().semanticContentAttribute = semanticContentAttribute
//                UITextView.appearance().semanticContentAttribute = semanticContentAttribute

        // change app language
        UserDefaults.standard.set([language.rawValue], forKey: "AppleLanguages")
        UserDefaults.standard.synchronize()

        // set current language
        currentLanguage = language
        print("---curent langugae from lanagugae maneger \(currentLanguage)")
    }
}

enum Languages:String {
    case ar,en,nl,ja,ko,vi,ru,sv,fr,es,pt,it,de,da,fi,nb,tr,el,id,ms,th,hi,hu,pl,cs,sk,uk,hr,ca,ro,he
    case enGB = "en-GB"
    case enAU = "en-AU"
    case enCA = "en-CA"
    case enIN = "en-IN"
    case frCA = "fr-CA"
    case esMX = "es-MX"
    case ptBR = "pt-BR"
    case zhHans = "zh-Hans"
    case zhHant = "zh-Hant"
    case zhHK = "zh-HK"
}


// MARK: Swizzling
extension UIView {
    static func localize() {

        let orginalSelector = #selector(awakeFromNib)
        let swizzledSelector = #selector(swizzledAwakeFromNib)

        let orginalMethod = class_getInstanceMethod(self, orginalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

        let didAddMethod = class_addMethod(self, orginalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))

        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(orginalMethod!), method_getTypeEncoding(orginalMethod!))
        } else {
            method_exchangeImplementations(orginalMethod!, swizzledMethod!)
        }

    }

    @objc func swizzledAwakeFromNib() {
        swizzledAwakeFromNib()

        switch self {
        case let txtf as UITextField:
            txtf.text = txtf.text?.localiz()
        case let lbl as UILabel:
            lbl.text = lbl.text?.localiz()
        case let btn as UIButton:
            btn.setTitle(btn.title(for: .normal)?.localiz(), for: .normal)
        default:
            break
        }
    }
}


// MARK: String extension
extension String {


    /// Localize the current string to the selected language

    /// - returns: The localized string
    ///
    func localiz() -> String {
        guard let bundle = Bundle.main.path(forResource: LanguageManger.shared.currentLanguage.rawValue, ofType: "lproj") else {
            return NSLocalizedString(self, comment: "")
        }

        let langBundle = Bundle(path: bundle)
        return NSLocalizedString(self, tableName: nil, bundle: langBundle!, comment: "")
    }

}

// MARK: UIApplication extension
extension UIApplication {
    // Get top view controller
    static var topViewController:UIViewController? {
        get{
            if var topController = UIApplication.shared.keyWindow?.rootViewController {
                while let presentedViewController = topController.presentedViewController {
                    topController = presentedViewController
                }
                return topController
            }else{
                return nil
            }
        }
    }

}

更改语言后我正在这样做,但是什么也没发生

 UINavigationBar.appearance().semanticContentAttribute = .forceLeftToRight
               UITextField.appearance().semanticContentAttribute = .forceLeftToRight
                    UITextView.appearance().semanticContentAttribute = .forceLeftToRight
                   UIView.appearance().semanticContentAttribute = .forceLeftToRight

In Arabic langauge text shows in right side

2 个答案:

答案 0 :(得分:0)

您可以使用view.contentMode = .right,这将强制内容,无论它是哪种语言,因为使用阿拉伯语时,它将变为左侧。

答案 1 :(得分:0)

UIView.appearance()。semanticContentAttribute = .forceLeftToRight