15
votes

I use localization strings to localize UI elements. Everything works, except localizing title of a button.

"21.title" = "It should be the localized text"; //does not work

I think it would be caused by state of button (...forState:UIControlStateNormal...), title can be set by view state. How can I defie it in the localization file?

How can I define button title in localization string? What is the trick?

NOTE: I know how to do it from source code, my question is about how to do it by localization string file. So: i know how to use localization strings to localize UI, except buttons.

3
This is a valid question. - ToddB

3 Answers

36
votes
"21.title" = "It should be the localized text"; //does not work

should read

"21.normalTitle" = "It should be the localized text"; //does work

instead.

7
votes

In Interface Builder, you can set 4 strings, one for each of the states in the "State Config" dropdown.

OR, alternatively, in code, you set the button title for each state:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:NSLocalizedString(@"21.title", @"Norm!") forState:UIControlStateNormal];
[button setTitle:NSLocalizedString(@"21.title-highlighted", @"hi btn") forState:UIControlStateHighlighted];
[button setTitle:NSLocalizedString(@"21.title-selected", @"sel btn") forState:UIControlStateSelected];
[button setTitle:NSLocalizedString(@"21.title-disabled", @"dis btn") forState:UIControlStateDisabled];

Edit: To be clear, you'll add the localization strings into your Localizable.strings file. As long as you copy that into your app, you'll get the substitution; and of course you can support multiple languages. Localization Tutorial and Localize in IB

0
votes

In my case, I had sent the files out to be translated. When they came back there was a subtle error

"0ZD-ku-bT7.title" = “Fechar";

Notice that they have used TWO different double quotes! This breaks the file in subtle way that does not fail in the compiler.

The correct code must be

"0ZD-ku-bT7.title" = "Fechar";