4

I have developed an app in Swift 3.2 which is currently live with base language English. I have added String files for Arabic and French. But till the time everything in completed and tested by my team I want to restrict app to one language as there would be issues I need to address even for the English version and upload to App store. Deleting the sting and nib file option will not work for me.

Please let me know if there is any code that I can add to appdelegate to restrict app only for English.

I have tried disabling language Arabic in Settings which deletes localized string files

Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25

3 Answers3

5

Added below code to AppDelegate in didFinishLaunchingWithOptions First line to force app left to right. Then in Edit Scheme - Added an Argument on Launch -AppleLanguages (Base). Working quite well for me to force user to English.

UIView.appearance().semanticContentAttribute = .forceLeftToRight
    UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()

enter image description here

Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
1

Here's a method that do what you want. I don't think there's another way to do that in the code.

// You can change 'Base' to 'en' if you don't have Base.lproj folder 
func localizedString(_ key: String) -> String? {
    if let path = Bundle.main.path(forResource: "Base", ofType: ".lproj"),
        let baseBundle = Bundle(path: path) {
        return baseBundle.localizedString(forKey: key, value: nil, table: nil)
    }
    return nil
}

EDIT: I found another way to do it based on this answer.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
    // Rest of your init code
}

EDIT 2:

What you could do for the first launch of the app in the viewDidLoad of your initial ViewController:

    if let languageArray = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String],
        languageArray.count >= 1,
        languageArray[0] != "Base" && languageArray.count == 1 {
        UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
        UserDefaults.standard.synchronize()
    } 
Jeremy
  • 1,461
  • 12
  • 26
  • Trying the second option. How do I implement the first option – Yogesh Tandel Sep 27 '17 at 13:05
  • For the first option, you can either have a Singleton with the method and replace the `NSLocalizedString` calls by `YourSingleton.sharedInstance. localizedString("yourKey")`, or you declare the method globally (outside of any class so you can access it from anywhere in your project) – Jeremy Sep 27 '17 at 13:12
  • 1
    Below code worked. It worked on an iphone device not on the simulator `UserDefaults.standard.set(["Base"], forKey: "AppleLanguages") UserDefaults.standard.synchronize()` – Yogesh Tandel Sep 27 '17 at 13:45
  • Hi Jeremy The above code works when the app is opened for the 2nd time. When i uninstall and reinstall the app the language remains Arabic for the first screen and for the second screen show english as required. Basically when I first install the app "Base' is not set as the userdefaults which might be a problem. I have added the above code in appdelegate – Yogesh Tandel Sep 27 '17 at 14:57
  • I already have that code. As said earlier, when i first install the app the Homescreen shows in Arabic and then the next screens shows in English. Once I close and start the app again then it shows Homescreen in English. I have found an answer to my question which i will add below. – Yogesh Tandel Sep 28 '17 at 05:21
1

Have you tried removing languages from the localization settings in the project file?

This is based on the guide provided by apple: Enabling Base Internationalization Image taken from developer.apple.com

I believe you would not have to delete anything. This would just disable the localization.