Try following code...it
might help you to come out from your problem.
Step 1
extension String {
/// Returns the localized string value
public var localized: String {
if let bundleName:String = UserDefaults.standard.value(forKey: "USER_LANG") as? String {
let path = Bundle.main.path(forResource: bundleName, ofType: "lproj")
let bundle = Bundle.init(path: path!)
return localize(withBundle: bundle!)
} else {
return localize(withBundle: Bundle.main)
}
}
public func localize(withBundle bundle: Bundle) -> String
{
return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
}
}
Step 2
- Store the bundle name on a button click in user default.
Example
// Strore Base bundleName when english button is clicked
UserDefaults.standard.set("Base", forKey: "USER_LANG")
// Strore fr(short from of french) bundleName when french button is clicked
UserDefaults.standard.set("fr", forKey: "USER_LANG")
Step 3
Usage
"lbl_name"="name";
// name will convert in French and English too
"lbl_name".localized
Thank you!!!