Skip to content

Instantly share code, notes, and snippets.

@lispstudent
Forked from svetlyak40wt/lw-current-lang.lisp
Created January 18, 2021 16:17
Show Gist options
  • Save lispstudent/7bac55aa2bd41a7128f1c63229db78c6 to your computer and use it in GitHub Desktop.
Save lispstudent/7bac55aa2bd41a7128f1c63229db78c6 to your computer and use it in GitHub Desktop.
An example how to get user's preferred language on OSX and Windows using LispWorks.
#+cocoa
(defun current-language ()
(let ((lang (objc:with-autorelease-pool ()
(objc:invoke-into
'string
(objc:invoke
(objc:invoke "NSUserDefaults" "standardUserDefaults")
"objectForKey:" "AppleLanguages")
"objectAtIndex:" 0))))
;; 2019-04-29
;; lang can be either on the short form "sv" or the long form "sv-SE".
;; It may have changed in later versions of macOS.
;; Anyway, make sure that this function returns on the short form "sv".
(if (and (= (length lang) 5)
(eq (char lang 2) #\-))
(subseq lang 0 2)
lang)
)
)
#+win32
(fli:define-foreign-function (get-system-default-language-id "GetSystemDefaultLangID")
() :result-type :int)
#+win32
(defun current-language ()
;; Map language id to code - a complete list is available in the Windows documentation of GetSystemDefaultLangID
(case (mod (get-system-default-language-id) 1024) ; we are only interested in the primary language, which resides in the last 10 bits
(#x16 "pt")
(#x1D "sv")
(t "default")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment