Special - Chinese characters in string - ios

I am doing language translation in code.
self.title.text = [NSString stringWithFormat:NSLocalizedString(#"Q%ld", nil), (long)quizNumber];
I have added localization which works fine in French case but in Chinese '%ld' comes on the screen.
If I put the chinese string in place of english string, I get error
"data argument not used by format string"
Any pointers? Should I use some kind of encoding?

I have done localisation in my app in Chinese as well, no problem so far, but I use mostly %d, not %ld.
Can you try using %d instead?
self.title.text = [NSString stringWithFormat:NSLocalizedString(#"Q%d", nil), (int)quizNumber];
Take a look at
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Related

Convert UTF string to Chinese Language from JSON response

I tried everything to convert JSON response to Chinese language but not getting any success. I need to display those string in uilabel.
This is the response I'm getting:
sentence = "\U00e6\U201a\U00a8\U00e5\U00a5\U00bd\U00e3\U20ac\U201a";
pinyin = "n\U00c3\U00adn h\U00c4\U0192o"
Converting sentence's string should be like 您好 but I'm getting 您好。
For pinyin I'm getting exactly right string [[nín hăo]] in label without converting but for sentence it gives me wrong value.
I'm using XCode 7.1 and my deployment target is 8.0.
Hi thanks all for helping and trying :) i ended up solving my own problem. What I did is directly put dict value to label text rather than passing from NSString. Taking it into string will give me value like 您好。
Here is what i've done.
cell.lblWord.text = [NSString stringWithFormat:#"Word: %#",[[dic objectForKey:#"cat"]objectForKey:#"chart"]];
It's strange but true, tried before but wasn't working.

Convert Unicode Emoji to NSString (Not \ue415 format)

I am trying to convert an emoji to an NSString. I previously asked a question on how to do the opposite (convert an NSString to a unicode) at NSString to Emoji Unicode. I thought perhaps it would be best to ask this as a new question here.
How can I convert an NSString containing an emoji (😃) to an NSString containing a unicode in this format (U0001F603)?
This question is basically the reverse engineering of the solution from the previous page. The catch is the project does not use the \ue415 format, but rather the U0001F603 format.
Edited per comment:
2014-07-11 11:37:19.448 emoticon[******] unicode: 😂
unicode = [NSString stringWithFormat:#"%#\\UFE0E", unicode];
2014-07-11 11:37:19.449 emoticon[******] unicode: 😂\UFE0E
SECOND COMMENT RESPONSE
I'm not entirely sure if I follow what you mean by I didn't add the first line of code. I hope I haven't been unclear. To try and be more specific on what I would like, I logged your code in, and then logged what I wish to get:
NSString *first = #"😃";
NSString *second = #"😃\\UFE0E";
NSString *third = #"U0001F603\\UFE0E";
2014-07-11 12:00:45.815 emoticon[******] first: 😃, second: 😃\UFE0E, third: U0001F603\UFE0E
2014-07-11 12:00:45.816 emoticon[******] desiredString: U0001F603
My hope is to produce the desiredString by converting the emoji to the desired string.
THIRD COMMENT RESPONSE
What you need is using the escape character \U0000FE0E to the end of all Unicode characters to make it skip the emoji and display the proper Unicode character.
Here's the code:
#"😃" //This shows the colorful emoji icon.
#"😃\U0000FE0E" //This shows the good old Unicode character.
You can also add it to the character code:
#"U0001F603\U0000FE0E"
Here is a "pseudo-code" (JavaScript, you can run it in your browser's console) ..for opposite-direction.
String.fromCharCode(
((0x1F603 - 0x10000) >> 10) | 0xD800
,
((0x1F603 - 0x10000) % 0x400) | 0xDC00
)
=>>"😃"
Just reverse the bytewise operations, and zero-pad it.
If you are a programmer it should be more than easy for you.
..give a man a fish...
source: JavaScript Ninja - Easy Unicode Emoji Generator 😁🌠🐬

iOS strings file collision - Same string in source langage, different in target

So, I'm localizing an app from japanese to english.
In japanese, there is no distinction between (say) "Mr." and "Ms."(/"Mrs."), so I need to do something like this:
/* Salutation format for user (male) */
"%#様" = "Mr. %#";
/* Salutation format for user (female) */
"%#様" = "Ms. %#";
As you can see, the Japanese localization uses the same string in both cases. I was under the impression that, when the strings file contains more than one entry with the same 'source' (i.e., left side) string, the 'comment' was used to determine which one was employed. It turns out I was wrong, and the comment parameter is just an aid for human translators, totally ignored by NSLocalizedString().
So if I run the app, both of the code paths below produce the same string, regardless of the user's gender:
NSString* userName;
BOOL userIsMale;
/* userName and userIsMale are set... */
NSString* format;
if(userIsMale){
// MALE
format = NSLocalizedString(#"%#様",
#"Salutation format for user (male)");
}
else{
// FEMALE
format = NSLocalizedString(#"%#様",
#"Salutation format for user (female)");
}
NSString* salutation = [NSString stringWithFormat:format, userName];
So, how should I deal with a case like this?
Well, actually “left side” of the NSLocalizedString is a key. So you could do something like:
NSLocalizedString(#"SalutForUserMale", #"Salutation format for user (male)");
NSLocalizedString(#"SalutForUserFemale", #"Salutation format for user (female)");
Then in your base *.strings file (Japanese I presume) you would have:
"SalutForUserMale" = "%#様";
"SalutForUserFemale" = "%#様";
And in your English *.strings file you would have:
"SalutForUserMale" = "Mr. %#";
"SalutForUserFemale" = "Ms. %#";
The Localizable.strings files are nothing more than key value lists. You are using the Japanese phrases as keys which is unusual but I guess it works. I assume this is because the original app was developed in Japanese(?). I usually use English phrases keys, but whatever.
The point is that if you want two different phrases in even just one translation you have to have two different keys in all your translations. If there is something in your base language that is not distinguished but in the translations it is, then you can just "split" an existing key in two new ones. Just change it slightly or add a number, see below:
/* english .strings file */
"hello_world_key" = "Hello World";
"Yes1" = "Yes"; // same in english but different values in german
"Yes2" = "Yes";
/* german .strings file */
"hello_world_key" = "Hallo Welt";
"Yes1" = "Ja";
"Yes2" = "Jawohl";

IOS Convert URL string to NSString?

I have a problem to convert an URL string, which I extract from XML file to NSString.
The URL string look like this, it looks like odd but it is URL format.
%3CTEXTFORMAT%20LEADING%3D%222%22%3E%3CP%20ALIGN%3D%22LEFT%22%3E%3CFONT%20FACE%3D%22Arial%22%20SIZE%3D%2212%22%20COLOR%3D%22%23000000%22%20LETTERSPACING%3D%220%22%20KERNING%3D%220%22%3E%u53F0%u5317%u7E2323141%u65B0%u5E97%u6C11%u6B0A%u8DEF130%u5DF714%u865F5%u6A13%3C/FONT%3E%3C/P%3E%3C/TEXTFORMAT%3E
However, when I use stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding method, it return nil.
After some experiment and research, seems this URL contain %u cause problem while converting URL and this %u looks like unicode, however, I try to remove all %u then stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding method return a proper string without any problem.
Does anyone know how can I convert this URLstring to NSString properly?
It is Unicode han characters in your urlString thats why it is not converting.
Replace %u to \u and you will get your String.
NSString *str=#"%3CTEXTFORMAT%20LEADING%3D%222%22%3E%3CP%20ALIGN%3D%22LEFT%22%3E%3CFONT%20FACE %3D%22Arial%22%20SIZE%3D%2212%22%20COLOR%3D%22%23000000%22%20LETTERSPACING%3D%220%22%20KERNING%3D%220%22%3E%u53F0%u5317%u7E2323141%u65B0%u5E97%u6C11%u6B0A%u8DEF130%u5DF714%u865F5%u6A13%3C/FONT%3E%3C/P%3E%3C/TEXTFORMAT%3E";
str=[str stringByReplacingOccurrencesOfString:#"%u" withString:#"\\u"];
NSString *convertedStr=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"converted string is %# \n",convertedStr);
output :---------------
converted string is <TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">\u53F0\u5317\u7E2323141\u65B0\u5E97\u6C11\u6B0A\u8DEF130\u5DF714\u865F5\u6A13</FONT></P></TEXTFORMAT>
for more Info follow this url
This is chinese unicode char
here is some code that will prove it:
NSString *newStr=#"\u53F0\u5317\u7E2323141\u65B0\u5E97\u6C11\u6B0A\u8DEF130\u5DF714\u865F5\u6A13";
NSLog(#"chinese string is %#",[newStr stringByReplacingPercentEscapesUsingEncoding:NSUTF16StringEncoding]);
output:----------------------
台北縣23141新店民權路130巷14號5樓
go to google translate converting this string will give you someone's address.
as :-
Citizens Xindian, Taipei County 23141 Road 130, 5th Floor, No. 14, Lane

NSDictionary objectForKey: string issue

I have a NSDictionary created with data from a web api.
Here is the dictionary logged:
{
chapter = {
text = "\n \tAmo\U00cc\U0081s";
};
}
When loging [dict objectForKey:#"chapter"] looks like this:
{
text = "\n \tAmo\U00cc\U0081s";
}
And when logging [dict objectForKey:#"text"] I get
AmoÌs
which is not correct, it should be Amo\U00cc\U0081s / Amós
It seems to be an encoding problem, but I can't figure it out.
Any idea why this is happening?
Thanks
The NSLog is printing correctly!!!
You can not print Unicode text to log. You have new line, a tab and \U00cc and \U0081 which is converting to some un-readalbe texts.
This is not a bug. CF and Cocoa interpret %S and %C differently from how printf and its cousins interpret them. CF and Cocoa treat the character(s) as UTF-16, whereas printf (presumably) treats them as UTF-32.
The CF/Cocoa interpretation is more useful when working with Core Services, as some APIs (such as the File Manager) will hand you text as an array of UniChars, not a CFString; as long as you null-terminate that array, you can use it with %S to print the string.
Copied from here.

Resources