1

I created Constants.h and Constants.m as suggested here:

Constants in Objective-C

Now I want to also use some strings defined in Constants.h in Interface Builder to for example set the text of a label.

How to do this using bindings? I think I should somehow use an ObjectController (Mode: Class, Class Name: Constants)? But what would be the Content Object for this controller (since I don't have any variable of type Constants)? Maybe use a Singleton in Constants.m? Any suggestions?

Community
  • 1
  • 1
Tobias
  • 4,034
  • 1
  • 28
  • 35

1 Answers1

2

I don't think you can bind the strings in any way.

I would recommend do it in code in viewDidLoad.

Note that string constants aren't so great for texts in UI.

EDIT:

Xibs have their own localization system but I don't think it's very good. It basically means creating a new xib for every language. If you support only one language, just put your strings into the xib and the problem is solved.

NOTE: the following is an idea for my current project and I haven't implemented it yet but I suppose it would let us easily add new language translations.

My idea for a better xib localization is to define IBOutlet for every localizable component (e.g. myButton1, myTextField1) and then write a file with localized strings (xml, properties, plist whatever) where every string is keyed by the IBOutlet name, e.g:

   myXib1.myButton1.selected.title = This is a button.
   myXib1.myTextField1.placeholder = "This is text field placeholder"

Then, you have to write a method which takes the xib name, finds current language and goes through all string properties for the given xib. It can use [NSObject performSelector:] to access the IBOutlet getters:

id localizableView = [self performSelector:NSSelectorFromString(@"myButton1")];

and you call this method from viewDidLoad (or you create a UILocalizedController class which calls it automatically and all your controllers will be its descendant).

Also note there is NSLocalizedString class which should help you with localization.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Thank you for your answer. What do you think would be a better way? Using the constants as keys to strings stored in a .strings file? Any other idea to not set the strings programmatically? – Tobias Dec 19 '11 at 10:09
  • Answer updated. In short, if you have only one language, forget about constants and put everything into a xib. If you are thinking about i18n, read the extended answer. – Sulthan Dec 19 '11 at 12:40