简体   繁体   中英

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

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)

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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM