1

I would like to make my application multilingual, so I have been looking about how to add other languages in an app in Xcode, however I saw the language changes based on the language of your phone.

Is there a way to set a language when a user selects it in the application? If so, is it also possible to remember the selected language for the future? So the user will not have to select it every time when he or she starts the application again.

Thank you in advance

  • I'm not sure on how to change the language, but to use it every time you can save it into `UserDefaults`. – George Oct 25 '18 at 09:01
  • Thank you, I will look that :) Hopefully someone else knows how to change the language. –  Oct 25 '18 at 09:04
  • @AdriánT95 check my answer and feel free to ask about details if you need – Mohmmad S Oct 25 '18 at 09:09
  • There's an excellent answer to this question that was posted earlier: https://stackoverflow.com/a/20257557/10165733; This can be translated to Swift as well by creating a Bundle extension. I'd recommend to go this way for setting the language. You still need to persist the setting in UserDefaults and set it at app startup as mentioned above. – Lutz Oct 25 '18 at 10:39
  • Did you check my answer @AdriánT95? – Reinier Melian Nov 30 '18 at 11:59

3 Answers3

0

Well first you need to have a Localization.Strings file that have multiple languages strings. Read about them here

Second of all you have multiple ways to detect what language user selected when the app starts, the common one for this case is userDefualts read about them here.

Therefore, you can implement the Localization file and use the value saved to detect what language to use from the userDefualts.

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • Thank you @Tobi! The first tutorial has worked and now it works after changing the language of the iOS simulator, however, as I said, I would like to change the language when the user selects it from a button in the Main.storyboard, so I don't want it based on the language of the phone. –  Oct 25 '18 at 09:49
  • as i explained you need to setup a button for example and save a value in userDefaults, based on that change the language retrieved from the localization – Mohmmad S Oct 25 '18 at 09:59
  • yes, but the thing is... How can I save the selected language in userDefaults? Because it does not change when I select it, so previously I would need to know how to force a langauge in order to set the selected language. I would just need userDefaults after relaunching the app, but the button should do its function previously. –  Oct 25 '18 at 10:18
  • Learn the basics of saving and getting valued from userdefualts then you can learn how to use those values to force a language – Mohmmad S Oct 25 '18 at 10:19
0

Localization is simply the process of translating your app into multiple languages.Internationalization is the process of making your app able to adapt to different languages, regions, and culture. 

Refer the link to implement:-

https://codeburst.io/localization-of-ios-app-in-swift-4-and-xcode-9-3c7c7d53ae11

user1376400
  • 614
  • 1
  • 4
  • 8
0

You need to save the application language selected by user in userDefaults by example, this example is using the third party library SwiftyUserDefaults

Using this way you need to add the .strings with the "Localizable_" + initials of language of regular localization way, example

your .string file for Spanish should be named

"Localizable_es" but you can customize that in code too

this are the steps:

Save the app language selected by the user:

func setupAppLanguage(lang:String) {
    Defaults[.appLanguage] = lang
 }

Get saved language:

static func getCurrentLang() ->String
  {
    if(Defaults[.appLanguage] == nil)
    {
        if(NSLocale.current.languageCode == nil)
        {
            return "en"
        }
        return NSLocale.current.languageCode!
    }else
    {
        return Defaults[.appLanguage] as String!
    }
}

Get localized tableName language:

static func getLocalizedTableName() ->String
{
    return "Localizable_\(Client.getCurrentLang())"
}

Method to localize:

//MARK: Localization Util
static func getLocalizedText(toLocalizeText:String) ->String{
    return NSLocalizedString(toLocalizeText,tableName: Client.getLocalizedTableName(), comment: "")
}

Then you can use the getLocalizedText method the same way as you use NSLocalizedString, replacing it

Example of use

self.labelText.text = Client.getLocalizedText(toLocalizeText: "k_glossary")
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55