Default language of the application is not set

149 views Asked by At

I need to set the default Russian language in the application to swift. I am using Xcodegen to generate .xcodeproj. There is my project.yml

options:
  deploymentTarget: '13.0'
  groupSortPosition: top
  generateEmptyDirectories: true
  minimumXcodeGenVersion: '2.32.0'
  developmentLanguage: ru

After generation, Russian is displayed as default

enter image description here In app delegate I call this function:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        LanguageManager.shared.firstLaunchCurrentLanguageSetup()
...
final class LanguageManager {
    static let shared = LanguageManager()
    
    var selected: String {
        set {
            UserDefaults.standard.set([newValue, selected], forKey: "AppleLanguages")
            UserDefaults.standard.set(newValue, forKey: "language")
            UserDefaults.standard.synchronize()
            setLanguage()
        }
        get {
            return UserDefaults.standard.string(forKey: "language") ?? LanguagesList.russianCode
        }
    }
    func firstLaunchCurrentLanguageSetup() {
        if UserDefaults.standard.string(forKey: "language") != nil {
            return
        } else {
            selected = LanguagesList.russianCode
        }
    }
private func setLanguage() {
        NotificationCenter.default.post(name: NSNotification.Name("LanguageDidChange"), object: nil)
    }
}

struct LanguagesList {
    static let englishCode = "en"
    static let russianCode = "ru"
    
    static let languages: [String: String] = [
        englishCode: "English",
        russianCode: "Русский"
    ]
}

Info.plist: enter image description here

After all these manipulations, when the application is first launched, it still runs in English on the device with the English language set, and when repeated, it is already in Russian. I also need to run it in Russian at the first launch

1

There are 1 answers

2
Miraj On

I am not able to comment so I posted an answer here.

Please Add setLanguage() function in your question

Or

You can check/Add below code in your setLanguage() function

let defaults = UserDefaults.standard
defaults.set(["ru"], forKey: "AppleLanguages")
defaults.synchronize()
Bundle.setLanguage("ru")