How to make a list of available locales?

foundation
asobjc

(Jonas Whale) #1

Can’t figure out how to get the localized description of a locale.
I tried with localizedStringForLocaleIdentifier: but I’m missing something, obviously…

 use framework "Foundation"

set theLocales to current application's NSLocale's availableLocaleIdentifiers()
set aMutableArray to current application's NSMutableArray's array()
repeat with aLocale in theLocales
	set aLocaleDesc to (current application's NSLocale's localizedStringForLocaleIdentifier:aLocale)
	set aDict to (current application's NSDictionary's dictionaryWithDictionary:{desc:aLocaleDesc, code:aLocale})
	(aMutableArray's addObject:aDict)
end repeat
aMutableArray as list

(Nigel Garvey) #2

Hi.

localizedStringForLocaleIdentifier: is an instance method which returns the name of the parameter locale in the language of the locale on which it’s called. So if you want the locale names in their own local languages, it’s:

use framework "Foundation"

set theLocaleIDs to current application's NSLocale's availableLocaleIdentifiers()
set aMutableArray to current application's NSMutableArray's array()
repeat with aLocaleID in theLocaleIDs
	set aLocale to (current application's NSLocale's localeWithLocaleIdentifier:aLocaleID)
	set aLocaleDesc to (aLocale's localizedStringForLocaleIdentifier:aLocaleID)
	set aDict to (current application's NSDictionary's dictionaryWithDictionary:{desc:aLocaleDesc, code:aLocaleID})
	(aMutableArray's addObject:aDict)
end repeat
aMutableArray as list

If you want them in your locale’s local language, it’s:

use framework "Foundation"

set theLocaleIDs to current application's NSLocale's availableLocaleIdentifiers()
set aMutableArray to current application's NSMutableArray's array()
set aLocale to (current application's NSLocale's currentLocale())
repeat with aLocaleID in theLocaleIDs
	set aLocaleDesc to (aLocale's localizedStringForLocaleIdentifier:aLocaleID)
	set aDict to (current application's NSDictionary's dictionaryWithDictionary:{desc:aLocaleDesc, code:aLocaleID})
	(aMutableArray's addObject:aDict)
end repeat
aMutableArray as list

(Jonas Whale) #3

Thanks Nigel

I understand now why all my tests was unsuccessful: LocalizedStringForLocaleIdentifier: is not available in 10.11

If I’m right, I have to use displayNameForKey:value:
Could you help me with this?


(Jonas Whale) #4

Found it!

 use framework "Foundation"

set theLocaleIDs to current application's NSLocale's availableLocaleIdentifiers()
set aMutableArray to current application's NSMutableArray's array()
set aLocale to (current application's NSLocale's currentLocale())
repeat with aLocaleID in theLocaleIDs
	set aLocaleDesc to (aLocale's displayNameForKey:(current application's NSLocaleIdentifier) value:aLocaleID)
	set aDict to (current application's NSDictionary's dictionaryWithDictionary:{desc:aLocaleDesc, code:aLocaleID})
	(aMutableArray's addObject:aDict)
end repeat
aMutableArray as list

(Jim Underwood) #5

Thanks for sharing.
Confirmed: It works for me running Script Debugger 6.0.5 (6A205) on macOS 10.11.6.

I was amazed: It returned 713 locales!


(Nigel Garvey) #6

Aha. That would explain part of the problem. :wink: Mind you, the Xcode 8.2.1 documentation says the same thing about availableLocaleIdentifiers and current locale, although in their cases it’s not true.

Yes. That produces exactly the same result and works in 10.11. Well done! It must have taken some finding!


(Jonas Whale) #7

@NigelGarvey
Do you mean it’s not safe to use currentLocale on 10.11?
On Apple’s documentation site currentLocale is compatible with MacOs 10.4+

@JMichaelTX
731!
:wink:


(Nigel Garvey) #8

No. It’s fine in 10.11. It’s the Xcode 8.2.1 documentation that’s wrong. Sorry for any confusion.


(Shane Stanley) #9

FWIW, when there’s any doubt it’s a good idea to double-check the header (.h) files. And it’s pretty easy to do in Xcode. Just hit command-shift-O (that’s the letter O, not the digit 0) and type into the Open Quickly panel. Most times it will find the method, and sometimes you need to enter just the class name.

Then you just look in the document for the entry, and whether it says something like API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)). If there’s no such annotation, you can be pretty confident.


(Shane Stanley) #10

Let me also throw in a reminder about using Script Debugger’s code completion, because it can also help you resolve issues of version availability.

Consider this code:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

current application's NSLocale's currentLocale()'s localized

If you hit F5, the completion list won’t show localizedStringForLocaleIdentifier: because it wasn’t available in 10.10 (AS 2.4).

If you change the AS version to 2.5 and recompile, it will appear in the list if you are running 10.12, otherwise it won’t.

So the absence or not in a completion list can be a good clue.

(Unfortunately having version 2.5 for both 10.11 and 10.12 complicates things, but for apps you can also set the minimum version in the bundle’s Info.plist file, and SD will respect that.)

In fact, I often use completion after the fact. If a bit of code isn’t working, just clicking at the end of a term and hitting F5 to see if anything appears can be a good initial sanity check.


(Jonas Whale) #11

I’m always doing that but didn’t notice how much the AS version declaration changes things.

Now I will be more alert.
:wink:


(Jonas Whale) #12

No problem. You put me on the track…