Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 24, 2007, 07:00 AM   #1
satyam90
macrumors regular
 
Join Date: Jul 2007
Location: Bangalore, India
Send a message via Yahoo to satyam90
How NSLocalizedString works?

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.
satyam90 is offline   0 Reply With Quote
Old Oct 24, 2007, 08:10 AM   #2
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: Austin, TX
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.
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me
Sayer is offline   0 Reply With Quote
Old Oct 24, 2007, 03:30 PM   #3
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: Austin, TX
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"));
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me

Last edited by Sayer; Oct 24, 2007 at 03:38 PM.
Sayer is offline   0 Reply With Quote
Old Oct 24, 2007, 06:59 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Oct 24, 2007, 07:04 PM   #5
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by robbieduncan View Post
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.
kainjow is offline   0 Reply With Quote
Old Oct 24, 2007, 07:42 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by kainjow View Post
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
robbieduncan is offline   0 Reply With Quote
Old Oct 26, 2007, 12:25 AM   #7
satyam90
Thread Starter
macrumors regular
 
Join Date: Jul 2007
Location: Bangalore, India
Send a message via Yahoo to satyam90
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?
satyam90 is offline   0 Reply With Quote
Old Oct 26, 2007, 12:50 AM   #8
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by satyam90 View Post
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.
robbieduncan is offline   0 Reply With Quote
Old Oct 26, 2007, 05:28 AM   #9
satyam90
Thread Starter
macrumors regular
 
Join Date: Jul 2007
Location: Bangalore, India
Send a message via Yahoo to satyam90
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?
satyam90 is offline   0 Reply With Quote
Old Oct 26, 2007, 06:50 AM   #10
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Sep 13, 2010, 08:51 AM   #11
strawb
macrumors newbie
 
Join Date: Sep 2010
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?
strawb is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
It works! iamMacPerson Mac Pro 4 Mar 23, 2014 03:23 PM
How MR Works cclloyd Web Design and Development 8 Nov 26, 2013 11:19 PM
MS Works 4.0 mike0000 Mac Applications and Mac App Store 3 Oct 18, 2013 01:02 PM
iPhone: How it works twisted-pixel Jailbreaks and iOS Hacks 5 Feb 5, 2013 05:18 PM

Forum Jump

All times are GMT -5. The time now is 01:53 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC