How to get correctly localised display name for macOS Trash/Bin in Swift?

Viewed 62

I'm trying to get Swift to correctly return the local name for the Trash (eg since macOS 10.14/10.15, the Trash is now called Bin in many countries.)

My attempt simply returns "Trash" despite me being in a "Bin" place :)

let trashUrl = try! FileManager.default.url(for: .trashDirectory, in: .allDomainsMask, appropriateFor: nil, create: false)
let trashName = FileManager.default.displayName(atPath: trashUrl.path)
1 Answers

Your code would work if you had added your local language as one of the localisations of your app. For example, my macOS system language is Japanese, and if I add Japanese to the localisations in project settings:

enter image description here

printing trashName from your code would give "ゴミ箱".

Essentially, displayName(for:) is getting the localised name in your app's current localisation. If your app can't be localised to your system's locale, it will stay in English, right? That's why it will show "Trash", unless you make your app able to be localised to whatever locale you are interested in.

Related