6

In an application I only want to support German language. This is fine with all of the texts that I define myself, but I cannot change the texts displayed by views provided by the system, such as the cancel button of a UISearchBar:

UISearchBar with English localization

If the device language is set to German, the cancel button shows "Abbrechen" correctly. However, if I set the device language to English (or to any other language), the button shows "Cancel", which is incorrect since everything in the application is in German, so I want to have this button in German as well. Is it possible to get this done? What do I do wrong?

  • I only have a de.lproj folder, and I do not have an en.lproj folder. I have the InfoPlist.strings and my main nib files inside the de.lproj folder.
  • The nib for the content view of UIViewController whose searchDisplayController property I use to access the search bar is also inside the de.lproj folder.
  • I have set the development region inside the .pbxproj to German:

    developmentRegion = de;
    
  • I have set the "Localization native development region" to "de" inside the Info.plist file:

    <key>CFBundleDevelopmentRegion</key>
    <string>de</string>
    
atxe
  • 5,029
  • 2
  • 36
  • 50
Imre Kelényi
  • 22,113
  • 5
  • 35
  • 45

3 Answers3

0

Your solution is sufficient, you don't need to mess with AppleLanguages.

If you have taken all the steps you mention in your question, than the problem probably was that you had en.lproj already on your device (or in simulator) from previous builds. What you need to do is remove the app from the device (or simulator), run Product > Clean in Xcode and install it again.

Those are the steps that worked for me:

  • Only have a de.lproj folder in project (no en.lproj).
  • Set developmentRegion = de; in project.pbxproj.
  • Set knownRegions = ( de, ); in project.pbxproj.
  • Set "Localization native development region" to "de".
Marián Černý
  • 15,096
  • 4
  • 70
  • 83
0

You can change the UISearchBar's placeholder text easily by setting it's placeholder property.

However, there aren't any easy hooks to get to the cancel button.

A way you can access the search bar's cancel button is by going through its subviews:

UIButton *cancelButton = nil;

    for(UIView *subView in [searchBar subviews])
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
            [cancelButton setText:@"Abbrechen"];
        }
    }
Mutix
  • 4,196
  • 1
  • 27
  • 39
  • Thanks. I was aware of this solution but it is only a "local" fix for the search bar and if your app uses more views provided by the system, you have to "hack" them all one by one. I'm looking for the correct way to force the system to use a particular localization for all elements. – Imre Kelényi Dec 07 '11 at 07:18
0

I have managed to solve this by hard coding the application language in the program's main function, as suggested by this answer.

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSArray arrayWithObject:@"de_DE"] forKey:@"AppleLanguages"];

    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

This, however, only solves the problem if you have only one localization.

Community
  • 1
  • 1
Imre Kelényi
  • 22,113
  • 5
  • 35
  • 45