0

i am trying to concat arabic text with english text for example :

let pdfName = "\(name)_copy_1234.pdf"

name is like : وصغار

1234 is my random number

But in document folder it is saving like this : copy_35679_وصغا.pdf

Mahesh Shahane
  • 489
  • 5
  • 16
  • https://stackoverflow.com/questions/38848130/combine-two-language-text-rtl-ltr – Prashant Tukadiya Mar 11 '19 at 10:28
  • I Already used "\u{200E}" but every time not sure the name is Arabic it will be English or any language depends on user. – Mahesh Shahane Mar 11 '19 at 10:32
  • https://stackoverflow.com/questions/6325073/detect-language-of-nsstring – Prashant Tukadiya Mar 11 '19 at 10:33
  • You have to ask yourself what it is that you ***actually*** want to achieve. In what way is what is currently being displayed not correct? Is it not correct for *you*, or not correct for the arabic user who input the data? – luk2302 Mar 11 '19 at 10:35
  • I'm not able to reproduce this. How are you creating the file, and how are you determining what the name of the file is on disk? – Rob Napier Mar 11 '19 at 18:28
  • Regarding \u200e, that's safe to use in all cases, including LTR languages, to achieve correct layout. It is good practice to include that in localizations, for example, any time the string starts with unknown content. However, this is a filename, and should likely not include extra control characters, but it also should not need them, particularly if you're using URL properly. – Rob Napier Mar 11 '19 at 18:32

2 Answers2

2

You need to properly escape the beginning and the end of your pdfName string, as seen here:

let name = "وصغار"
let pdfName = "\u{202A}\(name)_copy_1234.pdf\u{202C}"
print(pdfName)

// English ‪وصغار_copy_1234.pdf‬
// Arabic ‪وصغار_copy_1234.pdf‬

Apple discusses it here, but the code is for Objective-C, so it does not show how to properly enclose it for Swift with { }.

CodeBender
  • 35,668
  • 12
  • 125
  • 132
1

This is not a Swift-specific issue, as it is baked into the Unicode standard and is related to the way that Unicode supports bi-directional scripts (e.g. Latin and right-to-left scripts such as Arabic). Consider reviewing a detailed explanation here Unicode Controls for Bidirectional Text to better understand how the change of direction override characters work.

CodeBlender's answer points you to an example that works fine – however, be careful when saving file names that contain extended charsets, as you may encounter issues when saving to legacy operating systems, or when transferring between computers, for example.

Alexandre Cassagne
  • 2,384
  • 23
  • 40