Localisation Settings Not Changing the System Defined Text in Swift 4.2
Asked Answered
H

1

8

I have 2 lang support for my app 1) English - en 2) German - de

I have done all the procedures for localization, the only issue is whenever I change the language from "en" to "de" or vice versa then after the system text is not changing to the latest lang, but it reflects when I kill the app and reopen it.

For Example:

enter image description here

The popover Copy-LookUp-Share is not localsied to German Lang. but the other things from .string file are lcoalised properly.

My change lang code:

func setLanguage(languageCode:String) {
        var appleLanguages = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
        appleLanguages.remove(at: 0)
        appleLanguages.insert(languageCode, at: 0)
        UserDefaults.standard.set(appleLanguages, forKey: "AppleLanguages")
        UserDefaults.standard.synchronize()

        if let languageDirectoryPath = Bundle.main.path(forResource: languageCode, ofType: "lproj")  {
            bundle = Bundle.init(path: languageDirectoryPath)
        } else {
            resetLocalization()
        }
        }


    func resetLocalization() {
        bundle = Bundle.main
    }

FYI: Similar thing happens in 'WeChat' iOS application.

Hickman answered 23/1, 2020 at 15:27 Comment(3)
UIMenuController language will set by Device Language, is your device language in German?Tint
@Tint no device lang is Not dependingHickman
I think we have no need to manage language bundle path like your code. Simply, we can use localized strings with NSBundle methods such as "NSLocalizedString(key, comment)". The language is according to the device language. I think the code can not ensure the timing of resource loading. i think system may load resources before "bundle = Bundle.init(path: languageDirectoryPath)" into cache.Seducer
P
0

Those are UIKit SDK private menu items and you cannot control them, probably they are just created once then kept cached.

The only thing you can try is to force update in the moment of language change, like

UIMenuController.shared.update()

Note: actually using "AppleLanguages" key for that purpose is not documented, so it is kind of hucky. L10N is system settings originated feature, by design.

Plangent answered 6/2, 2020 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.