[futurebasic] Re: [FB] RE: Priority language

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 2006 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Sun, 20 Aug 2006 11:52:09 +1200
Ken Shmidheiser wrote:
>
> Yoshiyuki asked:
>> How is whether the priority language of OSX is English or Japanese  
>> examined?
>
> there are helper functions that are supposed to work in newer  
> versions of OS X.
> In Mac OS X v10.3:
>    CFLocaleCreateCanonicalLocaleIdentifierFromString


> 'XXXXXXXXXXXXXXXX BEWARE! CODE BROKEN!!!! XXXXXXXXXXXXXXXXX

To use CFLocaleCreateCanonicalLocaleIdentifierFromString you must  
load the function pointer from ApplicationServices.framework.
The modified version of Ken's demo below works on my Mac; it returns  
en_GB.

Robert P.

'---------------------------------------
include "Subs DEF USR CFPrefs.incl"

begin globals
dim as CFBundleRef gAppServicesBundle
dim as pointer gCFLocaleCreateCanonicalLocaleIdentifierFromString
end globals

local fn CFLocaleCreateCanonicalLocaleIdentifierFromString( alloc as  
CFAllocatorRef, localeIdentifier as CFStringRef  )
'~'1
if ( gAppServicesBundle == 0 ) then gAppServicesBundle = fn  
CreateBundleForFramework( "ApplicationServices.framework" )
gCFLocaleCreateCanonicalLocaleIdentifierFromString = fn  
GetMachFunctionFromBundle( gAppServicesBundle,  
"CFLocaleCreateCanonicalLocaleIdentifierFromString" )
beginassem
   lwz    r12,^gCFLocaleCreateCanonicalLocaleIdentifierFromString
   mr     r3,^alloc
   mr     r4,^localeIdentifier
   mtspr  ctr,r12
   mr     r31,r2
   bctrl
   mr     r2,r31
endassem
end fn // returns CFStringRef



local fn DefaultAppleLanguage$
'~'1
dim as CFPropertyListRef	localizationList
dim as CFStringRef		languageName, localeName
dim as Str255			defaultLanguage
dim as Boolean			languageFound

defaultLanguage = "en_US"
localizationList = fn CFPreferencesCopyAppValue( fn CFSTR 
( "AppleLanguages" ), fn CFSTR( "kCFPreferencesCurrentApplication" ) )
long if ( localizationList )
long if ( fn CFArrayGetCount( localizationList ) > 0 )
languageName = fn CFArrayGetValueAtIndex( localizationList, 0 )
long if ( languageName )
localeName = fn CFLocaleCreateCanonicalLocaleIdentifierFromString( 0,  
languageName )
long if ( localeName )
languageFound = fn CFStringGetPascalString( localeName,  
@defaultLanguage, 256, _kCFStringEncodingASCII )
CFRelease( localeName )
long if ( languageFound == 0 )
// Default to en_US if not found
defaultLanguage = "en_US"
end if
end if
end if
end if
CFRelease( localizationList)
end if

end fn = defaultLanguage



window 1
dim as Str255  s
s = fn DefaultAppleLanguage$
print s
do
HandleEvents
until gFBQuit
'---------------------------------------