简体   繁体   English

如何在 Swift 中为 macOS Trash/Bin 正确本地化显示名称?

[英]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.)我正在尝试让 Swift 正确返回垃圾箱的本地名称(例如,从 macOS 10.14/10.15 开始,垃圾箱现在在许多国家都称为 Bin。)

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:例如,我的 macOS 系统语言是日语,如果我在项目设置中将日语添加到本地化中:

在此处输入图像描述

printing trashName from your code would give "ゴミ箱".从您的代码中打印trashName将给出“ゴミ箱”。

Essentially, displayName(for:) is getting the localised name in your app's current localisation.本质上, displayName(for:)是在您应用的当前本地化中获取本地化名称。 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.这就是为什么它会显示“垃圾箱”,除非您使您的应用程序能够本地化到您感兴趣的任何语言环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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