I've created this mini translation of words from English to Spanish. The words read from a Localizable.strings and retrieves the words "cat" = "gato" using NSLocalizedString. At the moment it only read one word from the string, I would like yo know if there's any way to retrieve multiple words from Localizable.string, for example, "the" + "cat" = "el gato"
var myEnglishArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.translateTextField.delegate = self
picker.delegate = self
picker.dataSource = self
//var myEnglishArray = [String]()
if let URL = NSBundle.mainBundle().URLForResource("englishArray", withExtension: "plist") {
if let englishFromPlist = NSArray(contentsOfURL: URL) as? [String] {
for myEnglish in englishFromPlist {
myEnglishArray.append(myEnglish)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func translateButtonTapped(sender: UIButton) {
let emptyString = self.translateTextField.text
if (emptyString!.isEmpty) {
print("please enter a word")
}
for transIndex in myEnglishArray.indices {
if myEnglishArray[transIndex] == emptyString!.lowercaseString {
//englishArray
//translateLabel.text = "\(spanishArray[transIndex])"
translateLabel.text = NSLocalizedString(emptyString!.lowercaseString, comment:"")
print(emptyString)
return
}
}
}
I think that you should use Localization for iOS - not files or arrays or anything like that.
You will need to create several localizable.strings files and use that one as a key, for instance NSLocalizedString("yourKeyForWordYouWantToTranslate", comment: "")
.
So, if you want this code to work, again, you have to create localizable strings and setup your project to have several languages supported. Here is a great tutorial on this case (applies to swift as well):
https://www.raywenderlich.com/64401/internationalization-tutorial-for-ios-2014