skip to Main Content

I have localized my app into 4 different languages.
I did’t use String catalog and followed an old Localizable.strings approach.

I want to specify programmatically what languages should be available for the user.
Let’s say I want this kind of a method in my AppDelegate:

func setPreferredLanguages(_ languages: [String]) {
   /// ...
}

So if I call setPreferredLanguages(["en, de"]), user won’t be able to see other than English and German languages in system’s Settings -> My app -> Language(preferred language).

I am searching for a way to "hide" localizations in a runtime.
I need to provide only 2 out of 4 languages to the end user but without actual deleting .lproj files.

2

Answers


  1. I think these 2 langs u wanna show should be the local lang of user and also English as additional lang yeah ? But at the same time u should thinking about if the user local lang is already English then u should not showing any lang as additional for selecting right?
    So u may get the device lang and then check if its different then english and show which lang it is and also english at the same time.

    For example;

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            // Set preferred languages based on device language
            setPreferredLanguages()
    
            return true
        }
    
        func setPreferredLanguages() {
            let deviceLanguage = Locale.preferredLanguages.first ?? "en"
            
            if deviceLanguage.starts(with: "en") {
                // Device language is English, so only allow English
                UserDefaults.standard.set(["en"], forKey: "AppleLanguages")
            } else {
                // Device language is not English, allow device language and English
                UserDefaults.standard.set([deviceLanguage, "en"], forKey: "AppleLanguages")
            }
    
            UserDefaults.standard.synchronize()
        }
    }
    
    Login or Signup to reply.
  2. You cannot modify the list of available localisations with code.

    iOS obtains the list of languages shown in Settings -> My app -> Language(preferred language) by reading your app bundle. Your app may not be running and may never have been run before the user accesses these settings.

    You can remove 2 of your localisations without actually deleting the lproj files by excluding them from the target in Xcode.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search