Localized String including emoji codes not working - ios

My question is while
NSLocalizedStringFromTableInBundle(#"Sample Text", #"Localizable", [Globals GetLocalizebundle], #"")
is working perfect and I get Localised string from file but
NSLocalizedStringFromTableInBundle(#"Sample Text \U0001F431", #"Localizable", [Globals GetLocalizebundle], #"")
can't get Localised text from bundle.
Any help appreciated.

I solved this by replacing whole unicode code with "surrogates".
For example 😁 has code "1F601" it's surrogates are D83D and DE01. So 😁 should be localised as "\UD83D\UDE01"

Don't use translated text for key, use something like
"sample_text_emoji" = "Sample Text \U0001F431";
in your localized.string file and then use
NSLocalizedStringFromTableInBundle(#"sample_text_emoji", #"Localizable", [Globals GetLocalizebundle], #"")
Documentation clearly states this is a key, so use it as a key, not a text
NSString *NSLocalizedStringFromTableInBundle(NSString *key, NSString *tableName, NSBundle *bundle, NSString *comment)

Actually, below solved my question but I still don't believe it is a proper solution. Anyway here the code resolves emoji characters for NSLocalizedStringFromTableInBundle:
NSString *str = NSLocalizedStringFromTableInBundle(#"Sample Text", #"Localizable", [Globals GetLocalizebundle], #"");
NSString *stringWithEmoji = [str stringByAppendingString:#" \U0001F431"];
NSData *data = [stringWithEmoji dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *valueUnicode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoji = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];
Where base Localized.string includes "Sample Text" = "Sample Text";
The method is to get Localised text from bundle and then add emoji Unicode to string. Then convert it to NSNonLossyASCIIString. This method is working if you are using same emojis for every language.

Related

How to show special character in UILabel iOS

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.
How to get rid of these. If anybody could help. Thanks.
Edit: When i am getting my response it logs that Below result
Message = (
"Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min"
);
But when i extract the value according to key from Dictionary it shows
España:1.3¢/min
It means when i am getting the value from dictionary it cant do proper decoding.
how to resolve this. Any idea..?
First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.
NSString *string = #"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;
UPDATE 1
I think there is some error from your response, Please see below
NSString *string = #"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"finalString = %#", finalString);
Output of above code is,
finalString = Nuša Florjančič
UPDATE 2
If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,
NSString *string = #"Espa\\u00F1a";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",finalString);
Output is España

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.
Below is the code what i have used to support Emoji, after that Arabic text is not showing.
+(NSString *)emojiToSave:(NSString *)str
{
NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
return goodMsg;
}
Can anyone pls suggest to give support for Arabic what change i should do?
Thanks in advance.
Try convert it into base64 code, then insert base64 code to database:
//Original string to base64 string
NSString *emojiString = #"مرحبا 😀 Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
NSLog(#"Result: %#",originalString);
Output:
You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.
You should use NSUTF8StringEncoding everywhere, and you're fine.
Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

\n for NSString not work for string from NSData

I retrieve NSData from a server containing \n. When I convert it into an NSString, \n is not working. It prints "\n".
My code is like this:
NSString *st=[NSString stringWithFormat:#"%#",data];
Thanks
Try with this:
NSString * string = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding];
That may help you.
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#" "];

convert unicode characters while data loading in uiwebview

{ disclaimertxt = "<b>sample tex\U221a\U00a9t n\U221a\U00a9ewerv\U221a\U00a9e iew adults.</b>
\n<br/>
\n<br/>
\nthis is sample \U221a\U2020 an d\U221a\U00a9convertion of the language\U221a\U00a9reduction\U201a\U00c4\U00b6
\n ";
}
the above one is dictionary which contains above value with key disclaimertext
but in output the unicode characters is replaced with ?(C)" and "?A
and my code is :
NSData *messagedata = [dictionary[disclaimertxt] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *string = [[NSString alloc]initWithData:messagedata encoding:NSUTF8StringEncoding];
so i want to display those unicode characters with respected text/value while loading in uiwebview.. plz give ur suggestions. in ios7 its working fine with desired output what i need. but when i run ios6 aim getting above outout.so plz find solution.
Follow these steps:
manually load the HTML from the page that doesn't include the meta tag, into a string.
Using string manipulation, insert your appropriate encoding meta tag into the manually loaded HTML
set the HTML in your web view to the modified string using loadHTMLString:
Use this to convert Unicode charaters to NSString:
char cString[] = "This isn\u2019t Test String";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now use this string in your Webview.

Chinese Character Encoding Issue ios

I have one app in that I have support four Languages. In that When I am login with Chinese User name at that time it shows me Response like this ..
[{"0":"41","intid":"41","1":"\u8a00\u3046","varfirstname":"\u8a00\u3046","2":"\u8a00\u3046","varlastname":"\u8a00\u3046","3":"\u5730","varusername":"\u5730","4":"abc#gmail.com","varemailid":"abc#gmail.com","5":"qwert","varpassword":"qwert","6":"12345","varmobileno":"12345","7":"Enable","mobileMessage":"Enable","8":"","varphoneno":"","9":"Enable","enumstatus":"Enable","10":"2013-01-30","date_insert":"2013-01-30","11":"2013-01-30","date_edit":"2013-01-30","12":"1.38.28.36","varipaddress":"1.38.28.36"}]
I want to Show "varfirstname" to UITextfield Text . But I am not getting any Text when I print it in NSLog .
NSLog(#"Text is === %#",textfname,text);
How can I decode this Text? And show it on UITextfield or UILabel.
I just searched it and found one of the useful Answer from here.
It's natural that Chinese and Japanese characters don't work with ASCII string encoding. If you try to escape the string by Apple's methods, which you definitely should to avoid code duplication, store the result as a Unicode string. Use one of the following encodings:
NSUTF8StringEncoding
NSUTF16StringEncoding
NSShiftJISStringEncoding (not Unicode, Japanese-specific)
UPDATE
For Example you can encode Decode your chinese String like below:
NSString * test = #"汉字马拉松是";
NSString* encodedString =[test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"====%#",encodedString);
OUTPUT IS:
%E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF
Then Decode it like:
NSString* originalString =[encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"====%#",originalString);
OUTPUT IS:
汉字马拉松是
NSString *abc = #"\u8a00\u3046";
NSLog(#" %# " , [NSString stringWithUTF8String:[abc UTF8String]]);
and if you use json :
NSString *html = #"\u8a00\u3046";
NSData *jsonData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %# " , [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
they all output "言う" I think it is Japanese

Resources