16

We have key-value pair in Localization.string file.

"spanish-key" = "Espa\u00f1ol";

When we fetch and assign to label then app displays it as "Espau00f1ol".

Doesn't work.

self.label1.text= NSLocalizedString(@"spanish-key", nil);

It works- shows in required format.

self.label1.text= @"Espa\u00f1ol";

What could be the problem here when we use

NSLocalizedString(@"spanish-key", nil)?

If we set \U instead of \u, then it works.

 "spanish-key" = "Espa\U00f1ol";

When to use "\Uxxxx" and "\uxxxx"?

Gaurav Borole
  • 796
  • 2
  • 13
  • 32

2 Answers2

40

NSString literals and strings-files use different escaping rules.

NSString literals use the same escape sequences as "normal" C-strings, in particular the "universal character names" defined in the C99 standard:

\unnnn      - the character whose four-digit short identifier is nnnn
\Unnnnnnnn  - the character whose eight-digit short identifier is nnnnnnnn

Example:

NSString *string = @"Espa\u00F1ol - \U0001F600"; // Español - 

Strings-files, on the other hand, use \Unnnn to denote a UTF-16 character, and "UTF-16 surrogate pairs" for characters > U+FFFF:

"spanish-key" = "Espa\U00f1ol - \Ud83d\Ude00";

(This is the escaping used in "old style property lists", which you can see when printing the description of an `NSDictionary.)

This (hopefully) answers your question

When to use "\Uxxxx" and "\uxxxx"?

But: As also noted by @gnasher729 in his answer, there is no need to use Unicode escape sequences at all. You can simply insert the Unicode characters itself, both in NSString literals and in strings-files:

NSString *string = @"Español - ";

"spanish-key" = "Español - ";
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks for great explanation. Still I want to know which method is recommended when it comes to localization strings file. We have to use unicode escape sequences, because strings file is ready which contains unicode escape sequences. Is it possible to get apple document reference where these things mentioned? – Gaurav Borole May 04 '14 at 16:26
  • @GauravBorole: It is recommended in https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/Articles/StringsFiles.html that the strings files are saved in UTF-16 encoding, which means that you can use all characters without any escape sequence, as in `"spanish-key" = "Español - ";`. But it is completely your choice, both methods work. – Martin R May 04 '14 at 16:33
  • @MartinR Good explanation, but what is the solution when a localized string should contain an escape sequence for a unicode character, other than typing the character out? – Manuel Sep 01 '17 at 21:30
  • Just copy-pasting the character even works with invisible characters; if you go to Edit > Symbols and Emoji, and type U+xxxx into the search, it will let you copy-paste that Unicode character. (note that the character will be invisible till you click on it in the character table). (This is needed in Swift since Objective C escape sequences are treated as a build error in Swift, if you define your localized string values in code) – DivideByZer0 Nov 17 '17 at 00:00
4

Just write the string in proper Unicode in Localization.string.

"spanish-key" = "Español";
gnasher729
  • 51,477
  • 5
  • 75
  • 98