0

How to use String(format:) in Arabic string in Swift?

let seconds = 458569

my code is:

let arabicStr = "لديك ٪d ثانية متبقية للاتصال"

let Str = String(format: arabicStr,seconds)

and output:

لديك d٪ ثانية متبقية للاتصال

2 Answers2

3

Try this code.

let seconds = 458569
let Str = "لديك \(seconds) ثانية متبقية للاتصال"
print(Str);

OR

let Str2 = String(format: "لديك %d ثانية متبقية للاتصال",seconds)
print(Str2);

enter image description here

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
2

The has nothing to do with used -Arabic- language. When using init(format:arguments:), the format should be a valid string format specifier:

let seconds = 458569
let Str = String(format: "لديك %d ثانية متبقية للاتصال",seconds)

Note that it should be %d instead of d%, which means that this specifier describes:

Signed 32-bit integer (int).


On the other hand:

Since you are working with Swift strings, you could also achieve it by implementing a string interpolation as mentioned in AtulParmar`s answer.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • 1
    This was very helpful. I also found that when you use certain translation software to do the translation, then import the localization into Xcode, the embedded %d appears to be in an Arabic font. If you look closely, the percent sign will look different. I fixed the issues I had by copying and pasting the correct '%d' or other specifier with the correct font. – FontFamily Dec 15 '20 at 22:35