iOS Localization: Unicode character escape sequences, which have the form '\uxxxx' does not work
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"?
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 - 😀";
Just write the string in proper Unicode in Localization.string.
"spanish-key" = "Español";