0

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.

icould
  • 315
  • 3
  • 9
  • 1
    Have you read http://stackoverflow.com/a/23454538/909655 about how to include unicode characters in strings-files? Especially about code points above 0xFFFF. – Mats Mar 02 '16 at 16:33

3 Answers3

1

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"

  • Unfortunately, this doesn't compile in Xcode 12.2, when put inside a `@""` literal. The complete code is needed, e.g. `\U0001F601` – manmal Nov 24 '20 at 09:33
0

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) 
Matija Kraljic
  • 136
  • 1
  • 4
  • 1
    It doesn't work really. Have you tested your solution? because it is not about the key supplied. It is about the value coming from localized.string file. I have tested `"sample_text_emoji" = "Sample Text \U0001F431"` in localized.string file, but the result on screen is "Sample Text F431" – icould Mar 02 '16 at 15:44
0

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.

icould
  • 315
  • 3
  • 9