How to transliterate Cyrillic alphabet without diacritics using CFStringTransform?

I am trying to use CFStringTransform to transliterate names entered in Russian into secure ASCII for credit card processing. However, when the diacritic is removed, č becomes c, which in fact is not a good transliteration. Is there anyway I can use CFStringTransform without diacritics? In other words, h, should return ch, as is assumed in almost every standard http://en.wikipedia.org/wiki/Romanization_of_Russian

NSMutableString *name = [@"" mutableCopy]; CFMutableStringRef nameRef = (__bridge CFMutableStringRef)name; CFStringTransform(nameRef, NULL, kCFStringTransformToLatin, false); //name is romančuk CFStringTransform(nameRef, NULL, kCFStringTransformStripCombiningMarks, false); //name is now romancuk 
+4
source share
1 answer

Create a category in NSString and add the following methods:

 - (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]]; } 
+6
source

Source: https://habr.com/ru/post/1480705/


All Articles