Localization with NSLocalizedString or in .xib - ios

I'm looking for an information that I haven't found on the Internet. I would like to know in which iOS version did NSLocalizedString appear ? (maybe it's here since the first version of iOS ?)
I also would like to know in which case using localization in the .xib and in which case using NSLocalizedString ?
I'm asking this question because I'm working on an iOS project using localization in the .xib files AND NSLocalizedString, and I don't really know why the two methods are used... (it's an old project, that's why I'm asking the first question ^^)
Last question, for a new project, which one of these two methods would be your preferred one ?
Sorry for all this questions, and thanks for the answer ! =)

Localisation using xib files is used when we want to localise elements on a UI e.g., when you want to show different images which has a localised text for a particular UIButton. You can achieve this using localised xib files.
There will be separate xib files for each language.
And when you are setting any text on the UI from you controller, NSLocalizedString.
I hope this helps.

NSLocalizedString was first available in 10.0
So I localize programmatically w/ NSLocalizedString because this way I can have all of my strings in one location.
Instead of through .xib localization generation, because with that you'll have a strings file for each xib you have.
However, if you have a .xib that you aren't modifying programmatically (i.e you don't have a custom class set as the file's owner), then you won't be able to programmatically set the NSLocalizedStrings. In which case, you will have to generate the strings files through the .xib.
Also, it is slightly more difficult to access string files associated with a .xib because it's directly connected with the .xib

NSLocalizedString exists since iOS 1.
Basically you need NSLocalizedString for all dynamically created strings.
In a new project I'd recommend AutoLayout and Base Localization.
There is one .xib file and all localizations – and layout adjustments – are performed at runtime via .strings files

Probably this question is dead but I jumped here searching for a method to localize xib strings and this page was the first result.
You can use both NSLocalizedString and xib. Have a look a this answer:https://stackoverflow.com/a/21443515/907720 discussed in detail here: https://medium.com/zendesk-engineering/ios-how-to-add-adaptive-constraints-to-support-a-universal-app-273663475b12

Related

Why is my main storyboard not picked up for localisation when I add a new language?

I am starting to localise my app to other languages but when I add a new language in Xcode 9, only my launch screen storyboard is picked up for localisation not my main storyboard, I had expected both to be picked up for localisation, how do I get my main storyboard included for localisation?
Select your storyboard -> In file inspector there is a button "Localize", i believe that is what you need.
I wouldn't recommend localizing your app using Storyboard.
Why: In future, you won't be able to send a storyboard to the interpreter to translate it. It's slower and heavier. It takes more time.
Instead, there is another solution. Use NSLocalizedString to do localization. Then you will generate the file with translations and have 1 file for 1 language of the app. It's a common practice among developers and I always have been doing it that way.
Check out this tutorial it seems to be good from what I've looked through: link.

Localize several storyboards

I have a project which is relatively big, for such reason I use several storyboards. The thing is, I have not find the way to localize several storyboards in xcode. The options I see when using Base Internationalization are only related to my Main.storyboard and the LaunchScreen.storyboard files.
My app should be in english and spanish, those are the requirements. I have searched and searched but have not found the way to have .strings (english and spanish) for every storyboard, except the Main.
My storyboards are located in disk (and project structure) like this:
General_UI_Folder
Main.storyboard (with its .strings)
LaunchScreen.storyboard (with its .strings)
Subfolder1
...several .storyboards
Subfolder2
...several .storyboards
I'm using Xcode 8 and Swift 3.
I would appreciate any help, since I'm somehow new to iOS development.
You have 2 choices.
1) You localize each storyboard, using the storyboard localization system where it creates "substoryboards", 1 per language, where you change the value of labels, and other components, straight into the storyboards themselves.
You have to use this option:
2) Use the Localizable.strings files and update your view in every viewDidLoad method of your controllers.

Adding new language to existing localized app

I have an app that uses localization, I need to add one more language, I want to display two different language images of same name in one xib file, the xib file is not localized as I am localizing the images. Can it be done using interface builder, without writing any code?
You can add one more language by click on + button.Hope this will help you.But you have to make multiple XIB's as you are saying to set the images only in one XIB i think it is not possible.
Add the images to your localized subdirectories in your project (en.lproj, es.lproj, etc.) and iOS will pick up the correct image when it unarchives the localized xib file.
Xcode can do this for you automatically: Just select the resource you want to localize (an image, xib file, etc.), display the File Inspector pane, and then click the Localize button.
(As a side note, it's a bad practice to localize a high number of images. You should remove text from images and choose them so they are "neutral" and "understandable" in every region of the world. Your app size will thank you for it.)
To do it without code you must localize the xibs, otherwise you will require code from you or others and some configuration. It's easier as follows:
Assuming you have added already localizations to the project (you can do it in Project > Info tab) you just need to select the image in the project tree and check the desired localizations in the right sidebar (cmd-alt-1, localizations).
Replace the image in each *.lproj directory with the localized version. Then in your viewDidLoad use -[UIImage imageNamed:] as usual to load an image into the xib image view or whatever you are doing.

Localized Storyboard: How to add new content?

I just started to use Localization in my app. I now see that there are multiple "MainStoryboard.storyboard" files, for each language one. I can easily change the text.
But what happens when I want to extend the storyboard with a new view? I expected it to be duplicated into all storyboard files, but that doesn't seem to be the case?
Any suggestions?
Thank you.
You should be using Base Internationalization (see docs). The idea is to build up a flexible UI in your storyboards and then rely on Xcode / iOS to substitute in appropriate text. Instead of having a storyboard per language, you would have a single Base storyboard and a set of language strings files.

Maintaining different localized versions of interface NIB files

I'm learning the basics steps to internationalize an app and I have a question about localizing interface NIB files... I've seen that in Xcode you can click on a AppViewController.xib interface file and click on the + in the Localization section of the inspector window to add another language along with the default English one (I add Italian for example)... This allows me to translate the interface in Italian... but then how do you maintain future evolution of the App? If you need to add a button, for example, you have to add it by hand to all the localized versions of your interface NIB file? Documentation says also
Objects in a nib file typically have connections between them that should not be broken. Make sure you lock all connections before handing your nibs off to translation. For information on how to lock down your nib file, see “Localization” in Interface Builder User Guide.
Can you explain me this concept? I've seen the Localization Locking section within the inspector which allows you to lock Properties, Localizable Properties or Un-localizable Properties, but I don't understand very well what it does.
So, to sum up a bit, I'd like you to suggest me what is the workflow to design localized versions of your interface and maintain the multiple NIB files when the interface evolves.
EDIT: any idea and/or link that can help me?
iLocalize provides an easy way of localising an app and updating translated nibs. If you'd rather use ibtool, --localize-incremental, which will handle the changes in a nib and copy them to the translated nibs, is explained well here.
Alternatively, you could use NSLocalizedStrings and one nib which is useful in some instances. Set each control's title in code and you need only worry about making the controls wide enough for the longest translation.

Resources