icould
icould

Reputation: 315

Localized String including emoji codes not working

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.

Upvotes: 0

Views: 1439

Answers (3)

Serge Vysotsky
Serge Vysotsky

Reputation: 131

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"

Upvotes: 1

icould
icould

Reputation: 315

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.

Upvotes: 0

Matija Kraljic
Matija Kraljic

Reputation: 136

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) 

Upvotes: 0

Related Questions