0

I am following below link for multilanguage functionality in my app but when i am changing the language of Finnish i.e "fi" to chineese simplified i.e "zh-Hans" then it is not wokring.

Change Language in the app programmatically

Here is code:

class LanguageManager: NSObject {


var availableLocales = [CustomLocale]()
static let sharedInstance = LanguageManager()
var lprojBasePath = String()

override fileprivate init() {

    super.init()
    let english = CustomLocale(languageCode: GlobalConstants.englishCode, countryCode: "gb", name: "United Kingdom")
    let finnish  = CustomLocale(languageCode: GlobalConstants.finnishLangCode, countryCode: "cn", name: "China")
    self.availableLocales = [english,finnish]
    self.lprojBasePath =  getSelectedLocale()
}


fileprivate func getSelectedLocale()->String{

    let lang = Locale.preferredLanguages//returns array of preferred languages
    let languageComponents: [String : String] = Locale.components(fromIdentifier: lang[0])
    if let languageCode: String = languageComponents["kCFLocaleLanguageCodeKey"]{

        for customlocale in availableLocales {

            if(customlocale.languageCode == languageCode){

                return customlocale.languageCode!
            }
        }
    }
    return "en"
}

func getCurrentBundle()->Bundle{

    if let bundle = Bundle.main.path(forResource: lprojBasePath, ofType: "lproj"){

        return Bundle(path: bundle)!

    }else{

        fatalError("lproj files not found on project directory. /n Hint:Localize your strings file")
    }
}

func setLocale(_ langCode:String){

    UserDefaults.standard.set([langCode], forKey: "AppleLanguages")//replaces Locale.preferredLanguages
    UserDefaults.standard.synchronize()
    self.lprojBasePath =  getSelectedLocale()
}

}


class CustomLocale: NSObject {

var name:String?
var languageCode:String?
var countryCode:String?

init(languageCode: String,countryCode:String,name: String) {

    self.name = name
    self.languageCode = languageCode
    self.countryCode = countryCode

}
}
manku
  • 13
  • 1
  • 2
  • 8
  • [Did you checked this project on github](https://github.com/anishparajuli555/SMSLocalization) – Mathi Arasan Jul 04 '17 at 11:27
  • yes and i made change in that only after downloading. – manku Jul 05 '17 at 02:04
  • I tried hindi language it's working fine but it's not working for chineese. But also getting this crash "fatal error: lproj files not found on project directory. /n Hint:Localize your strings". My .lproj name is "zh-Hans.lproj". This is my language name in Global Constant static let chineeseLangCode = "zn". The line of code in iBaction LanguageManager.sharedInstance.setLocale("zh-Hans") bundle = LanguageManager.sharedInstance.getCurrentBundle() – manku Jul 05 '17 at 09:54
  • I don't know exactly, Can you create a issue or try to contact that github guy? – Mathi Arasan Jul 06 '17 at 12:35
  • @user3589771 Is there any way i can uplaod my project on stackoverflow? – manku Jul 07 '17 at 03:08
  • I don't think so. Try dropbox, github, google drive some other cloud storage. – Mathi Arasan Jul 07 '17 at 13:08

0 Answers0