PDA

View Full Version : How NSLocalizedString works?




satyam90
Oct 24, 2007, 07:00 AM
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.



Sayer
Oct 24, 2007, 08:10 AM
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:

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:

/* Update Now Item */
"Update Now" = "Update Now";

The matching code is:

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.

Sayer
Oct 24, 2007, 03:30 PM
You mention French, heres a sample line from Localizable.strings in a fr.lproj folder:

/* 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):

gRecommendColorBlackStr = CFCopyLocalizedString(CFSTR("Black"), CFSTR("Recommended Text Color - Black"));

robbieduncan
Oct 24, 2007, 06:59 PM
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.

kainjow
Oct 24, 2007, 07:04 PM
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.

robbieduncan
Oct 24, 2007, 07:42 PM
It would be nice, however, if Apple included a list of common words in the OS, like Yes, No, Quit, Close, etc.

What do you want Yes, No etc for. You're not meant to label the buttons in dialogues with them anyway :p

satyam90
Oct 26, 2007, 12:25 AM
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?

robbieduncan
Oct 26, 2007, 12:50 AM
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.

satyam90
Oct 26, 2007, 05:28 AM
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?

robbieduncan
Oct 26, 2007, 06:50 AM
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.

strawb
Sep 13, 2010, 08:51 AM
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?