Skip to content

Instantly share code, notes, and snippets.

@death
Last active September 22, 2022 00:50
Show Gist options
  • Save death/b793fd31c212f9a0c55e573b86884bfc to your computer and use it in GitHub Desktop.
Save death/b793fd31c212f9a0c55e573b86884bfc to your computer and use it in GitHub Desktop.
late hours localization
(defpackage #:localization
(:use #:cl #:constantia))
(in-package #:localization)
(defvar *english-translations*
(make-hash-table))
(setf (gethash 'the-sum-of-x-and-y-is-z *english-translations*)
'("The sum of " x " and " y " is " z))
(defvar *german-translations*
(make-hash-table))
(setf (gethash 'the-sum-of-x-and-y-is-z *german-translations*)
'(z " ist die Summe aus " x " und " y))
(defvar *compiled-translations*
(make-hash-table))
(defvar *translations*)
(defun set-translations (translations)
(setf *translations* translations)
(clrhash *compiled-translations*))
(set-translations *english-translations*)
(defun get-translation-function (identifier arg-names)
(or (gethash identifier *compiled-translations*)
(setf (gethash identifier *compiled-translations*)
(compile nil
`(lambda ,arg-names
(outs ,@(gethash identifier *translations*)))))))
(define-out-op :l (stream identifier &rest args)
(let ((arg-names
(loop for (name value-form) on args by #'cddr
collect name))
(arg-values
(loop for (name value-form) on args by #'cddr
collect value-form)))
`(:forms
(out (:to ,stream)
(funcall (get-translation-function ',identifier ',arg-names)
,@arg-values)))))
(defun output-sum (x y)
(out (:l the-sum-of-x-and-y-is-z x x y y z (+ x y))))
;; LOCALIZATION> (set-translations *english-translations*)
;; #<HASH-TABLE :TEST EQL :COUNT 0 {101D478283}>
;; LOCALIZATION> (output-sum 123 321)
;; The sum of 123 and 321 is 444
;; ; No value
;; LOCALIZATION> (set-translations *german-translations*)
;; #<HASH-TABLE :TEST EQL :COUNT 0 {101D478283}>
;; LOCALIZATION> (output-sum 123 321)
;; 444 ist die Summe aus 123 und 321
;; ; No value
;; LOCALIZATION>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment