Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

xcode - NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS)

I've made a strings file named "Localizable.strings" and added two languages to it, like so:

"CONNECTIONERROR" = "Check that you have a working internet connection.";
"CONNECTIONERRORTITLE" = "Network error";

I have also converted the files to Unicode UTF-8 However, when I create a UIAlertView like this:

 UIAlertView *myAlert = [[UIAlertView alloc]
 initWithTitle:NSLocalizedString(@"CONNECTIONERRORITLE",nil)
 message:NSLocalizedString(@"CONNECTIONERROR",nil)                    
 delegate:self
 cancelButtonTitle:@"Ok"
 otherButtonTitles:nil];

the alert view only shows the key text, not the value. It works if I, for example, set a UITextviews text to NSLocalizedString(@"CONNECTIONERROR",nil), but the alert view only displays the key. Anyone know what's wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my case it was because I had mistakenly named the file "Localization.strings" and hadn't noticed (it has to be named Localizable.strings). As explained previously the symptom is because the compiler cannot find the string. Otherwise the cause could be any number of things but usually it's a missing semi colon or quotation mark. These are hard to find when you're doing a lot of localizations at once. The lesson learned is to start building your localization file early on in your development process and build it as you go, so that they are easier to spot.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...