I want to transliterate a cyrillic string to it's closest latin equivalent. E.g. "матрешка" => "matreshka", "водка" => "vodka". So ideally I want some ready to use method on the NSString or somewhere else that already knows everything about the alphabets and can do the conversation.
But if such functionality doesn't exist in the iOS APIs then I will be totally happy with something like ruby's tr method that just replaces the characters in a string using a simple mapping specified as a parameter.
"баба".tr('абвгд', 'abvgd')
Either try
CFStringTransform
function of CFMutableString withkCFStringTransformToLatin
transform identifier or make a NSString category with simple mapping.Edited by the question poster: more specifically it should be:
If you don't need diacritics or accents you can call
CFStringTransform(bufferRef, NULL, kCFStringTransformStripCombiningMarks, false);
Additional article: http://nshipster.com/cfstringtransform/
With Swift 4.2 and iOS 12,
String
has a method calledapplyingTransform(_:reverse:)
.applyingTransform(_:reverse:)
has the following declaration:The following Playground code shows how to use
applyingTransform(_:reverse:)
in order to transliterate from cyrillic to latin characters:As an alternative, if needed, you can use Core Foundation
CFStringTransform(_:_:_:_:)
function: