
Let’s get started
First, create a single view application.

Click on the project folder in the project navigator and select project section. Click on the + button under the localizations section.




Give the file name “Localizable.strings”. Click on this file. Then click on Localize button. You will see the below popup. Select your language and then click on Localize button.





Helper Class:
Create one Helper class for global methods. Add the following function for getting localize string.class AppHelper: NSObject { static func getLocalizeString(str:String) -> String { let string = Bundle.main.path(forResource: UserDefaults.standard.string(forKey: "Language"), ofType: "lproj") let myBundle = Bundle(path: string!) return (myBundle?.localizedString(forKey: str, value: "", table: nil))! } }
UIView.appearance().semanticContentAttribute = .forceLeftToRight UIView.appearance().semanticContentAttribute = .forceRightToLeft
if UserDefaults.standard.object(forKey: "Language") != nil && UserDefaults.standard.object(forKey: "Language") as! String == "he" { UserDefaults.standard.set("he", forKey: "Language") UIView.appearance().semanticContentAttribute = .forceRightToLeft } else { UserDefaults.standard.set("en", forKey: "Language") UIView.appearance().semanticContentAttribute = .forceLeftToRight } return true
Localizable String:
Localizable.strings (English) file :
“Choose Language” = “Choose Language”; “English” = “English”; “Hebrew” = “Hebrew”; “Next” = “Next”; “Country List” = “Country List”; “India” = “India”; “Australia” = “Australia”; “Mexico” = “Mexico”; “Kenya” = “Kenya”; “China” = “China”; “Bhutan” = “Bhutan”; “Canada” = “Canada”; “Japan” = “Japan”; “Qatar” = “Qatar”; “Israel” = “Israel”;”Country Description Here…” = “Country Description Here…”;Storyboards
Add two storyboard file named CountryListSB.storyboard and CountryDetailSB.storyboard in the project. Add two ViewController file CountryListViewController.swift and CountryDetailViewController.swift file in the project. ViewController.swift Create following variables for storing language dictionary and selected language. Initialize dictionary in viewDidLoad method. Write the viewWillAppear method as follow :var dictLanguage = [String:String]() var selectedLanguage: String = ""
dictLanguage = ["en" : "English", "he":"עברית"]
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tblLanguage.reloadData() selectedLanguage = dictLanguage[UserDefaults.standard.object(forKey: "Language") as! String]! lblTitle.text = AppHelper.getLocalizeString(str: "Choose Language") btnNext.setTitle(AppHelper.getLocalizeString(str: "Next"), for: .normal) }
extension ViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dictLanguage.keys.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let lblLanName = cell.viewWithTag(101) as! UILabel lblLanName.text = Array(dictLanguage)[indexPath.row].value let imgSelect = cell.viewWithTag(102) as! UIImageView if Array(dictLanguage)[indexPath.row].value == selectedLanguage { imgSelect.isHidden = false } else { imgSelect.isHidden = true } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tblLanguage.deselectRow(at: indexPath, animated: false) let cell = tblLanguage.cellForRow(at: indexPath) selectedLanguage = (cell?.viewWithTag(101) as! UILabel).text! tblLanguage.reloadData() } }
Button Click Method:
Write Next button click method as below:@IBAction func onClickBtnNext(_ sender: Any) { if selectedLanguage != dictLanguage[UserDefaults.standard.object(forKey: "Language") as! String] { if selectedLanguage == "English" { UserDefaults.standard.set("en", forKey: "Language") UIView.appearance().semanticContentAttribute = .forceLeftToRight } else { UserDefaults.standard.set("he", forKey: "Language") UIView.appearance().semanticContentAttribute = .forceRightToLeft } let objStoryboard = UIStoryboard.init(name: "Main", bundle: nil) let rootNav = objStoryboard.instantiateViewController(withIdentifier: "rootNav") as! UINavigationController UIApplication.shared.keyWindow?.rootViewController = rootNav UIApplication.shared.keyWindow?.makeKeyAndVisible() let objListStoryboard = UIStoryboard.init(name: "CountryListSB", bundle: nil) let objListVC = objListStoryboard.instantiateViewController(withIdentifier: "CountryListViewController") as! CountryListViewController rootNav.pushViewController(objListVC, animated: true) } else { let objListStoryboard = UIStoryboard.init(name: "CountryListSB", bundle: nil) let objListVC = objListStoryboard.instantiateViewController(withIdentifier: "CountryListViewController") as! CountryListViewController self.navigationController!.pushViewController(objListVC, animated: true) } }

CountryListViewController.swift
Create a variable arrCountry for storing the country’s name in the selected language. and add data in viewDidLoad method as below:var arrCountry = [String]() override func viewDidLoad() { super.viewDidLoad() arrCountry = [AppHelper.getLocalizeString(str: "India"), AppHelper.getLocalizeString(str: "Australia"), AppHelper.getLocalizeString(str: "Mexico"), AppHelper.getLocalizeString(str: "Kenya"), AppHelper.getLocalizeString(str: "China"), AppHelper.getLocalizeString(str: "Bhutan"), AppHelper.getLocalizeString(str: "Canada"), AppHelper.getLocalizeString(str: "Japan"), AppHelper.getLocalizeString(str: "Qatar"), AppHelper.getLocalizeString(str: "Israel")] lblTitle.text = AppHelper.getLocalizeString(str: "Country List") if UserDefaults.standard.object(forKey: "Language") as! String == "he" { self.btnBack.imageView!.transform = CGAffineTransform(scaleX: -1, y: 1) } }
Check Selected Language:
Here we have checked the selected language from UserDefaults and if it is Hebrew then rotate the back arrow image 180 degrees because as said before, in Hebrew language direction is right to left. We will rotate all directional images 180 degrees for the Hebrew language. Write the following function for back button.@IBAction func onClickBack(_ sender: Any) { _ = self.navigationController?.popViewController(animated: true) }
extension CountryListViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrCountry.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let lblCountryName = cell.viewWithTag(101) as! UILabel lblCountryName.text = arrCountry[indexPath.row] let imgSelect = cell.viewWithTag(102) as! UIImageView if UserDefaults.standard.object(forKey: "Language") as! String == "he" { imgSelect.transform = CGAffineTransform(scaleX: -1, y: 1) } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let objDetailStoryboard = UIStoryboard.init(name: "CountryDetailSB", bundle: nil) let objDetailVC = objDetailStoryboard.instantiateViewController(withIdentifier: "CountryDetailViewController") as! CountryDetailViewController objDetailVC.countryName = arrCountry[indexPath.row] self.navigationController?.pushViewController(objDetailVC, animated: true) } }
What’s next?
Here we have set arrCountry data in tableview. And on select cell, push the viewController to the country details view. We have passed countryName to the detail view so that it can be set to the title of that view. Open CountryDetailSB.storyboard. Design title and tableview as below.
var countryName: String = "" override func viewDidLoad() { super.viewDidLoad() lblTitle.text = AppHelper.getLocalizeString(str: countryName) lblDescription.text = AppHelper.getLocalizeString(str: "Country Description Here...") if UserDefaults.standard.object(forKey: "Language") as! String == "he" { self.btnBack.imageView!.transform = CGAffineTransform(scaleX: -1, y: 1) } }
@IBAction func onClickBack(_ sender: Any) { _ = self.navigationController?.popViewController(animated: true) }