0

I am making a test app to implement localization. In the test app, this is what I'm trying to achieve...

When the user launches the app, the screen will display a label with the text ... 'This is a test project' and button called 'Change' below. Now when I click on the 'change' button, the label should display in French..'c'est un projet test' and the button will be called 'changement'.

I have a class called L102Language which will set my language to the current language. It is given like so..

import Foundation
import UIKit
// constants
let APPLE_LANGUAGE_KEY = "AppleLanguages"
/// L102Language
class L102Language {
    /// get current Apple language
    class func currentAppleLanguage() -> String{
        let userdef = UserDefaults.standard
        let langArray = userdef.object(forKey: APPLE_LANGUAGE_KEY) as! NSArray
        let current = langArray.firstObject as! String
        return current
    }
    /// set @lang to be the first in Applelanguages list
    class func setAppleLAnguageTo(lang: String) {
        let userdef = UserDefaults.standard
        userdef.set([lang,currentAppleLanguage()], forKey: APPLE_LANGUAGE_KEY)
        userdef.synchronize()
    }
}

In the viewcontroller, this is the code I have written..

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var txtLabel: UILabel!

    @IBOutlet weak var change: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    @IBAction func changeLanguage(_ sender: Any) {


        if L102Language.currentAppleLanguage() == "en" {
         L102Language.setAppleLAnguageTo(lang: "fr")
        } else {
         L102Language.setAppleLAnguageTo(lang: "en")
        }        
    }    
}

But when I click on the 'change' button, nothing happens. The text is in english only.

EDIT 1: My Main.strings file have these...

/* Class = "UILabel"; text = "This is a test project"; ObjectID = "OQM-55-dEJ"; */
"OQM-55-dEJ.text" = "c'est un projet test";

/* Class = "UIButton"; normalTitle = "CHANGE"; ObjectID = "eMn-n8-na5"; */
"eMn-n8-na5.normalTitle" = "changement";
  • Make sure you have put all the text in String file which you are using in your project? – Rushabh Shah Nov 13 '19 at 07:05
  • @RushabhShah My Main.strings file have the french words mapped against the object id's..Have edited the question with it –  Nov 13 '19 at 07:09
  • hi @kfodof, can u debug and check which loop is it entering in changeLanguage(), is it if or else? – vishal Nov 13 '19 at 07:11
  • @vishal when i click on the change button for the first time, it is entering the if loop. –  Nov 13 '19 at 07:13
  • @kfodof please refer this link https://stackoverflow.com/questions/29985614/how-can-i-change-locale-programmatically-with-swift – vishal Nov 13 '19 at 07:32
  • @kfodof I can see here that you are just saving and fetching values in UserDefault but not applying any localization. You must include do it to get it worked!! – RJ168 Nov 13 '19 at 07:38
  • could you maybe mind showing how @RJ168 –  Nov 13 '19 at 07:43
  • @kfodof I have added my answer here where you can find the way to apply localization at run time. It is well tested solution and If it suits your requirements then do accept it. Happy coding :) – RJ168 Nov 13 '19 at 10:16
  • @kfodof I hope I helped you. If so then please mark it as accepted so others can take benefits from it!! – RJ168 Nov 21 '19 at 06:43

1 Answers1

0

To use localization in project you can use the following way!!

  1. Add Localization to Project. Project -> Localizations -> Add Language for localization for e.g. (English/German/Hindi etc)

  2. Add string file and apply localization from right panel.

  3. Add key and associate value according to your language in crated files (English/German/Hindi etc)

for e.g.

"localizeText" = "English"; "localizeText" = "German";

  1. Apply it where ever you want. For e.g.

    class ViewController: UIViewController {
        @IBOutlet weak var name: UILabel!
    
           @IBAction func showGermanText(_ sender: Any) {
            let val = name.text!.localized("de")
            name.text = val
          }
    
          @IBAction func showEnglishText(_ sender: Any) {
            let val = name.text!.localized("en")
            name.text = val
          }
        }
    

`

extension String {
      func localized(_ lang:String) ->String {
        let path = Bundle.main.path(forResource: lang, ofType: "lproj")
        let bundle = Bundle(path: path!)
        return NSLocalizedString("localizeText", tableName: "local", bundle: bundle!, value: "", comment: "")
      }}

Note* Here local is your localized string file name.

RJ168
  • 1,006
  • 2
  • 12
  • 22