2

After creating new cocoa application,I have added localization string files localizeFile.strings(English) and localizeFile.strings(Spanish)

In localizeFile.strings(English) file I added "TITLE" = "Hello!";

In localizeFile.strings(Spanish) file I added "TITLE" = "Hola!";

In my class I was trying to access the localized English text in actions with the following statements

-(IBAction)english:(id)sender
{
   // NSString *engText = [[NSBundle mainBundle] localizedStringForKey:@"TITLE" value:@"" table:nil];
    NSString *engText = NSLocalizedString(@"TITLE", nil);
    NSLog(@"engText %@",engText);
    [displayText setStringValue:engText];
}

-(IBAction)spanish:(id)sender
{
    NSString *spanText = NSLocalizedString(@"TITLE", nil);
      NSLog(@"spanText %@",spanText);
    [displayText setStringValue:spanText];
}

Here displayText is NSTextField and two actions in the same window.

I have seen in the console ,engText TITLE instead of engText Hello on clicking the buttons.

Can somebody guide me how can I get localized strings?Please help

Akbar
  • 1,509
  • 1
  • 16
  • 32
  • Have you looked at all the resources on [this page](https://developer.apple.com/internationalization/#internationalize)? – Rob Keniger Apr 16 '12 at 07:03
  • @Rob,I have seen that document,It is huge one,it is very vast and not mentioned neat procedure to do internationalization.For my understanding purpose,I am trying to do a very basic sample – Akbar Apr 16 '12 at 10:02

1 Answers1

3

It sounds like the application is not aware of localizeFile.strings. Did you declare the languages for the application? Is there a copy rule in your creation profile?

EDIT

You can add language here (red arrow): enter image description here

2nd EDIT

If you want use a language that isn't set on system level, you can do the following:

    // Assume you have defined constants, e.g. 1 - English, 2 - Espanol, 3 - ....
    switch (language) {
        case kLangEn: // =1
            path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
            break:
        case kLangEs: // =2
            path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
            break:
           // etc.
    }
    NSBundle *languageBundle = [NSBundle bundleWithPathath];
    NSString *str = [languageBundle localizedStringForKey:key value:@"" table:nil];
Matthias
  • 8,018
  • 2
  • 27
  • 53
  • Did you declare the languages for the application?No,I don't know how to do this.Can you just brief me about this. – Akbar Apr 16 '12 at 07:00
  • Thanks for sharing the image,but I couldn't to see the image as proxy is blocked.The steps I followed to add the new languages is selected localizedFile.String and in the inspector panel,under localization section,clicked on + sign,from the list I have sleeked the languages English,Japnees and Spanish etc. – Akbar Apr 16 '12 at 07:19
  • Then it should be added in the application configuration automatically, too. Check if the files exist within you application. I.e., your.app/Contents/Resources should contain en.lproj, es.lproj etc. with file "Localizable.strings" in. – Matthias Apr 16 '12 at 07:42
  • yes,it is there,But why is is not returning the correct value – Akbar Apr 16 '12 at 08:31
  • What is the key point for Xcode here for which language it has to return the results?I am not checking any where in the code that user preferred language is English or Spanish – Akbar Apr 16 '12 at 08:47
  • MacOS decides on base of the setting in "System Preferences". I guess, you selected the right one for your test, did you? Sometimes, there are caching problems, and you have to restart your application to adopt to the new selected language. – Matthias Apr 16 '12 at 08:53
  • Thanks Matthias,for spending your time for answering me.Now I have edited by question,please check it once.I have two buttons with the names English and spanish and one textfield.I am doing a sample,If user clicks on English Hello should be displayed in the textfield and when he clicks on Spanish Hola should be displayed in the textfield.Thats what I am trying to build.Displaying the localized text dynamically. – Akbar Apr 16 '12 at 09:02
  • yes,exactly What I was looking for.But I was getting the following NSBundle (not yet loaded).I think because of this only I am not getting the correct value i.e.hello string value. – Akbar Apr 16 '12 at 13:09
  • After a long struggling,I got it working,Thanks for your help,Matthias – Akbar Apr 18 '12 at 03:57