skip to Main Content

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.

2

Answers


  1. you have to create the localisation file steps are :

    1. Go to project Navigations
    2. Click on the project
    3. in the Info tab you will see the Localisation section, in that section
      you have to select the language that you want your app will support.
      Please check the below image link.
      https://phpout.com/wp-content/uploads/2023/07/DApwB.png
    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.
      https://phpout.com/wp-content/uploads/2023/07/rtDZq.png
    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 appearing.
    9. now you will see your right pane look like the below image.
      https://phpout.com/wp-content/uploads/2023/07/c6h3t.png
    10. now select all the files that you want to have the localisation content.
    11. you can add the translation in these files in the syntax follow 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 in 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.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search