15

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.

Tom
  • 3,899
  • 22
  • 78
  • 137

3 Answers3

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

should read

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

instead.

Volker Mohr
  • 476
  • 8
  • 9
  • 1
    I agree this is the correct answer. Also, the original question is quite valid IMHO. – ToddB Nov 29 '14 at 18:21
  • I have used the same, but not working for me. Can you please help me to solve this. My code is like "lXg-Tj-A7t.normalTitle"="Send"; Where "lXg-Tj-A7t" is objectID of Button – NSP Jun 24 '15 at 10:02
  • In case "xyz.normalTitle" does not work, change the Button Style from 'Plain' to 'Default' in Interface Builder. (Be careful, this will also make the button text smaller, so increase the font size afterwards.) – Jekapa Dec 30 '22 at 09:37
7

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

Dave
  • 7,552
  • 4
  • 22
  • 26
  • So, what is the syntax to do it in localizable strings file for a button title? NOT in code! Again: I can do it for other things, it works, I don't need that explonation. The problem is only with button titles! – Tom Feb 03 '13 at 10:42
  • Localizable.strings does not have any special syntax for button titles. – Gereon Feb 03 '13 at 10:49
  • "KEY" = "String"; /*ALWAYS - in strings file*/ – Daij-Djan Feb 03 '13 at 10:50
  • But for button NEVER works. "21.title" = "It should be the localized text"; //does not work – Tom Feb 03 '13 at 11:18
  • As @Daji-Djan said, the .strings file only maps the base string to the localized string. It has no context about where you're using the text. There is nothing special to do for a button. You need to let us know if you see "21.title" show up or nothing at all, and if you are using Interface Builder or creating the button programmatically. What style button are you using? Did you set an image on the button? etc. What have you tried? – Dave Feb 03 '13 at 15:13
0

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";
Jim Holland
  • 1,180
  • 12
  • 19