DateFormatter localized abbreviated months

Using Swift's DateFormatter class, how does it convert abbreviated date format to languages that don't support abbreviated month names e.g. Urdu

For Example, the following code doesn't return the correct Urdu translation for abbreviated Month format.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
dateFormatter.locale = Locale(identifier: "ur-IN")

let date = Date()
let formattedDate = dateFormatter.string(from: date)

print(formattedDate) 

// Expected: "جولائی ۱۲, ۲۰۲۳" for Jul 12, 2023
// Actual: "جول ۱۲, ۲۰۲۳" 



Replies

Try using dateFormatter.setLocalizedDateFormatFromTemplate("yMMMd") to get the best format for the locale rather than an explicit one. As for the abbreviated form, those come from sources like https://github.com/unicode-org/icu/blob/70d308731ae01a4187fe49f7ae1c8e1ad1570be6/icu4c/source/data/locales/ur.txt#L1073C1-L1074C1

Thanks for the suggestion... I realized that I should have use setLocalizedDateFormatFromTemplate() in the original post for reproducing the issue.

I think its just not picking from the abbreviated section that you provided as even the simplest format like below results in a trimmed word (not the complete month name as in the monthNames -> format -> abbreviated section)

let dateFormatter = DateFormatter()

dateFormatter.locale = Locale(identifier: "ur")
dateFormatter.setLocalizedDateFormatFromTemplate("MMM")
print(dateFormatter.string(from: Date()))

// Expected: "جولائی"
// Actual: "جول" 

I tried with the other variant of Locale identifier such as ur_IN and ur_PK as well

  • Please submit a feedback request with sample code and reference the number here.

Add a Comment