Hello.
The use of localized string, doesn’t work well in Mavericks, not for me anyway, so I have tried to make a general handler for returning the correct localized string.
In order to return the correct localized string, I have to get at the correct Lproj bundle, that contains the localized string for either my language or AppleLocale.
Algorithm:
see if the first of languages is in one of the *big* languages[1], if it is, then return correct bundle name.
Else see if the appleLocale, is among the *very detailed bundles*
check and see if the language, has a related, *normal region specific bundle*
if all else fails, use English.lproj as fall back.
[1] Big Languages:
Dutch.lproj, English.lproj, French.lproj, German.lproj, Italian.lproj, Japanese.lproj, Spanish.lproj
The "big languages " are collected up front.
[2] Very detailed bundles:
pt_PT.lproj, zh_CN.lproj zh_TW.lproj
[3]normal region specific bundle:
ar.lproj, ca.lproj, cs.lproj, da.lproj, el.lproj, fi.lproj, and so on . . .
Documentation:
This handler is for returning a localized string, on some machine from some app, according to the current users language and locale settings.
How to preparate input for this handler:
You have to open the resources folder of the app that you inted to retrieve the localized string from, and other than assure that you have got the correct “key” for your localized string, you have to make a list with any spelled out language names, and a related list with the language codes, this page may help when contstructing the lists: iso-639-2 list.
Here comes the handler with a demonstration: Figure out the local string for screenshots of ScreenCapture app.
set hfsAppName to "Macintosh HD:System:Library:CoreServices:SystemUIServer.app:"
# I have figured out the big languages list, by peeking into the SystemUIServer apps Resources folder . . .
set langList to {"nl", "en", "fr", "de", "it", "jp", "es"}
set lProjList to {"Dutch.lproj", "English.lproj", "French.lproj", "German.lproj", "Italian.lproj", "Japanese.lproj", "Spanish.lproj"}
set rscPath to POSIX path of ((hfsAppName & "Contents:Resources") as alias)
set correctLProj to getTheLProjBundle(rscPath, langList, lProjList)
set correctResource to path to resource "no.lproj" in bundle file hfsAppName
set locString to localized string of "%@ %@ at %@" from table "ScreenCapture" in bundle correctResource
log locString
--> "%@ %@ kl. %@"
on getTheLProjBundle(pxPathToResourcesFolder, langList, lProjList)
# http://macscripter.net/viewtopic.php?pid=179383#p179383
set sysLang to word 1 of (do shell script "defaults read -g AppleLanguages")
# StefanK
if (count langList) ≠(count lProjList) then error "getTheLProjBundle: langList is not as long as lProjList"
set pass to 1
repeat
if pass = 1 then
-- check if the first language is amongst the "big" languages.
if sysLang is in langList then
repeat with i from 1 to (count langList)
if sysLang is item i of langList then return item i of lProjList
end repeat
else
set pass to 2
end if
else if pass = 2 then
-- check if the current AppleLocales are among the very detailed localization bundles.
try
set curLocale to do shell script "/usr/bin/defaults read -g AppleLocale"
on error
set pass to 3
end try
if pass = 2 then -- still
try
tell application id "MACS"
set locBundleName to name of first folder of folder (POSIX file pxPathToResourcesFolder as text) whose name begins with curLocale
end tell
return locBundleName
on error
set pass to 3
end try
end if
else if pass = 3 then
-- check and see if the language, has a related, *normal region specific bundle*
try
set region to (do shell script "/usr/bin/grep \"" & sysLang & "_*\" /usr/share/locale/locale.alias |/usr/bin/head -1 |/usr/bin/sed -En 's/([[:alpha:]]+[[:blank:]]+)([^_]+)_([^.]+)(.*)/\\3/p' |/usr/bin/tr 'A-Z' 'a-z'")
on error
set pass to 4
end try
if pass = 3 then -- still . . .
try
tell application id "MACS"
set locBundleName to name of first folder of folder (POSIX file pxPathToResourcesFolder as text) whose name begins with region
end tell
return locBundleName
on error
set pass to 4
end try
end if
else if pass = 4 then
-- fall back
-- Thanks to Shane Stanley
tell application id "MACS"
set rcsFol to folder (POSIX file pxPathToResourcesFolder as text)
if exists folder "English.lproj" of rcsFol then
return "English.lproj"
else if exists folder "en.lproj" of rcsFol then
return "en.lproj"
else
error "getTheLProjBundle: No english localization bundle in this App!!"
end if
end tell
end if
end repeat
end getTheLProjBundle