0

I am creating an app which uses 2 languages Arabic and English. I have managed to change the layout to RTL for Arabic and normal for English. Also I have added the Localizable.strings file for Arabic and English.

The app picks up English and normal layout shows when English is selected and picks up Arabic and RTL layout is shown when the app is started the first time or on every restart.

It does not pick up Arabic or English Localizable.strings file on runtime. Is there a way to do this.

Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
  • Possible duplicate of [iOS: How to change app language programmatically WITHOUT restarting the app?](https://stackoverflow.com/questions/9416923/ios-how-to-change-app-language-programmatically-without-restarting-the-app) – Tamás Sengel Apr 04 '18 at 10:56
  • Yes you can, In short you can use `NSLocalizedString("your localizable KEy",tableName: "NameOFYourLocalizableStringFileForThatLanguage", comment: "")` – Reinier Melian Apr 04 '18 at 11:01

2 Answers2

3

You can change the current bundle you read from

extension String {
      func localizedStr(language:String) -> String {
          let path = Bundle.main.path(forResource: language, ofType: "lproj")
          let bundleName = Bundle(path: path!)
          return NSLocalizedString(self, tableName: nil, bundle: bundleName!, value: "", comment: "")

    }
}

In Action enter image description here enter image description here enter image description here

see demo here local

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
-1

Try following code...it might help you to come out from your problem.

Step 1

extension String {

    /// Returns the localized string value
    public var localized: String {
        if let bundleName:String = UserDefaults.standard.value(forKey: "USER_LANG") as? String {
            let path = Bundle.main.path(forResource: bundleName, ofType: "lproj")
            let bundle = Bundle.init(path: path!)
            return localize(withBundle: bundle!)
        } else {
            return localize(withBundle: Bundle.main)
        }

    }

    public func localize(withBundle bundle: Bundle) -> String
    {
        return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
    }

}

Step 2

  • Store the bundle name on a button click in user default.

Example

// Strore Base bundleName when english button is clicked
UserDefaults.standard.set("Base", forKey: "USER_LANG") 

// Strore fr(short from of french) bundleName when french button is clicked 
UserDefaults.standard.set("fr", forKey: "USER_LANG") 

Step 3

Usage

  • In String File

"lbl_name"="name";

// name will convert in French and English too
"lbl_name".localized

Thank you!!!

VishalPethani
  • 854
  • 1
  • 9
  • 17
  • All of the above works with the XIB files but when i use values from string files it does not work. – Yogesh Tandel Apr 04 '18 at 12:44
  • 1
    Your string file should be like `"lbl_name" = "name"` and when you use this string you have to write like `"lbl_name".localized`. – VishalPethani Apr 04 '18 at 12:49
  • // ` if L102Language.currentAppleLanguage() == "en" { L102Language.setAppleLAnguageTo(lang: "ar") UIView.appearance().semanticContentAttribute = .forceRightToLeft UserDefaults.standard.set("ar", forKey: "USER_LANG") } else { L102Language.setAppleLAnguageTo(lang: "en") UIView.appearance().semanticContentAttribute = .forceLeftToRight UserDefaults.standard.set("Base", forKey: "USER_LANG") } ` – Yogesh Tandel Apr 04 '18 at 13:02
  • I am using as given below `lbl_Hotels.text = StringClass.sharedInstance.lcStr_bookahotel.uppercased().localized` – Yogesh Tandel Apr 04 '18 at 13:35