translation-class.lisp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;; This software is Copyright (c) Leslie P. Polzer, 2011.
  2. ;; Leslie P. Polzer grants you the rights to distribute
  3. ;; and use this software as governed by the terms
  4. ;; of the Lisp Lesser GNU Public License
  5. ;; (http://opensource.franz.com/preamble.html),
  6. ;; known as the LLGPL
  7. (in-package :cl-i18n)
  8. (alexandria:define-constant +fuzzy-flag+ :fuzzy :test 'eq)
  9. (alexandria:define-constant +untranslated-flag+ :untranslated :test 'eq)
  10. (alexandria:define-constant +translated-flag+ :translated :test 'eq)
  11. (alexandria:define-constant +id+ "id" :test 'string=)
  12. (alexandria:define-constant +translation+ "translation" :test 'string=)
  13. (alexandria:define-constant +plurals-form+ "plurals-form" :test 'string=)
  14. (alexandria:define-constant +status+ "status" :test 'string=)
  15. (alexandria:define-constant +plurals+ "plurals" :test 'string=)
  16. (defclass translation ()
  17. ((translated
  18. :initform ""
  19. :initarg :translated
  20. :accessor translated
  21. :type string
  22. :documentation "The translated string")
  23. (plural-form
  24. :initform ""
  25. :initarg :plural-form
  26. :accessor plural-form
  27. :type string)
  28. (plural-translated
  29. :initform '()
  30. :initarg :plural-translated
  31. :accessor plural-translated
  32. :type list
  33. :documentation "a list of string for each valid plural form")
  34. (flag
  35. :initform +untranslated-flag+
  36. :initarg :flag
  37. :accessor flag
  38. :documentation "The status of the translation, can be one of +fuzzy-flag+ +untranslated-flag+ or +translation+"))
  39. (:documentation "The class that holds a translated string, its plural form and the translation status"))
  40. (defmethod print-object ((object translation) stream)
  41. (format stream "~a ~s~%~a ~s~%~a ~s~%~a ~s~%"
  42. +translation+ (translated object)
  43. +plurals-form+ (plural-form object)
  44. +status+ (flag object)
  45. +plurals+ (plural-translated object)))
  46. (defmethod make-load-form ((object translation) &optional environment)
  47. (make-load-form-saving-slots object
  48. :slot-names '(translated plural-form plural-translated flag)
  49. :environment environment))
  50. (defgeneric copy-translation (object old)
  51. (:documentation "Copy an instance of translation class from old to object"))
  52. (defmethod copy-translation ((object translation) (old translation))
  53. (setf (translated object) (translated old))
  54. (setf (plural-form object) (plural-form old))
  55. (setf (plural-translated object) (copy-list (plural-translated old)))
  56. (setf (flag object) (flag old))
  57. object)
  58. (defun make-translation (translation &optional (flag +untranslated-flag+)
  59. (plural-form "") (plural-translated '()))
  60. "Create an instance of a translation class"
  61. (make-instance 'translation
  62. :translated translation
  63. :flag flag
  64. :plural-form plural-form
  65. :plural-translated plural-translated))
  66. (defun translation-hash-table->list (ht)
  67. "Convert a translation table to a list with the format used to store the table in a file"
  68. (loop for key being the hash-keys of ht
  69. and value being the hash-values of ht
  70. collect (format nil "~a ~s~%~a ~s~%~a ~s~%~a ~s~%~a ~s~%"
  71. +id+ key
  72. +translation+ (translated value)
  73. +plurals-form+ (plural-form value)
  74. +status+ (flag value)
  75. +plurals+ (plural-translated value))))
  76. (defgeneric translation-list->hash-table (source dest))
  77. (defmethod translation-list->hash-table ((list list) (ht hash-table))
  78. "Parse a list into a translation table."
  79. (when (and (> (length list) 0)
  80. (= (mod (length list) 10) 0))
  81. (loop
  82. for str = (nth 1 list)
  83. and translation = (nth 3 list)
  84. and plural-form = (nth 5 list)
  85. and flag = (nth 7 list)
  86. and plurals = (nth 9 list)
  87. do (progn
  88. (setf list (subseq list 10))
  89. (setf (gethash str ht) (make-translation translation flag plural-form plurals)))
  90. until (equal list nil)))
  91. ht)