iOS Localization: Unicode character escape sequences, which have the form '\uxxxx' does not work

最后都变了- 提交于 2019-11-26 22:58:29

问题


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"?


回答1:


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 - 😀";



回答2:


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

"spanish-key" = "Español";


来源:https://stackoverflow.com/questions/23452906/ios-localization-unicode-character-escape-sequences-which-have-the-form-uxxx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!