1

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

        }
    }
}
Miguel
  • 213
  • 1
  • 3
  • 12

2 Answers2

1

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

Miknash
  • 7,888
  • 3
  • 34
  • 46
  • would this work if the user types in "the" and "cat" in the text field and outputs the key value "el gato" using Localization.strings. not looking for the app to be in Spanish but just whatever is inside the textfield read multiple strings @NickCatib – Miguel May 20 '16 at 13:42
  • well yeah, you could define values like "the cat" = "el gato";, so when you call for key "the cat" you will get corresponding in spanish. The question is - do you want to have certain amount of translatable nouns? If you want to have almost indefinite amount of nouns you could use https://cloud.google.com/translate/docs/ as well (the question is will you get correct translation every time :) ) – Miknash May 20 '16 at 13:53
  • I've defined values like "the dog" = " el perro" but I guess there's no way it can pick up different words from the list one by one for example, this is my list, "the" = "el", "cat" = "gato", "dog" = "perro". when users enters the key " the dog" it will look for the words in the list and output all the words together " the dog", " the cat" @NickCatib – Miguel May 20 '16 at 15:24
  • 1
    well this could be a little too much, but you could do the following: to the split on the term - look for each word in the array and assemble it again: for instance: text. componentsSeparatedByString(" "), then for each get translation and then build string again – Miknash May 20 '16 at 15:50
-1

I make an example. How to make Localizable in SWIFT APP

  1. Open Xcode and click File->New->Project

  2. Create a new project and choose Single View Application.

  3. Input "Localizable" as app name

  4. On "Localizable" app name, right click and choose New File.

  5. click "Resource", "Strings File"

  6. input "Localizable.strings" as file name

  7. click "Localize"

  8. please choose "Base" and click "Localize"

  9. To find "info" of "Localizations", and click add "+"

  10. choose your language to your app

  11. choose "Localizable.strings", and click "Finish"

  12. You will have two files called "Localizable.strings"

tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72