xCode ios5 How to append to a localized string - ios

I'm trying to figure how to append some parameters to a localized string?
The desired result (in a string) is "TITLE-xxxx ddmmyy".
TITLE is to be localized.
xxxx is a calculated number and ddmmyy is a fixed date format.
Thanks for any help!

Given TITLE, myDateFormatter which is a NSDateFormatter and number, which is an int:
NSString* myString = [NSString stringWithFormat:#"%#-%d %#", NSLocalizedString(TITLE, #"comment"), number, [myDateFormatter stringFromDate:date]];
Of course you can change number to an NSNumber or something if you wish so (change %d to %# then).

Related

How should I localise with multiple parameters?

Let's say I have the following string:
[NSString stringWithFormat:#"Booked for %# at %#", colleagueName, time];
And I realise I've forgotten to localise that string, so I replace it:
[NSString stringWithFormat:NSLocalizedString(#"bookings.bookedFor", "Booked for user at time"), colleagueName, time];
Now when doing translations, I find that the language X needs the parameters the other way round; something closer to:
<time> for booking of <colleague> is done.
What is the best way to address the fact that now I need the second parameter of my formatted string to be time and the third to be colleagueName please?
As is often the case, my colleague found the solution almost as soon as I had asked on here! Apparently Objective-C has positional arguments
The positions are 1-indexed so %1$# refers to the first argument.
NSString *firstParam = #"1st";
NSString *secondParam = #"2nd";
NSLog(#"First %1$# Second: %2$#", firstParam, secondParam);
NSLog(#"Second %2$# First: %1$#", firstParam, secondParam);
This prints:
First 1st Second: 2nd
Second 2nd First: 1st
You can try like this:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:#"X"]) {//X is language code like "fr","de"
[NSString stringWithFormat:NSLocalizedString(#"bookings.bookedFor", "Booked for user at time"), time, colleagueName];
} else {
[NSString stringWithFormat:NSLocalizedString(#"bookings.bookedFor", "Booked for user at time"), colleagueName,time];
}

Splitting string into substring in iOS

I have a small doubt on splitting a string into substring.
I want to display State and Country name in one label. i have a string in a service coming from json file "FullAddress": "New Windsor,New York,USA". i want only New York,USA to display in a label.
Use this and get your desire String from Array element 1 and 2
NSArray *strings = [FullAddress componentsSeparatedByString:#","];
Please Use this code it will be help you out
NSString *fullAddress=#"New Windsor,New York,USA";
NSRange range=[fullAddress rangeOfString:#","];
if (range.location!=NSNotFound) {
NSString *string=[fullAddress substringFromIndex:range.location+range.length];//this is address you want
NSLog(#"%#",string);
}

How to replace emoji char to specific string from UITextView.text

A string from textview.text and may have emoji, then i want to replace emoji to some string like [smile] and send to server for save. I already have a map for text and emoji.
self.emoji =
#{
#"[pretty]" : [NSString stringWithFormat:#"%C%C", 0xD83D, 0xDE0A],
#"[smile]" : [NSString stringWithFormat:#"%C%C", 0xD83D, 0xDE03],
#"[angry]" : [NSString stringWithFormat:#"%C%C", 0xD83D, 0xDE20]
}
This map use for display emoji and text in uilabel that string get from server. Now, how could i convert string from textview.text correctly.
If your server support unicode, why don't you use emojis directly in your NSString?
NSString *emojiString = #"This is a cat 😻";
oops, StackOverflow doesn't support unicode!

iOS Currency input field

I would like to have a currency input field in my app where the user can either include the currency symbol (as appropriate for their locale), or not, as they please.
I have a text field set up and I am storing the value in a NSDecimalNumber (which I understand is the recommended way to store currency).
The following code will get me from an NSDecimalNumber to a formatted currency string:
[NSNumberFormatter localizedStringFromNumber:currencyValue numberStyle:NSNumberFormatterCurrencyStyle]
But I can't find a way to do the reverse of that. i.e., take the string that the user has typed into my text field and convert it (if possible) into an NSDecimalNumber. Keeping in mind that the currency symbol may be there (because it came from the function above) or not (because the user didn't bother to type the currency symbol).
What am I missing?
If I can't figure this out I will just not accept any currency symbol at all (i.e., just parse it using the code below). But it seems better to allow the currency symbol.
[NSDecimalNumber decimalNumberWithString:currencyString locale:[NSLocale currentLocale]]
I have the feeling I am missing something. What's the right way to convert back and forth between a localized currency string and an NSDecimalNumber?
If you get an instance of an NSNumberFormatter (instead of using the static method call) you can use the NSNumberFormatter's "stringFromNumber" method to format the currency string and use "numberFromString" to parse that string back into a number.
https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html
If you are sure that the currency symbol, if it exists, will be at the beginning of the string, then you can just use this (string is the NSString that the user enters and number is the NSDecimalNumber that represents the value of the currency)
NSDecimalNumber *number;
if([string hasPrefix:#"0"] || [string hasPrefix:#"1"] || [string hasPrefix:#"2"] || [string hasPrefix:#"3"] || [string hasPrefix:#"4"] || [string hasPrefix:#"5"] || [string hasPrefix:#"6"] || [string hasPrefix:#"7"] || [string hasPrefix:#"8"] || [string hasPrefix:#"9"]) {
// The string does not contain a currency symbol in the beginning so we can just assign that to a NSDecimalNumber
number = [NSDecimalNumber decimalNumberWithString:string];
}
else {
// The string contains a currency symbol at the beginning and we will assign the currency symbol to the currencySymbol variable
NSString *currencySymbol;
currencySymbol = [string substringWithRange:NSMakeRange(0,1)];
number = [NSDecimalNumber decimalNumberWithString:[string stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:#""];
}
This should do the trick:
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setLenient:YES];
NSString* input = #"$3,456.78";
NSNumber *number = [currencyFormatter numberFromString:input];
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithDecimal:[number decimalValue]];

Issues with URL string with parameters

I need to insert string parameters startDate and endDate into URL string and I am trying with stringWithFormat, however xcode complains about many other characters in the URL string which it interprets probably as invalid specifiers.
NSString *urlString = [NSString stringWithFormat:#"https://api.xmltime.com/astronomy?accesskey=l1fMY6Om37&expires=2013-06-05T18%3A00%3A00%2B00%3A00&signature=12P2tr1MyD4NnLjqhWzc%2BpsBKR0%3D&object=sun&placeid=netherlands%2Famsterdam&startdt=2013-06-04&enddt=2013-07-04&geo=0&isotime=0&lang=en&utctime=0&types=all", startDate, endDate];
I put my variables with %# specifiers on the place of 2013-06-04 and 2013-07-04 strings. I guess xcode does not like the other percent signs in the string. What is the right way of setting parameters in this string? Thank you.
Use %% to place % in stringWithFormat.
So your code becomes:
NSString *urlString = [NSString stringWithFormat:#"https://api.xmltime.com/astronomy?accesskey=l1fMY6Om37&expires=2013-06-05T18%%3A00%%3A00%%2B00%%3A00&signature=12P2tr1MyD4NnLjqhWzc%%2BpsBKR0%%3D&object=sun&placeid=netherlands%%2Famsterdam&startdt=2013-06-04&enddt=2013-07-04&geo=0&isotime=0&lang=en&utctime=0&types=all", startDate, endDate];

Resources