Converting numbers to strings is no problem, but the string created uses ascii numbers. I am inserting the number in a Japanese sentence. The character itself is correct, say 3, for example, but the width and positioning should be a little different when typing in Japanese. As a result the number looks cramped up against the following character. Is there any clever non-manual way to convert 1 to 1, 2 to 2, 3 to 3, etc.?
views:
62answers:
1
A:
For lack of any better ideas, I wrote up a not-so-elegant category solution that manually converts each character. If anyone has a better solution, please let me know.
#import "NSNumber+LocalizedNumber.h"
@interface NSNumber()
-(NSString*)convertRepresentation:(unichar)character;
@end
@implementation NSNumber (LocalizedNumber)
-(NSString *)localizedNumber {
NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
if([languageCode isEqualToString:@"ja"]) {
NSString *numberString = [self stringValue];
NSUInteger length = [numberString length];
NSMutableString *convertedString = [[NSMutableString alloc] initWithCapacity:length];
for(NSUInteger index = 0; index < length; index++) {
unichar character = [numberString characterAtIndex:index];
[convertedString appendString:[self convertRepresentation:character]];
}
return [convertedString autorelease];
}
return [self stringValue];
}
-(NSString*)convertRepresentation:(unichar)character {
switch (character) {
case '1':
return @"1";
case '2':
return @"2";
case '3':
return @"3";
case '4':
return @"4";
case '5':
return @"5";
case '6':
return @"6";
case '7':
return @"7";
case '8':
return @"8";
case '9':
return @"9";
case '0':
return @"0";
default:
return nil;
}
}
Chase
2010-09-06 06:33:18
りI'm a Japanese, and I **loathe** full width numerals! Please just use the half-width numerals within Japanese sentences. All you need is to pad them with spaces. ここに10個林檎があります looks bad, but ここに 10 個林檎があります looks completely OK! ここに10個林檎があります looks horrible to me.
Yuji
2010-09-06 16:32:12
Thanks for your input. The resulting text was actually just a number with a counter, such as 1個,2個, etc. Since there will be both English and Japanese versions, I could always just put the extra spaces in the Japanese localized strings file. I think deep down I knew that was probably better but I thought it wouldn't hurt to try.
Chase
2010-09-07 01:12:57