0
votes

when I'm doing localizable, Bundle just not yet loaded,path should be right, coz when i print(fm.fileExists(atPath: path!)), it returns me true

Here's func initUserLanguage()

string = string.replacingOccurrences(of: "-CN", with: "")
                   .replacingOccurrences(of: "-US", with: "")
                   .replacingOccurrences(of: "-SG", with: "")

    var path = Bundle.main.path(forResource: string, ofType: "lproj")
    print("path 1 = \(String(describing: path))")
    if path == nil {
        def.set("en", forKey: UserLanguage)
        def.synchronize()
        path = Bundle.main.path(forResource: "en", ofType: "lproj")
        print("path 2 = \(String(describing: path))")
    }
    bundle = Bundle(path: path!)
    print(bundle!)
    let fm = FileManager()
    print(fm.fileExists(atPath: path!))

Here's the log

// ==== 1-1 - haven't change system language(Chinese), path exist,the Bundle(path)just not yet loaded string -> zh-Hans-SG

#####UserLanguage! -> zh-Hans-SG = zh-Hans-SG

LanguageHelper - is guest LanguageHelper - loadData_Language - zh-Hans //when system language is Chinese, i send to server that i need Chinese API path 1 = Optional("/var/containers/Bundle/Application/0142208A-EC53-46C1-B6D8-276A5C5D87F7/DiningButlerSG.app/zh-Hans.lproj") bundle = Optional(NSBundle (not yet loaded)) true

// ==== 1-2 - after change system language(Chinese to English), path exist,the Bundle(path)just not yet loaded string -> zh-Hans-SG

#####UserLanguage! -> zh-Hans-SG = en-SG

LanguageHelper - is guest LanguageHelper - loadData_Language - en //when system language is English, i send to server that i need English API path 1 = Optional("/var/containers/Bundle/Application/F185F1E3-0F34-4208-8D8A-F9362978BD9C/DiningButlerSG.app/en.lproj") bundle = Optional(NSBundle (not yet loaded)) true

// ==== 2 - after change system language(English to Japanese), path not exist,the Bundle(path)also not yet loaded string -> en-SG

#####UserLanguage! -> en-SG = ja-SG

LanguageHelper - is guest LanguageHelper - loadData_Language - en ////when system language is Japanese(anyothers), i send to server that i need English API path 1 = nil path 2 = Optional("/var/containers/Bundle/Application/DDFACBDA-7AFA-4DD6-828E-E69F56B624BE/DiningButlerSG.app/en.lproj") bundle = Optional(NSBundle (not yet loaded)) true

However, the Localizable.strings and storyboard can work when switch system language between Chinese and English (but from log, actually path also not yet loaded)

i deleted and removed localizable file before, I'm afraid the path may messed so i clear all and recreate the localizable.strings again, is something wrong because of this?

this is the folder

1

1 Answers

0
votes

You can't load your storyboard on language change from code. You must need to relaunch your code if you want to use two storyboards for different languages. But you can use Localizable string directly from code on language change using the same storyboard. means you can change the text using your Langauge_Helper Class by connecting outlet of each and every element. You will always get the error(Not yet loaded) but it will work for localizable.strings but you can't load your storyboard without relaunching your app.

Hope it helps..