How NSLocalizedString works?

Discussion in 'Mac Programming' started by satyam90, Oct 24, 2007.

  1. macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    Hi,

    I am using Objective C with Cocoa Framework on XCode IDE.
    I want to know NSLocalizedString works. I know that it will return the localized version of the string. Does it mean that if I am using Mac with French language version, will it return French equivalent of the string (or) "genstring" application will generate it according to algorithm. Then how to use the other language version according to Mac's language.

    Please let me know immediately.

    Regards,
    Satyam.
     
  2. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #2
    Basically you write in the code the default language string you want to use, and assign it a keyword or phrase to guide another coder or translator:

    Code:
    gAlertLevelHighStr = NSLocalizedString(@"high", @"HTML Condition 4");
    In this case the key phrase is @"HTML Condition 4" and the default language string is @"high". Thus any time there is no localized string available, @"high" is used.

    Now the real magic is done by creating a text file called "Localizable.strings" that is placed in the resources folder that is named for each language you wish to support, English.lproj for English.

    This Localizable.strings file is just a text file with a comment including the translation keyword or phrase, and a pair of literal strings - the default form and the localized form appropriate for each language like so:

    Code:
    /* Update Now Item */
    "Update Now" = "Update Now";
    The matching code is:

    Code:
    menuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Update Now", @"Update Now Item") action:@selector(updateAlertStatusNow:) keyEquivalent:@""];
    Cocoa will look up the Localizable.strings file for the current language, and if found it will look up and extract the language-appropriate string and insert it into place, otherwise the default string literal in the code will be used.

    You don't have to provide a translated string for each string in your code (key/value pairs for Preferences for example), but if it is a User Interface item it should be made to be localizable by using NSLocalizedString to load UI strings.
     
  3. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    You mention French, heres a sample line from Localizable.strings in a fr.lproj folder:

    Code:
    /* Recommended Text Color - Black */
    "Black" = "Noir";
    The comment is a guide so that a translator or other coder can identify this particular string and match it up in the source code files.

    To be complete you can do the same thing in Carbon code (if you use both):

    Code:
    gRecommendColorBlackStr = CFCopyLocalizedString(CFSTR("Black"), CFSTR("Recommended Text Color - Black"));
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Just to make it clear: you generate the localised versions of the strings. The OS will not translate into French or any other language for you.
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    It would be nice, however, if Apple included a list of common words in the OS, like Yes, No, Quit, Close, etc.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    What do you want Yes, No etc for. You're not meant to label the buttons in dialogues with them anyway :p
     
  7. thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #7
    From where the current language will be picked up. Will it depend on the OS language settings or we have to specify the language while building the package?
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    It's picked up from the OS preferences. Note there is not necessarily once chosen language, but rather a list of choices in order. If none are available then the development language is used which is set in Info.plist at build time, and for most software I've seen is English.
     
  9. thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #9
    I have a localization.strings file in english. But when I tried to modify the string in that file, it is not being reflected in the final application that i build. what ever the string that i am changing in code, it is only being reflected. So, how can it be sure that it will pick the correct language localization file once I have distributed the application?
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    The files is called localization.strings in an English.lproj folder? Because that won't work unless you are specifying a custom dictionary to look up in NSLocalizedString. To work with the normal/default call to NSLocalizedString the file must be named exactly Localizable.strings

    I've also found that a single syntax error in the file (failure to close a comment say) or failing to use a UTF-16 Unicode file can cause failures.
     
  11. macrumors newbie

    Joined:
    Sep 13, 2010
    #11
    I have a question too!

    How I can fix the Localized string not found message with the normal commands, copy;paste;select and select all on my Iphone Using as a default language a different language than the English?
     

Share This Page