NSString にカテゴリを作成し、次のメソッドを追加します。
- (NSString *)toLatinWithDictionary
{
NSMutableString *newString = [NSMutableString string];
NSRange range;
NSString *symbol;
NSString *newSymbol;
for (NSUInteger i = 0; i < [self length]; i ++)
{
// Take regular symbol
range = NSMakeRange(i, 1);
symbol = [self substringWithRange:range];
newSymbol = [self transliterateChar:symbol];
if (newSymbol != nil)
{
[newString appendString:newSymbol];
}
else
{
[newString appendString:symbol];
}
}
return [NSString stringWithString:newString];
}
- (NSString *)transliterateChar:(NSString *)symbol
{
// For simlicity there is only
NSArray *cyrillicChars = @[@"а", @"б", @"в", @"г", @"д", @"е", @"ё", @"ж", @"з", @"и", @"й", @"к", @"л", @"м", @"н", @"о", @"п", @"р", @"с", @"т", @"у", @"ф", @"х", @"ц", @"ч", @"ш", @"щ", @"ъ", @"ы", @"ь", @"э", @"ю", @"я"];
NSArray *latinChars = @[@"a", @"b", @"v", @"g", @"d", @"e", @"yo", @"zh", @"z", @"i", @"y", @"k", @"l", @"m", @"n", @"o", @"p", @"r", @"s", @"t", @"u", @"f", @"h", @"ts", @"ch", @"sh", @"shch", @"'", @"y", @"'", @"e", @"yu", @"ya"];
NSDictionary *convertDict = [NSDictionary dictionaryWithObjects:latinChars
forKeys:cyrillicChars];
return [convertDict valueForKey:[symbol lowercaseString]];
}