locale.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * HTML2PDF Librairy - HTML2PDF Locale
  4. *
  5. * HTML => PDF convertor
  6. * distributed under the LGPL License
  7. *
  8. * @author Laurent MINGUET <webmaster@html2pdf.fr>
  9. * @version 4.03
  10. */
  11. class HTML2PDF_locale
  12. {
  13. /**
  14. * code of the current used locale
  15. * @var string
  16. */
  17. static protected $_code = null;
  18. /**
  19. * texts of the current used locale
  20. * @var array
  21. */
  22. static protected $_list = array();
  23. /**
  24. * directory where locale files are
  25. * @var string
  26. */
  27. static protected $_directory = null;
  28. /**
  29. * load the locale
  30. *
  31. * @access public
  32. * @param string $code
  33. */
  34. static public function load($code)
  35. {
  36. if (self::$_directory===null) {
  37. self::$_directory = dirname(dirname(__FILE__)).'/locale/';
  38. }
  39. // must be in lower case
  40. $code = strtolower($code);
  41. // must be [a-z-0-9]
  42. if (!preg_match('/^([a-z0-9]+)$/isU', $code)) {
  43. throw new HTML2PDF_exception(0, 'invalid language code ['.self::$_code.']');
  44. }
  45. // save the code
  46. self::$_code = $code;
  47. // get the name of the locale file
  48. $file = self::$_directory.self::$_code.'.csv';
  49. // the file must exist
  50. if (!is_file($file)) {
  51. throw new HTML2PDF_exception(0, 'language code ['.self::$_code.'] unknown. You can create the translation file ['.$file.'] and send it to the webmaster of html2pdf in order to integrate it into a future release');
  52. }
  53. // load the file
  54. self::$_list = array();
  55. $handle = fopen($file, 'r');
  56. while (!feof($handle)) {
  57. $line = fgetcsv($handle);
  58. if (count($line)!=2) continue;
  59. self::$_list[trim($line[0])] = trim($line[1]);
  60. }
  61. fclose($handle);
  62. }
  63. /**
  64. * clean the locale
  65. *
  66. * @access public static
  67. */
  68. static public function clean()
  69. {
  70. self::$_code = null;
  71. self::$_list = array();
  72. }
  73. /**
  74. * get a text
  75. *
  76. * @access public static
  77. * @param string $key
  78. * @return string
  79. */
  80. static public function get($key, $default='######')
  81. {
  82. return (isset(self::$_list[$key]) ? self::$_list[$key] : $default);
  83. }
  84. }