Detecting System Language?

I’d like to build an Applescript program that can detect what the System Language is under MacOS X. As you know, MacOS X can have different language selected under the International Control Panel. The order in which those languages are listed is what is used if the program has the required nib and string files.

Example: If your Mac has the following language list in this order,

French
English
Spanish

and the program you were running had French nibs and string files, it would display stuff in French. If it did not have French nibs and strings it will fall through to the English nibs and strings.

My Applescript code fragment is the following:

on clicked theObject
display dialog “This is in English.”
end clicked

What I want to do is do something like the following:

on clicked theObject
if SystemLanguage is "English
display dialog "This is English
If SystemLanguage is “French”
display dialog “C’est Francias”
end clicked

I’m not sure how to have Applescript detect which language the operating System has as a preference.

You could read the international preferences file…

word -1 of (do shell script "defaults read com.apple.systempreferences AppleIntlCustomFormat | grep -e AppleIntlCustomResID")
--> "8"
word -1 of (do shell script "defaults read com.apple.systempreferences AppleIntlCustomFormat | grep -e AppleIntlCustomLocale")
--> "es_ES"

The “8” is the region code number for Spain and “es_ES” maybe the same stuff…

However, in AppleScript Studio you can “localize” both nibs and strings, and the application will care dinamically of the language, choosing french in a french system and so on…

Thank you! This looks like what I need. However, my international control panel shows, right now is:

US English
Francais

When I ran the script:

word -1 of (do shell script “defaults read com.apple.systempreferences AppleIntlCustomFormat | grep -e AppleIntlCustomLocale”)

as you suggested, I originally got 'en_US" as I expected. I then set the International control panel to:

Francais
US English

logged out and logged back in again. The Finder (and most everything else) was in French, which is what I expected. I re-ran the script and STILL got “en_US.” which is what I DIDN’T expect. :frowning:

Oh! I see… You need the “order”. Sorry, I don’t know where is it stored such info, but maybe in some plist file…

I was just doing this today:

defaults read .GlobalPreferences “AppleLanguages” | sed ‘s/(//’ | sed ‘s/)//’

It changes from the listings on the “International” pane of System Preferences. Sorry about the multiple seds - I just don’t know any better.

True, you found it!

I apologize if this is a little off topic (its more applscript studio), but since there are so few “language translation” subjects… I had a hard time figuring out internationalization. Searching, I didn’t find much Applescript specific, so here goes:

To add internationalization to your program (beyond the user interface - windows, menus, panels - which I already forgot how to do - but is much easier I recall), there are those nasty text strings we have in our programs :

tell main_win
set contents to contents & “Hi. How are you” & return
set contents to contents & “I am looking for " & some_currency & " in cash.”
end tell

So to internationalize, make a new “Localizable.strings” file in your project under “Resources”. So say I have english as my language in my program. Don’t make it global.

Using the above example (you can use whatever name you want instead of INL_Greeting - maybe you want “greeting_” :

“INL_Greeting” = “Hi. How are you”;

“INL_Search” = "I am looking for ";

“INL_Currency” = " in cash."

Create a new language translation. Select the English “Localization.strings” file and Control-click on English and choose “Show Info”. From the pop-up menu on the right called “Localization & Platforms” select the “Add Localized Variant…” option. Give it a name of “Spanish” - or whatever language you choose.

So in the Spanish translation, we would have (with the assistance of google if need be - and change the encoding to UTF-8 or you will loose those accent marks!):

“INL_Greeting” = “Hola. Como estas”;

“INL_Search” = "Estoy buscando a ";

“INL_Currency” = " dinero."

Save the localization.

So go back to the applescript code. Things need to be slightly changed now:

tell main_win
–set contents to contents & “Hi. How are you” & return
set contents to contents & (localized string “INL_Greeting” from table “Localizable”)
–set contents to contents & "I am looking for " & some_currency
set contents to contents & ((localized string “INL_Search” from table “Localizable”) & some_currency & (localized string “INL_Currency” from table “Localizable”)
end tell

You don’t have to read in a single file - it all gets handled by the OS - just with the “localized string” – whatever its called. You can go to the International pane of “System Preferences” and drag Espanol to the top. Start your program and see it Spanish. Pesky text strings stuck in English - Be GONE!

Hi jj,

I’m running OS X 10.3.2 and your code doesn’t work for me.

Error:

The domain/default pair of (com.apple.systempreferences, AppleIntlCustomFormat) does not exist.

Now I’ve changed the language from German to English and looked, whitch preferences file was changed.
It was “com.apple.dock.plist”. When I looked into this file, I saw the entry “loc” with the data “en_US”.

So this script works on my machine:

set theLang to (do shell script "defaults read com.apple.dock loc")

Result: “en_US”

Is there a way to localize your applescript for different languages without using Applescript Studio? In other words, without having to bundle .strings files?

Currently my applescripts have variables

set variable1 to “This is in English”
set variable2 to “This is in English2”
set variable3 to “This is in English3”

What I want is for all of these variables to have the localized equivalent in German or some other language.

So something like.

set variable 1 to (my get_target_languange_equivalent(“This is in English”))
set variable 2 to (my get_target_languange_equivalent(“This is in English2”))
set variable 3 to (my get_target_languange_equivalent(“This is in English3”))

then the subroutine would be. (I need help with this)

on get_target_languange_equivalent(text_string)

– go access this .strings file located here and give me the equivalent target language phrase
return the target_string

end get_target_languange_equivalent

The .strings file has the typical format:

“This is in English” = “Dieses ist auf englisch” ;

such that when the subhandler is run with the text_string “This is in English”, it will find that phrase and give the phrase on the other side of the = sign: “Dieses ist auf englisch”

So how do I do this? I have a lot of variables so I don’t want it to take a long time to do this for each one. I thought about using a Unix command with grep on a text file, but wouldn’t that take too long? Any advice would be much appreciated.