Locale command output differs in terminal and script

Enter the command “locale” in a terminal, and you’ll get output that includes strings like LANG="en_US.UTF-8". In a script, if you use do shell script "locale" the output includes LANG="" with no content between the quotation marks. Is there a way to get the same output from a script as from a terminal?

use framework "Foundation"

set theLocales to (current application's NSLocale's availableLocaleIdentifiers()) as list
1 Like

Thank you, Jonas. That command lists the available locales, not the locale of the current system. The output of the locale command in the terminal tells you what the local of the current system is.

I see that I can get the system locale with:

set localeString to user locale of (get system info)

which is probably all I need, but it would be nice to know how to get the same output that locale gives in the terminal

Sorry that it’s not an answer (I just woke up), but remember that the shell launched by “do shell script” does not have any connection to your Terminal environment.

Technical Note TN2065: do shell script in AppleScriptindex.html#//apple_ref/doc/uid/DTS10003093-CH1-TNTAG6-WHERE_DOES_THE_SHELL_ENVIRONMENT_COME_FROM_____ENVIRONMENT_VARIABLES__WORKING_DIRECTORY__AND_SO_ON

So I guess that you’d need to have your do shell script do also things like source your environment and then run locale, but that’s a wild guess.

In AppleScript, you can use:

set user_locale to user locale of (system info) & ".UTF-8"

When running a simple shell command, you can prefix the command with a variable assignment to LANG:

do shell script "LANG=" & user_locale & " command"
1 Like

@tree_frog, this is extremely helpful! Thank you!

Sorry for my misunderstanding…
Here is some AppleScriptObjC code for those who might need it:

use framework "Foundation"

set currentLocale to (current application's NSLocale's autoupdatingCurrentLocale())
set localeID to (currentLocale's valueForKey:"localeIdentifier") as text
1 Like

If you have bash_profile in you home directory you could use.

do shell script "source ~/.bash_profile; locale | grep LANG"
1 Like