'Translating Language programmatically

Just hopped into the world a multiLanguage settings in Xcode/Swift. After a good view on this tutorial I found it very easy to setting up multiple language using the Xcode Base Internationalization (Storyboards). The only problem is I have a progammatically UICollectionView which I don't get to see using the language string file. My titles for my UICollectionView is setup through a LET:

let titles = ["TITLE1", "TITLE2", "TITLE3", " TITLE4", "TITLE5", "TITLE6", "TITLE7",  "TITLE8", "TITLE9", "TITLE10"]

I want to translate the title#'s. But I can't add those to the language string file can I? (Because I don't know the objectID's of those title's? because they are not on my storyboard?)



Solution 1:[1]

Edit: Although this answer works good, if you want an easier tutorial or you want to know more about the topic, I suggest reading my article instead.

Go to Project > Info and check Use Base Internationalization.

Check "Use Base Internationalization" at bottom of Info page

Then, in the localization table, add the languages you wish to use. After adding them they should appear like this:

Multiple localized .strings files in different languages

Now create a new Swift file, and inside there, add all the strings you want to convert like this:

//First the text to display then a comment to describe what the string means
let editStr = NSLocalizedString("Edit" , comment: "Action to modify an element")

Now, in your base file (assuming that is in english), for each string you have write:

"Edit" = "Edit";

The first string must be exactly the one you put in your NSLocalizedString before, the second one is the translation (which can be different obviously). It is fundamental to put the ; at the end of each contiguous line;

Now in your other files (for example in the Italian one), you'll put the exact same code of the previous file but on the right side you'll have the translation:

"Edit" = "Modifica";

When you have to assign a title to something, you assign the one you declared in your file (ie. editStr).

That's pretty much all you need to know.

To immediately test the results click on the name of your app (next to the run button) and click on edit scheme, go to Run > Options > App language and change it to the language you want.


For your specific case, declare multiple NSLocalizedString for each title you have:

let title1 = NSLocalizedString("TITLE1" , comment: "Title1")
let title2 = NSLocalizedString("TITLE2" , comment: "Title2")
let title3 = NSLocalizedString("TITLE3" , comment: "Title3")

and so on...
Then you will add them in your base file and in your translated ones as I showed you above.

When it comes to the UIViewController where you put your UICollectionView, you declare your array as follows:

let titles =[title1, title2, title3]

This picture is from the link you sent me: enter image description here
You declare your array like:

let titles = ["TITLE1"...]

while you should do it like:

let titles = [TITLE1...]

where TITLE1 is the name of the localized string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1