Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 552
Smiden Avatar asked Feb 12 '12 17:02

Smiden


People also ask

What is localized string key?

The key used to look up an entry in a strings file or strings dictionary file.

How do I add localizable strings?

To add Localizable. strings file, go to File->New->File , choose Strings File under Resource tab of iOS, name it Localizable. strings , and create the file. Now, you have a Localizable.

How does NSLocalizedString work?

NSLocalizedString is a Foundation macro that returns a localized version of a string. It has two arguments: key , which uniquely identifies the string to be localized, and comment , a string that is used to provide sufficient context for accurate translation.


1 Answers

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.

like image 143
Jim Rota Avatar answered Sep 22 '22 12:09

Jim Rota