Language Switcher: how are they doing this?

I use language switcher to launch my osx apps in different localized versions, for testing.

Now I wonder what language switcher exactly is doing, in order to launch an app in a language version different from the os?

I was thinking of adding the same hack directly to my own apps.

Idea, anyone?

Probably renames the included language files inside the app bundle. Easy enough to check.

( Arghh … this forum is sooo unusable, with an iPad. It hides all those icons and the I hit, again, on the ‘answered’ icon! How to make it un-answered?! )

@ Markus: renaming language files would not work with a sandboxed app, right?

Maybe there is a plist somewhere, which can be modified?

I found this piece of information on the web:

[quote]You can change the language inside the preferences file of the application :

defaults write AppleLanguages ‘(“en-US”)’

Or just run once one application with another language :

/Applications/ -AppleLanguages ‘(de)’[/quote]

One could call these from one’s app, but then again I guess that this is not an option for a sandboxed app (? - Instantiating a shell object and then call defaults … ).
Maybe there are some Cocoa functions for this?

Cocoa picks the “best match for language” based on the combination of the users preferred language ordering & whats available as supported localizations

However you can take manual control of every label etc and use a specific language if you want

And once an app is running on OS X there really isn’t a way to say “use this localization instead” - really.

I found an AppleScript which lists the languages available in an app and then relaunches it with the user’s selection.

I added the script to my app and call it from the action event of a pushbutton:

The script:

[code]–Autor: Kudusch
–Licence: CC BY-NC

–set the Application you want to work with
set vInput to “Application Name”

–Path and BundleID of application are safed in variables
set vPath to (path to application vInput) as string
set vBundle to bundle identifier of (info for (path to application vInput))
on error
error “You must choose an application!”
end try
set vPath to (path to application vInput) as string
set vBundle to bundle identifier of (info for (path to application vInput))

–check available localizations
tell application “Finder”
set vOut to {}
set vFolder to vPath & “Contents:Resources:” as string as alias
set vList to name of every folder in vFolder
on error
error “There are no localizations available!”
end try
set vList to name of every folder in vFolder
set n to 1
repeat while n <= (count items in vList)
set vTr to item n of vList
set AppleScript’s text item delimiters to {"."}
set vTr to every text item in vTr as list
if (count every item of vTr) > 1 and item 2 of vTr = “lproj” then
set AppleScript’s text item delimiters to {""}
set vOut to ((item 1 of vTr as list) & vOut)
end if
set n to n + 1
end repeat
end tell

–user chooses language
set dialog2 to choose from list vOut with prompt “Choose language:”
set vLang to dialog2
if vLang = false then
error number -128
end if
set vLang to item 1 of vLang as text
–language is changed
set vScript to “defaults write " & vBundle & " AppleLanguages '(” & vLang & “)’”
do shell script vScript
tell application vInput to quit
delay 2
tell application vInput to activate

In terminal or from a shell…

/Applications/ -AppleLanguages '(fr)'

You could write a helper app to relaunch the app in a new language pretty easily.

You could also run a shell from your app that detaches, waits and runs your app with the specified language. Then quit the current version.

sh ` sleep 3 ; /Applications/ -AppleLanguages '(fr)' ;exit;` &

To get the list of Languages the user has selected in their system preferences do

defaults read -g AppleLanguages