0

I have followed the step in many tutorial for localization.

I added the storyboard localization , Added string files . And chnaged the vlue in both string file as well as in storyboard.

For example, if i am trying English , french

Then in storyboard we have :-> Main.Stroyboard

Main.Strings(French)

/* Class = "UILabel"; text = "Hamza Smith"; ObjectID = "2gA-Wx-5v2"; */
"2gA-Wx-5v2.text" = "Hamza Smith";

Same in language.String

Language.string(French)

"Hamza Smith" = "some thing";

Then in code on change language button action :

 Bundle.setLanguage(lang: "Fn"). //FN for example i am adding 
    

My code :

extension Bundle {
    private static var bundle: Bundle!

    public static func localizedBundle() -> Bundle! {
        if bundle == nil {
            var appLang = UserDefaults.standard.string(forKey: "app_lang") ?? "en"
            if Bundle.main.path(forResource: appLang, ofType: "lproj") == nil {
                appLang = "en"
            }
            let path = Bundle.main.path(forResource: appLang, ofType: "lproj")
            bundle = Bundle(path: path!)
        }

        return bundle;
    }

    public static func setLanguage(lang: String) {
        UserDefaults.standard.set(lang, forKey: "app_lang")
        let path = Bundle.main.path(forResource: lang, ofType: "lproj")
        bundle = Bundle(path: path!)
    }
}
extension String {
    func localized() -> String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.localizedBundle(), value: "", comment: "")
    }

    func localizeWithFormat(arguments: CVarArg...) -> String{
        return String(format: self.localized(), arguments: arguments)
    }
}

And my iboutlet label has:

 titleLabel.text = "Hamza Smith".localized()

Now on tap on my save button after select any language its not changing. I killed the app and relaunch again. But its not changing.

Any help would be great.

Samarat
  • 33
  • 5

2 Answers2

0

Since you're using a custom name for the .strings file, you need to provide it as tableName in the NSLocalizedString method.

extension String {
    func localized() -> String {
        return NSLocalizedString(self, tableName: "Language", bundle: Bundle.localizedBundle(), value: "", comment: "") // <- note the new parameter tableName
    }
}
Mario
  • 470
  • 4
  • 9
-1

First I want to ask do you have key in your localized strings as "Hamza Smith"?

NSLocalizedString takes key as first argument that exists in your localized strings, for example you can create something like this:

extension String {
    
    public func localize(key: String) -> String {
        return NSLocalizedString(key, tableName: nil, bundle: Bundle.localizedBundle(), value: "", comment: "")
    }
  
}

and than call it like this:

label.text = .localize(key: "<localized string key>")

or if you want to use like your implementation you need to call it like this:

label.text = "<localized string key>".localized()

Edit:
If you want to change to french, you need to set "fr" not "Fn" :)

Stamenkovski
  • 959
  • 1
  • 6
  • 10