-1

Sir.

I am trying to create an app of multi-language support. I use the abbreviations like zh-Hant, en , jp to save as user default. And change the language of the app. Taking place holder of textfield as an example, if I click the collection view item, the text of place holder of textfield will change. However , I can't do this. The simulator language is English as default

Here is my work in-progress :

Localisation File : Japan (jp)

"User Email"; = "ユーザーメール";

Set Text method

   let lcode : String = self.langList[indexPath.item].code
    print("You selected cell #\(indexPath.item) and code : \(lcode)")
    UserDefaults.standard.setValue(lcode, forKey: "lang")
    txtUsername.placeholder  = "User Email".localized(lang: lcode)

String exntension

extension String {

    func localized(lang:String) ->String {

        if let path = Bundle.main.path(forResource: lang, ofType: "lproj") {
            if let bundle = Bundle(path: path) {
                return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
            }
        }

        return "";
    }
Nishant Bhindi
  • 2,242
  • 8
  • 21
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141
  • The language of the app is something that changes on the device, not inside the app itself. If you want to see localized strings, change the language in the device's settings. – matt Sep 27 '17 at 03:09
  • SO how should I change the app language at the runtime ? – Jeff Bootsholz Sep 27 '17 at 03:19

1 Answers1

0

The language of the app is dependant on the device language. To test the localized string, change your device language. Otherwise, though this is not recommended, you can change your app language on runtime by adding UserDefaults.standard.set("zh", forKey: "AppleLanguages") to your ViewController. But you will have to restart the app to see the language changes.

Ali
  • 481
  • 3
  • 16