Domanda

I'd like to get the preference of language in System Preferences-> language & text. I try to use "get system info" to get the language preference.

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

But i found that the (user locale of (get system info)) return value never changes when i change preference of language. Then i found that the user locale is different to preference language. Take Simplified Chinese as example, it's user locale is "zh_CN", but it's language preference is "zh-Hans". Someone says that use

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

this returns

(
"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
)

How can i get the language preference value such as "zh-Hans"?

Wish someone help me, thanks.

È stato utile?

Soluzione

Looking at the Cocoa/Objective C "NSLocale" documentation, I see there are actually two user level locale variables.

One is currentLocale and the other is autoupdatingCurrentLocale.

Unfortunately for you, it looks like the API Apple has decided to export via AppleScript is the one that only updates at the user's login time. I suspect your Applescript result will only change each time the user is rebooted or re-logged in.

You will need to come up with an Objective C tool that allows you to do "autoupdatingCurrentLocale" and put that into an Applescript variable.

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.

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top