iOS localization, label with number and word together - iphone

i want to localize my game. some of my label is like [#"Score: %i", score], and the score can be any number. so can i still use strings file localization?
this is what i do currently, but a lot of work
CCLabelTTF* bestLabelWord = [Helper createLocalizedLabelWithStringUpperCase:#"BEST" color:ccBLACK];
CCLabelTTF* bestLabelNumber = [Helper createUnlocalizedLabelWithString:[NSString stringWithFormat:#"%i", bestScore] color: ccBLACK];
bestLabelWord.anchorPoint = ccp(0, 0.5f);
bestLabelNumber.anchorPoint = ccp(0, 0.5f);
bestLabelWord.position = ccp(menuPosX, menuPosY2);
bestLabelNumber.position = ccp(menuPosX + bestLabelWord.contentSize.width + 5, menuPosY2);
[self addChild:bestLabelWord z:kZLabel];
[self addChild:bestLabelNumber z:kZLabel];
here i separate #"Score" and #"%i", score into 2 labels (word and number) and place them separately.
so how can i put them into one label? can i put like "NSString stringWithFormat" in localization.strings file?

Yes, you can do this. It looks like this (uncompiled; hopefully there is no typo):
NSString *scoreFormat = NSLocalizedString(#"Score: %d", #"Score format");
NSString *scoreString = [NSString stringWithFormat:scoreFormat, bestScore];
Your strings file for this in English would be:
/* Score format */
"Score: %d" = "Score: %d";
There is no difference between %d and %i. They are both specified by the standard. I personally prefer to express that it is "decimal" (vs. %x hexadecimal) rather than that it is an "integer" (vs. %f float). But it does not matter.
You should include the entire format statement in the strings file. You are localizing the entire format statement Score: %d. The rationale for this is to permit languages where the order might be different. For example, if there were a language where the correct translation were "%d score" or a language where to make sense you would have to say the equivalent of Score: %d points or Score: %d number.
If you don't want this kind of localization, and you will always put a colon after the label and a number after that, no matter the language, then you can localize just the label as you were in your original code, or like this:
NSString *localizedLabel = NSLocalizedString(#"Score", #"Score");
NSString *scoreString = [NSString stringWithFormat:#"%#: %d", localizedLabel, bestScore];
You should avoid calling NSLocalizedString with a variable parameter as you're suggesting. This is confusing, and can get in the way of using genstrings to create your strings file. That said, the following code is not a problem:
NSString * const kScoreLabelKey = #"Score";
NSString * const kPriceLabelKey = #"Price";
NSString *GetLocalizedStringForKeyValue(NSString *key, NSInteger value)
{
NSString *label = [[NSBundle mainBundle] localizedStringForKey:key value:nil table:#"Labels"];
NSString *valueLabel = [NSString stringWithFormat:#": %d", value];
return [label stringByAppendingString:valueLabel];
}
...
NSString *label = GetLocalizedStringForKeyValue(kScoreLabelKey, bestScore);
...
(In Labels.strings):
"Score" = "Score";
"Price" = "Price";
Things to note:
I created a separate strings file for this called Labels.strings. That way it doesn't impact using genstrings and the main Localizeable.string.
I used string constants for the keys. That puts all the strings used in one place at the top of the file, making it easy to audit against Labels.strings.

Related

converting language characters [ios] using NSISOLatin1StringEncoding

I am pulling out some language sentences (in different languages) and displaying them in TableView (max 5 rows each row) and when user taps on any row, then I navigate user to a new screen and display full text there.
The problem that i am running into is, it is taking too much time to convert the characters to be visible properly on device.
I wrote the following code to convert the json text for each row:
NSString *msgDesc = [myContentsArray objectAtIndex:indexPath.row];
char const *cStr = [msgDesc cStringUsingEncoding:NSISOLatin1StringEncoding];
msgDesc = [NSString stringWithCString: cStr encoding:NSUTF8StringEncoding];
Thanks for the help.
Regards,
Reno Jones
Since conversion is taking too long, you should move it out from your tableView:cellForRowAtIndexPath: method into the code that pulls the data out from its data source, do the conversion there, and store it for future use.
Add NSMutableArray *myContentsArrayConverted to your class, then convert everything into it, and use in your tableView:cellForRowAtIndexPath: instead of performing the conversion each time you must display your string:
for (int i = 0 ; i != myContentsArray.count ; i++) {
NSString *msgDesc = [myContentsArray objectAtIndex:i];
char const *cStr = [msgDesc cStringUsingEncoding:NSISOLatin1StringEncoding];
[myContentsArrayConverted addObject:[NSString stringWithCString: cStr encoding:NSUTF8StringEncoding]];
}
Now you can replace the slow code with the much faster
NSString *msgDesc = [myContentsArrayConverted objectAtIndex:indexPath.row];

Get first sentence of textview

I am trying to get the first sentence of a text view. I have the following code but am getting an out of bounds error. Thank You. Or are there any ways that aren't really complex.
-(IBAction)next:(id)sender
{
NSRange ran = [[tv.text substringFromIndex:lastLocation] rangeOfString:#". "];
if(ran.location != NSNotFound)
{
NSString * getRidOfFirstHalfString = [[tv.text substringFromIndex:lastLocation] substringToIndex:ran.location];
NSLog(#"%#",getRidOfFirstHalfString);
lastLocation+=getRidOfFirstHalfString.length;
}
How about:
NSString *finalString = [[tv.text componentsSeparatedByString:#"."] objectAtIndex:0] // Get the 1st part (left part) of the separated string
Go through the textview's text and divide the text into separate components where you find a period by calling componentsSeperatedByString on tv.text. You want the first sentence, which would be the 0th object in the array.
I know you've already accepted an answer to this question, but you might want to consider using the text view's tokenizer instead of just searching for the string ". " The tokenizer will automatically handle punctuation like !, ?, and closing quotes. You can use it like this:
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextRange *range = [tokenizer rangeEnclosingPosition:textView.beginningOfDocument
withGranularity:UITextGranularitySentence
inDirection:UITextStorageDirectionForward];
NSString *firstSentence = [textView textInRange:range];
If you want to enumerate all of the sentences, you can do it like this:
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextPosition *start = textView.beginningOfDocument;
while (![start isEqual:textView.endOfDocument]) {
UITextPosition *end = [tokenizer positionFromPosition:start toBoundary:UITextGranularitySentence inDirection:UITextStorageDirectionForward];
NSString *sentence = [textView textInRange:[textView textRangeFromPosition:start toPosition:end]];
NSLog(#"sentence=%#", sentence);
start = end;
}
Try checking that the substring was actually found.
NSRange ran = [tv.text rangeOfString:#". "];
if(ran.location != NSNotFound)
{
NSString * selectedString = [tv.text substringToIndex:ran.location];
NSLog(#"%#",selectedString);
}
You could alternatively try using NSScanner like this:
NSString *firstSentence = [[NSString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:tv.text];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"."];
[scanner scanUpToCharactersFromSet:set intoString:&firstSentence];
I'm not sure if you want this, but since you want the first sentence, you could append a period (you probably know how to do this, but it doesn't hurt to show it anyway):
firstSentence = [firstSentence stringByAppendingFormat:#"."];
Hope this helps!
PS: If it didn't work for you, maybe the text view doesn't actually contain any text.

cut nsstring after a special character in iphone

I'm new in iPhone, I want to cut a string after a special character, lets take this example
"works.php" , I want to remove ".php" from this string meaning that I want to remove what's after "."
what is the code to do that ??
thanks in Advance.
It seems you want to use -stringByDeletingPathExtension.
[#"works.php" stringByDeletingPathExtension] // becomes #"works".
Note that this method deletes path extensions, which is not exactly the same as "remove what's after a dot". Please read the reference I've linked above for detail.
If you really just need to remove the string after the last dot, just use the conventional algorithm of (1) find the last dot (2) get a substring until the last dot:
NSString* input = #"works.php";
NSRange lastDotRange = [input rangeOfString:#"." options:NSBackwardsSearch];
if (lastDotRange.location != NSNotFound) {
return [input substringToIndex:lastDotRange.location];
} else {
return input;
}
For the specific example you can use:
- (NSString *)stringByDeletingPathExtension
The more general way to do what you want is:
NSString *originalString = #"works.php";
NSString *finalString =
[[originalString componentsSeparatedByString:#"."] objectAtIndex: 0];
You can replace the dot character with any character you want.
You can use:
myString = #"anything.php";
myString = [myString stringByReplacingOccurrencesOfString:#".php" withString:#""];
The advantage of this is that you can put anything to remove, not only a path. For example:
myString = #"anything-123";
myString = [myString stringByReplacingOccurrencesOfString:#"-123" withString:#""];
Another approach is NSRange and substringWithRange if you want other removing options..

Uppercase first letter in NSString

How can I uppercase the fisrt letter of a NSString, and removing any accents ?
For instance, Àlter, Alter, alter should become Alter.
But, /lter, )lter, :lter should remains the same, as the first character is not a letter.
Please Do NOT use this method. Because one letter may have different count in different language. You can check dreamlax answer for that. But I'm sure that You would learn something from my answer.
NSString *capitalisedSentence = nil;
//Does the string live in memory and does it have at least one letter?
if (yourString && yourString.length > 0) {
// Yes, it does.
capitalisedSentence = [yourString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[yourString substringToIndex:1] capitalizedString]];
} else {
// No, it doesn't.
}
Why should I care about the number of letters?
If you try to access (e.g NSMakeRange, substringToIndex etc)
the first character in an empty string like #"", then your app will crash. To avoid this you must verify that it exists before processing on it.
What if my string was nil?
Mr.Nil: I'm 'nil'. I can digest anything that you send to me. I won't allow your app to crash all by itself. ;)
nil will observe any method call you send to it.
So it will digest anything you try on it, nil is your friend.
You can use NSString's:
- (NSString *)capitalizedString
or (iOS 6.0 and above):
- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale
Since you want to remove diacritic marks, you could use this method in combination with the common string manipulating methods, like this:
/* create a locale where diacritic marks are not considered important, e.g. US English */
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en-US"] autorelease];
NSString *input = #"Àlter";
/* get first char */
NSString *firstChar = [input substringToIndex:1];
/* remove any diacritic mark */
NSString *folded = [firstChar stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:locale];
/* create the new string */
NSString *result = [[folded uppercaseString] stringByAppendingString:[input substringFromIndex:1]];
Gonna drop a list of steps which I think you can use to get this done. Hope you can follow through without a prob! :)
Use decomposedStringWithCanonicalMappingto decompose any accents (Important to make sure accented characters aren't just removed unnecessarily)
Use characterAtIndex: to extract the first letter (index 0), use upperCaseString to turn it into capitol lettering and use stringByReplacingCharactersInRange to replace the first letter back into the original string.
In this step, BEFORE turning it into uppercase, you can check whether the first letter is one of the characters you do not want to replace, e.g. ":" or ";", and if it is, do not follow through with the rest of the procedure.
Do a [theString stringByReplacingOccurrencesOfString:#"" withString:#""]` sort of call to remove any accents left over.
This all should both capitalize your first letter AND remove any accents :)
Since iOS 9.0 there is a method to capitalize string using current locale:
#property(readonly, copy) NSString *localizedCapitalizedString;
I'm using this method for similar situations but I'm not sure if question asked to make other letters lowercase.
- (NSString *)capitalizedOnlyFirstLetter {
if (self.length < 1) {
return #"";
}
else if (self.length == 1) {
return [self capitalizedString];
}
else {
NSString *firstChar = [self substringToIndex:1];
NSString *otherChars = [self substringWithRange:NSMakeRange(1, self.length - 1)];
return [NSString stringWithFormat:#"%#%#", [firstChar uppercaseString], [otherChars lowercaseString]];
}
}
Just for adding some options, I use this category to capitalize the first letter of a NSString.
#interface NSString (CapitalizeFirst)
- (NSString *)capitalizeFirst;
- (NSString *)removeDiacritic;
#end
#implementation NSString (CapitalizeFirst)
- (NSString *)capitalizeFirst {
if ( self.length <= 1 ) {
return [self uppercaseString];
}
else {
return [[[[self substringToIndex:1] removeDiacritic] uppercaseString] stringByAppendingString:[[self substringFromIndex:1] removeDiacritic]];
// Or: return [NSString stringWithFormat:#"%#%#", [[[self substringToIndex:1] removeDiacritic] uppercaseString], [[self substringFromIndex:1] removeDiacritic]];
}
}
- (NSString *)removeDiacritic { // Taken from: http://stackoverflow.com/a/10932536/1986221
NSData *data = [NSData dataUsingEncoding:NSASCIIStringEncoding
allowsLossyConversion:YES];
return [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
}
#end
And then you can simply call:
NSString *helloWorld = #"hello world";
NSString *capitalized = [helloWorld capitalizeFirst];
NSLog(#"%# - %#", helloWorld, capitalized);

Any Way To Separate Text In UITextField?

I have a paragraph (4 sentences) of text in an .plist array that loads into a UITextView.
By default, it presents the text how it is, as one big lump of text in a paragraph. I want to know if it is possible to split this up?
Such as Line 1: sdfafasfsafsa, then line 2: asfdsafs, line 3: adfsfsdfsdfa, etc.
Is there a way I can search for a . and then separate the lines accordingly? I would just edit the plist manually but there are hundreds of entries so it isn't easy to do.
NSString* tidiedString = [sourceString stringByReplacingOccurrencesOfString:#"." withString:#"\n"];
Update: OK, so more detail is coming through. You could use a regular expression - but if you're not familiar, the learning curve is a bit steep. Otherwise, as with other answers, crank through the list. You need to take care of whitespaces, empty lines etc. The following snippet isn't pretty, but will do the job.
NSString* sourceString = #"Hyperlinks can be great. They can also dilute your focus and tempt you into putting off what you most want to do. Here I chose to place links at the foot of the page to help you to make an active choice as to whether to surf or refocus your attention elsewhere.";
NSArray* arrayOfStrings = [sourceString componentsSeparatedByString:#"."];
NSMutableString* superString = [NSMutableString stringWithString:#""];
int lineCount = 1;
for (NSString* string in arrayOfStrings)
{
if ([string length] < 1) continue;
[superString appendFormat:#"Line %d: %#.\n", lineCount++, [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
[superString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[[self userEntry] setText:superString];
NSArray *array = [sourceString componentsSeparatedByString:#"."];
NSMutableString *resultString= [[NSMutableString alloc] init];
int linecount=1;
for(NSString *lines in array)
{
[resultString appendString:[NSString stringWithFormat:#"Line%i:%#\n",linecount++,lines]];
}
NSLog(#"resultString:%#",resultString);
this may help..!!
Try making a loop that runs through the array and that adds every line to the UITextView plus #"\n".
So something like...:
NSString *curText = txtView.text;
NSString *lineBreak = #"\n";
txtView.text=[NSString stringWithFormat:#"%# + %#", curText, lineBreak];
Or just replace the dots by #"\n".