1

我想在系统偏好设置-> 语言和文本中获得语言的偏好。我尝试使用“获取系统信息”来获取语言偏好。

set language_prefer to (user locale of (get system info))
if (language_prefer is "zh-Hans") then
   set sharingstr to "共享"
   set startstr to "启动"
else if (language_prefer is "zh-Hant") then
   set sharingstr to "共享"
   set startstr to "啟動"
else
   set sharingstr to "Sharing"
   set startstr to "Start"
end if

但是我发现当我更改语言偏好时,((获取系统信息)的用户区域设置)返回值永远不会改变。然后我发现用户语言环境与偏好语言不同。以简体中文为例,它的用户语言环境是“zh_CN”,但它的语言偏好是“zh-Hans”。有人说用

set lang to do shell script "defaults read NSGlobalDomain AppleLanguages"

这返回

(
"zh-Hans",
en,
"zh-Hant",
de,
ja,
fr,
es,
it,
pt,
"pt-PT",
nl,
sv,
nb,
da,
fi,
ru,
pl,
ko,
ar,
cs,
hu,
tr
)

如何获得语言偏好值,例如“zh-Hans”?

希望有人帮助我,谢谢。

4

2 回答 2

1

查看 Cocoa/Objective C " NSLocale" 文档,我看到实际上有两个用户级语言环境变量。

一个是currentLocale,另一个是autoupdatingCurrentLocale

对您来说不幸的是,Apple 决定通过 AppleScript 导出的 API 似乎是仅在用户登录时更新的 API。我怀疑您的 Applescript 结果只会在每次用户重新启动或重新登录时发生变化。

您将需要提供一个允许您执行“ autoupdatingCurrentLocale”的 Objective C 工具并将其放入 Applescript 变量中。

Now I see you've edited your question, so I'll edit mine as well: It sounds like what you want to do is what has been suggested in the answers of this related question, which I'm pretty certain you've seen while doing your own research. Use that "get_language()" applescript bit to return item 1 of your "r" array.

于 2013-03-30T08:43:05.783 回答
0

There are multiple different locale settings:

  • /usr/libexec/PlistBuddy -c 'Print AppleLanguages:0' ~/Library/Preferences/.GlobalPreferences.plist
    • First language selected in Language & Text > Language
  • defaults read -g AppleLocale
    • Setting selected in Language & Text > Region
    • user locale of (system info)
    • Determines what Terminal and iTerm set the LC_ variables to by default
  • First item in AppleLanguages in /Library/Preferences/.GlobalPreferences.plist
    • Changed by sudo languagesetup and when running Setup Assistant
    • Language used in the login window and for new users and the guest user
  • defaults read /Library/Preferences/.GlobalPreferences.plist AppleLocale
    • Changed when running Setup Assistant
    • Region used for new users and the guest user
于 2013-03-30T09:34:28.330 回答