Search code examples
iosswiftlocalizationuikit

How to make iOS app that support different languages


I have to create an app Which will work in different languages and show the content of app in different languages that i select from the language list. I have list of three languages :- 1. English , 2. Hindi and 3. Spanish .

What are the steps to make the app support multiple languages . right now my app is working only with english language.

I'm expecting as i change select a different language the whole text of my app get converted into the selected language.


Solution

  • you have to create the localisation file steps are :

    1. Go to project Navigations
    2. Click on the project
    3. In the Info tab scroll down to see the Localisation section, in that section you can select the language that you want your app to support.
    4. Press the plus icon and select the language and continue.
    5. Now create the string File from the plus icon appearing at the bottom in the navigation pane and the file name must be Localisation
    6. When the new file is created it will look Like the below Screenshot.
    7. Now you will see the localise button in the right side pane. click on that button and continue
    8. You can select any of the languages from the list.
    9. Now you will see your right pane look like below.
    10. Now select all the files that you want to have the localisation content.
    11. You can add the translation in these files with the syntax like below:
    "sign" = "Sign";
    "continue" = "Continue";
    

    and below code in the Hindi localisation file

    "sign" = "साइन इन करें";
    "continue" = "जारी रखें";
    

    and similar content in each localisation file with their translation

    1. Add the below extension to your code :
    extension String {
      func localizeString(string: String) -> String {
        
          let path = Bundle.main.path(forResource: string, ofType: "lproj")
          let bundle = Bundle(path: path!)
          return NSLocalizedString(self, tableName: nil, bundle: bundle!, 
          value: "", comment: "")
      }
    
    }
    
    1. Now you can use this extension to display the localised content as below.
    let langCode = "en"
    yourLbl.text = "sign".localizeString(string: langCode)
    
    1. You have to change the value langCode and pass that value to the localizeString and change the translation according to that file.