How to combine 2 key for localizable - ios

I have a localizable string file that contains
"please" = "Please";
"try.again" = "try again.";
Inside my application, i would like to use
"please" = "Please";
"try.again" = "try again.";
at the same time for making one sentence = $please + $try.again = Please try again.
So, I have tried this approach
+ (NSString *)pleaseLocalizedString {
return NSLocalizedStringFromTableInBundle(#"please", #"Localizable",[Bundle bundle], #"Please button");
}
+ (NSString *)tryAgainLocalizedString {
return NSLocalizedStringFromTableInBundle(#"try.again", #"Localizable",[Bundle bundle], #"try again button");
}
But with this way again I have to use stringWithFormat to combine these two methods to make one sentence...
How can I make this in an easy way? Any idea?

Do not attempt to combine two separate individually localized strings into one string. Correct sentence formation is not easily generalized across languages. Some languages are right-to-left, while others are left-to-right. Also, certain words may have different meaning when combined, especially in languages like Chinese.
If you need "please" separately in some places and you need "try again" separately in some other places, and you need "please try again" separately in other different places then use three different keys for the three different strings.
If you only need "please try again" and you don't need "please" or "try again" separately, then only have the one key for "please try again".
Having said that, the solution to your question as asked would be to define a third string as:
"p.t.a" = "%1$# %2$#";
Then in some other localization the right side could be something like "%2$#, %1$#";.
Then you would have:
+ (NSString *)pleaseTryAgainLocalizedString {
NSString *format = NSLocalizedStringFromTableInBundle(#"p.t.a", #"Localizable",[Bundle bundle], #"Please Try Again");
return [NSString stringWithFormat:format, [self pleaseLocalizedString], [self tryAgainLocalizedString]];
}
But again, this is just silly. Simply define:
"please.try.again" = "Please try again.";
and:
+ (NSString *)pleaseTryAgainLocalizedString {
return NSLocalizedStringFromTableInBundle(#"please.try.again", #"Localizable",[Bundle bundle], #"Please Try Again");
}
This gives you the most flexibility for translating this string into any other language.

Related

Concatenating Two strings in Localization Objective-c

My Previous Code without Localization. It worked perfect.
case LOGIN_LOGOUT: ((Cell*)cell).lbl.text = [self isLoggedIn] ?
[NSString stringWithFormat:#"Logout %#", email]
:NSLocalizedString(#"Login", #"Message");
break;
But when I implement Localization in Logout the email will not show.
case LOGIN_LOGOUT: ((Cell*)cell).lbl.text = [self isLoggedIn] ?
[NSString stringWithFormat:NSLocalizedString(#"Logout", #"Message") ,"%#",
email] :NSLocalizedString(#"Login", #"Message");
break;
I know I am missing some basics in stringWithFormat but can anyone offer some guidance to me?
Let's assume, that you have .strings file and it contains entry named "Logout". You have:
[NSString stringWithFormat:NSLocalizedString(#"Logout", #"Message") ,"%#", email]
here you try to load format string via NSLocalizedString and use it with NSString. That means that you have to put correct format string into your .strings file, so, if currently you have:
"Logout" = "Logout";
In order to make it just like before localization, you need:
"Logout" = "Logout %#";
If you don't have a .strings file or don't have entry named "Logout", NSLocalizedString will return the key, i.e.
NSLocalizedString(#"key", #"comment") // returns "key"
That means, that your NSLocalizedString(#"Logout", #"Message") may return "Logout" if NSLocalizedString can't find correct entry in your .strings file.
There are more things that may go wrong, if you want some deeper insides on that, I have written great article on the whole topic: Understanding iOS internationalization.
Also I'd suggest to use +localizedStringWithFormat: instead of just plain +stringWithFormat:, because the former uses current locale.
You are looking up the localisation of "Logout". You are using that as a format string. That's not likely to work. Don't make statements that are too complex, it makes it impossible to debug.
I'd write
case LOGIN_LOGOUT: {
NSString* labelText;
if ([self isLoggedIn]) {
NSString* formatString = NSLocalizedString(#"Logout", #"Message");
labelText = [NSString stringWithFormat:formatString, "%#", email];
} else {
labelText = NSLocalizedString(#"Login", #"Message");
}
((Cell*)cell).lbl.text = labelText;
break;
}
And now you can actually debug that whole mess. The stringWithFormat parameters look very, very dodgy.

if statements and stringWithFormat

I need to know if there is a way to use if statements to display certain nsstrings, depending on whether or not that NSString contains any data.
I have an nsstringcalled visitorInfo.
The string uses data from other strings (i.e. which operating system the user is running) and displays that info. Here is an example of what I'm talking about:
NSString *visitorInfo = [NSString stringWithFormat:#"INFO\n\nVisitor Location\n%#\n\nVisitor Blood Type\n\%#", _visitor.location, _visitor.bloodType];
And it would display like this:
INFO
Location
Miami, FL
Blood Type
O positive
However, I have several pieces of data that only load if the user chooses to do so. i.e their email address.
This section of code below would do what I want, but my visitorInfo string contains tons of different strings, and if I use this code below, then it won't load any of them if the user chooses not to submit his blood type.
if ([self.visitor.bloodType length] > 0) {
NSString *visitorInfo = [NSString stringWithFormat:#"INFO\n\nVisitor Location\n%#\n\nVisitor Blood Type\n\%#", _visitor.location, _visitor.bloodType];
}
So basically if their is data stored in bloodType then i went that code to run, but if there isn't any data I only want it to skip over bloodType, and finish displaying the rest of the data.
Let me know if you have any more questions
Additional details. I'm using an NSString for a specific reason, which is why I'm not using a dictionary.
Just build up the string as needed using NSMutableString:
NSMutableString *visitorInfo = [NSMutableString stringWithFormat:#"INFO\n\nVisitor Location\n%#, _visitor.location];
if ([self.visitor.bloodType length] > 0) {
[visitorInfo appendFormat:#"\n\nVisitor Blood Type\n\%#", _visitor.bloodType];
}
You can check if a string has any data in it by using the following
if([_visitor.location length]<1){
//This means there's no data and is a better way of checking, rather than isEqualToString:#"".
}else{
//there is some date here
}
** EDIT - (just re-reading your question, sorry this answer is dependant on _visitor.location being a string in the first place)*
I hope this helps
Try this -
NSString *str = #"INFO";
if (_visitor.location) {
str = [NSString stringWithFormat:#"\n\nVisitor Location\n%#",_visitor.location];
}
if (_visitor.bloodType) {
str = [NSString stringWithFormat:#"\n\nVisitor Blood Type\n\%#",_visitor.bloodType];
}

How do I search an NSString sentence with words separated by commas for a specific word in iOS7?

I came across a question that had an example using rangeOfString. How ever the first thing that came to mind was NSPredicate.
I have different strings that return words separated by sentences. For example I have one that returns "Male, Female".
What is the most efficient way to search either "Male" or "Female". I'd like to perform some actions if the word happens to be part of the sentence and if it doesn't.
NSDictionary with stored words separated by commas. I use different keys to grab specific bunch of words. Below I use the "selectedGenders" key which returns "Male, Female":
if ([combinedRefinementSelection valueForKey:#"selectedGenders"]) {
NSString *selectedGenders = [combinedRefinementSelection valueForKey:#"selectedGenders"];
// Show string in label so customer knows how their clothes items will be filtered
[[_thisController chosenGender] setText:selectedGenders];
}
I simply want to search selectedGenders and find out if Male or Female is part of the string.
As you said, rangeOfString works just fine.
NSString* sentence = #"Male, Female";
if ([sentence rangeOfString:#"Male"].location != NSNotFound)
{
NSLog(#"Male is found");
}
if ([sentence rangeOfString:#"Female"].location != NSNotFound)
{
NSLog(#"Female is found");
}

Check if Text Field NEARLY Matches Set Text? iOS

Does anyone now how I can check if a text field NEARLY matches a set text?
I know how to check if it exactly matches, but i want it to know if its even close to the set text
So if they type HELLO WORD it indicates its close but not exact match?
if (([textfield.text isEqual:#"HELLO WORLD"]))
{
NSLog(#"Correct");
} else {
NSLog(#"Incorrect");
}
This library may be of use to you. And since it's open source, you can check the source to see how it's done. :)
Use this
For Case Insensitive :
if( [textfield.text caseInsensitiveCompare:#"My Case sensitiVE"] == NSOrderedSame ) {
// strings are equal except for possibly case
}
For Case Sensitive :
if([textfield.text isEqualToString:#"My Case sensitiVE"]) {
// Case sensitive Compare
}
You can compare each index of two string and see how many difference is there. And you should define your "nearly match", it may be difference in single character or in multiple character. And decide if you should accept it or reject it.
If you like algorithm Longest Common Subsequence is a key to your goal.. :)
use
NSString caseInsensitiveCompare:
or
- (NSComparisonResult)compare:(NSString *)aString
options:(NSStringCompareOptions)mask`
NSString *string = #"HELLO WORLD I AM JACK";
if ([string rangeOfString:#"HELLO WORLD"].location == NSNotFound) {
NSLog(#"string does not contain HELLO WORLD");
} else {
NSLog(#"string contains HELLO WORLD!");
}

iOS: can't figure out how to write the proper method

I've managed NSUserDefaults and stuck with writing the method to filter the SQL select according to settings in standardUserDefaults.
My app shows 6 continents and user has an option to disable some of them. In my SQL methods I currently trying to manage it this way:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"europe"])
{
sqlContinents="SELECT cont.continentID,cont.continentName,cont.continentImage\
FROM Continents as cont\
WHERE cont.continentID <> 1";
}
It will work if user will disable only europe, but what if he will disable more continents... so I can't figure out how write code that will check which continents are disabled and omit them.
Of cause there's an option to write many "ifs" like
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"europe"] && ....&&......), but I would be very thankful if someone will show me how to implement such method in more smart and elegant way. Thank you in advance.
You can create a WHERE string that holds all conditions before the SQL sentence.
NSString where = #"WHERE";
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"europe"]){
where = [where stringByAppendingString: #" cont.continentID <> 1 "];
}
// Rest of continents...
sqlContinents = [#"SELECT cont.continentID,cont.continentName,cont.continentImage\
FROM Continents as cont " stringByAppendingString: where];
Another approach: sqlite knows the IN in where clauses, so could setup a string with continet IDs like "1, 4, 5" resulting in a statement
sql = [NSString stringWithFormat:#"... WHERE cont.continetID NOT IN (%#)", string];

Resources