Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
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.
 
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.
 
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"));
 
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.
 
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.

It would be nice, however, if Apple included a list of common words in the OS, like Yes, No, Quit, Close, etc.
 
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?
 
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?

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.
 
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?
 
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.
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.