Others

App Localization in Swift

In this article, you’ll learn how to prepare your App to support multiple languages, including resources translation How to Localize your iOS App? Localizing your App is not just adding support of translation. It inclu...

J
Joynal Abedin
4
App Localization in Swift
In this article, you’ll learn how to prepare your App to support multiple languages, including resources translation

How to Localize your iOS App?

Localizing your App is not just adding support of translation. It includes adapting your App Interface to support language and regional specificities. That includes supporting different time zones, regional numeric formats, dates, measurement units, etc. All countries don’t use the same type of calendar to show date and time, some countries use the mile system, others the metric system and so on for the other units.

All these differences have to be taken in account while developing your apps, as part of the Internationalization process, and even during the design stage, as a globalized User Experience design.

N.B: But todays article i will show only translation support.

\"wpengine.netdna-ssl.com\"

 
  1. Add localization files to your Xcode project

Firt you need to create New Project. project name \"LocalizableApp\". Then you need to create a new file named \"Localizable.strings\". iOS will automatically recognize this file as a translation file, which will contains all your translations. This file works with key value pairs, with each key representing a string, one file per language. That's these keys that. will then replace strings in your code. To create this file, go to File->New->File & search for strings file and add name it Localizable.strings  Then clicked next button & finished it. \"\" Now, to add new languages, go to your project's settings, under the Info tab, click on + button to add a new language, and select the files you want to localize. \"\" ou can select in wich language you want to write by selecting the localized Localizable.strings file. After added languge check the localization checked mark \"\" 2.Now setup your project interface
//
//  ViewController.swift
//  LocalizableApp
//
//  Created by JOYNAL ABEDIN on 27/8/22.
//

import UIKit

class ViewController: UIViewController {
    
    //MARK: - properties
    
    var frenchButton: UIButton!
    var englishButton: UIButton!
    
    var firstNameLabel: UILabel!
    var secondNameLabel: UILabel!
    var departmentLabel: UILabel!
    
    //MARK: - init LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupLocalizeInterface()
    }
    
    //MARK: - initial interface setup
    func setupLocalizeInterface(){
        englishButton = UIButton()
        englishButton.frame = CGRect(x: 20, y: 100, width: 100, height: 50)
        englishButton.backgroundColor = .blue
        englishButton.setTitle(\"English\", for: .normal)
        self.view.addSubview(englishButton)
        englishButton.addTarget(self, action: #selector(englishButtonTapped), for: .touchUpInside)
        
        frenchButton = UIButton()
        frenchButton.frame = CGRect(x: self.view.frame.width-120, y: 100, width: 100, height: 50)
        frenchButton.backgroundColor = .gray
        frenchButton.setTitle(\"French\", for: .normal)
        self.view.addSubview(frenchButton)
        frenchButton.addTarget(self, action: #selector(frenchButtonTapped), for: .touchUpInside)
        
        firstNameLabel = UILabel()
        firstNameLabel.frame = CGRect(x: 0, y: 220, width: view.frame.width, height: 30)
        firstNameLabel.textAlignment = .center
        firstNameLabel.text = \"Student First name\"
        self.view.addSubview(firstNameLabel)
        
        secondNameLabel = UILabel()
        secondNameLabel.frame = CGRect(x: 0, y: 260, width: view.frame.width, height: 30)
        secondNameLabel.textAlignment = .center
        secondNameLabel.text = \"Student Last name\"
        self.view.addSubview(secondNameLabel)
        
        departmentLabel = UILabel()
        departmentLabel.frame = CGRect(x: 0, y: 300, width: view.frame.width, height: 30)
        departmentLabel.textAlignment = .center
        departmentLabel.text = \"Student Department name\"
        self.view.addSubview(departmentLabel)
        
    }
    
    @objc
    func englishButtonTapped(){
        print(\"✅english button Tapped\")
    }
    
    @objc
    func frenchButtonTapped(){
        print(\"✅french button Tapped\")
    }
    
    
}
Now you set text key & value in localizable file french & english. Which you want to change when user select language. Key Value pair for English \"\" Key Value pair for French \"\" Added localizable extension which return string.
//MARK: - Extension
extension String{
    func localizableString(local: String) -> String {
        let path = Bundle.main.path(forResource: local, ofType: \"lproj\")
        let bundle = Bundle(path: path!)
        return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: \"\", comment: \"\")
    }
}
Finally set the text in textField which translated by localizable.
@objc
   func englishButtonTapped(){
       print(\"✅english button Tapped\")
       firstNameLabel.text = \"firstNameKey\".localizableString(local: \"en\")
       secondNameLabel.text = \"lastNameKey\".localizableString(local: \"en\")
       departmentLabel.text = \"departmentNameKey\".localizableString(local: \"en\")
   }
   
   @objc
   func frenchButtonTapped(){
       print(\"✅french button Tapped\")
       firstNameLabel.text = \"firstNameKey\".localizableString(local: \"fr\")
       secondNameLabel.text = \"lastNameKey\".localizableString(local: \"fr\")
       departmentLabel.text = \"departmentNameKey\".localizableString(local: \"fr\")
   }
Final code Repo: https://github.com/Joynal279/LocalizableApp   Reference by:  https://medium.com/swlh/app-localization-in-swift-ios-swift-guide-baa2c2e4298e
J

Written by Joynal Abedin

Passionate about technology, code, and sharing knowledge.

0 Comments

Leave a Comment