test.lisp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;; Cl-i18n example
  2. ;; Copyright (C) 2012 cage
  3. ;; This program is free software: you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. (require 'cl-i18n)
  14. (in-package :cl-i18n)
  15. ;;;; Native file format example ;;;;
  16. ;; change accordingly to the actual location of the directory cointaining
  17. ;; the translation files
  18. (let ((*translation-file-root* "cl-i18n/examples/locale/"))
  19. (handler-bind ((i18n-conditions:no-translation-table-error ;; Or just (load-language "italian.lisp")
  20. #'(lambda(e)
  21. (declare (ignore e))
  22. (invoke-restart 'load-language "italian.lisp" nil))))
  23. (format t "~a ~a~%" 1 #!"apple")
  24. (format t "~a ~a~%" 1 #!"pie")
  25. (format t "~a ~a~%~%" 4 (cl-i18n:ntranslate "apple" "apples" 4))))
  26. ;;; PO file loading example
  27. (let ((*translation-file-root* "cl-i18n/examples/locale/"))
  28. (cl-i18n:with-translation ((cl-i18n:init-translation-table ;; or pass
  29. ;; a
  30. ;; previously
  31. ;; loaded
  32. ;; hashtable
  33. ;; of
  34. ;; course
  35. "it.po"
  36. :store-hashtable nil
  37. :store-plural-function t
  38. :update-translation-table nil)
  39. cl-i18n:*plural-form-function*)
  40. (format t "~a ~a~%" 1 #!"apple")
  41. (format t "~a ~a~%" 1 #!"pie")
  42. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "pie" "pies" 4))
  43. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "apple" "apples" 4))
  44. (format t "~a~%~%" #!"Invalid argument")))
  45. ;; a way similar to GNU gettext style API; please note we are assuming
  46. ;; "/usr/share/locale/" as the path where the catalog can be found and
  47. ;; also foo.mo is there...
  48. ;; We also let the library guess the right locale with find-locale
  49. (let ((*translation-file-root* "/usr/share/locale/"))
  50. (load-language "foo" :locale (find-locale))
  51. (format t "~a~%" #!"Browse")
  52. (format t "~a~%" #!"Save as..."))
  53. (setf *translation-file-root* "cl-i18n/examples/locale/")
  54. (defparameter *italian-from-lisp* (multiple-value-list (load-language "italian.lisp"
  55. :locale nil
  56. :store-hashtable nil
  57. :store-plural-function nil
  58. :update-translation-table nil)))
  59. (defparameter *italian-from-po* (multiple-value-list (load-language "it.po"
  60. :locale nil
  61. :store-hashtable nil
  62. :store-plural-function nil
  63. :update-translation-table nil)))
  64. (defparameter *it-from-utx* (multiple-value-list (load-language "it.utx"
  65. :locale nil
  66. :store-hashtable nil
  67. :store-plural-function nil
  68. :update-translation-table nil)))
  69. ;; runtime dictionary switching
  70. (cl-i18n:with-translation ((first *italian-from-lisp*)
  71. (second *italian-from-lisp*))
  72. (format t "translation from lisp~%~a ~a~%" 1 #!"apple")
  73. (format t "~a ~a~%" 1 #!"pie")
  74. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "pie" "pies" 4))
  75. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "apple" "apples" 4))
  76. (format t "~a~%~%" #!"Invalid argument")
  77. (cl-i18n:with-translation ((first *italian-from-po*)
  78. (second *italian-from-po*))
  79. (format t "switching to po file ~%~a ~a~%" 1 #!"apple")
  80. (format t "~a ~a~%" 1 #!"pie")
  81. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "pie" "pies" 4))
  82. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "apple" "apples" 4))
  83. (format t "~a~%~%" #!"Invalid argument")))
  84. (format t "UTX example~%")
  85. (cl-i18n:with-translation ((first *it-from-utx*)
  86. (second *it-from-utx*))
  87. (format t "~a ~a~%" 4 (cl-i18n:ntranslate "apple" "apples" 4)))