Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly transliterate Cyrillic, without diacritics, using CFStringTransform?

I am trying to use CFStringTransform to transliterate names entered in Russian to a safe ASCII for credit card processing. However, when stripping diacritics č becomes a c, which is really not a good transliteration. Is there anyway I can use CFStringTransform, without diacritics? In other words, ч, should return ch, as it's supposed to 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
like image 751
Ryan Romanchuk Avatar asked Mar 23 '23 23:03

Ryan Romanchuk


1 Answers

Create category to NSString and add these 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]];
} 
like image 72
vahotm Avatar answered Apr 07 '23 01:04

vahotm