Phamlp.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /* SVN FILE: $Id: HamlException.php 61 2010-04-16 10:19:59Z chris.l.yates $ */
  3. /**
  4. * Phamlp.
  5. * @author Chris Yates <chris.l.yates@gmail.com>
  6. * @copyright Copyright (c) 2010 PBM Web Development
  7. * @license http://phamlp.googlecode.com/files/license.txt
  8. * @package PHamlP
  9. */
  10. /**
  11. * Phamlp class.
  12. * Static support classes.
  13. * @package PHamlP
  14. */
  15. class Phamlp {
  16. /**
  17. * @var string Language used to translate messages
  18. */
  19. public static $language;
  20. /**
  21. * @var array Messages used for translation
  22. */
  23. public static $messages;
  24. /**
  25. * Translates a message to the specified language.
  26. * @param string message category.
  27. * @param string the original message
  28. * @param array parameters to be applied to the message using <code>strtr</code>.
  29. * @return string the translated message
  30. */
  31. public static function t($category, $message, $params = array()) {
  32. if (!empty(self::$language)) {
  33. $message = self::translate($category, $message);
  34. }
  35. return $params!==array() ? strtr($message,$params) : $message;
  36. }
  37. /**
  38. * Translates a message to the specified language.
  39. * If the language or the message in the specified language is not defined the
  40. * original message is returned.
  41. * @param string message category
  42. * @param string the original message
  43. * @return string the translated message
  44. */
  45. private static function translate($category, $message) {
  46. if (empty(self::$messages[$category])) self::loadMessages($category);
  47. return (empty(self::$messages[$category][$message]) ? $message : self::$messages[$category][$message]);
  48. }
  49. /**
  50. * Loads the specified language message file for translation.
  51. * Message files are PHP files in the "category/messages" directory and named
  52. * "language.php", where category is either haml or sass, and language is the
  53. * specified language.
  54. * The message file returns an array of (source, translation) pairs; for example:
  55. * <pre>
  56. * return array(
  57. * 'original message 1' => 'translated message 1',
  58. * 'original message 2' => 'translated message 2',
  59. * );
  60. * </pre>
  61. * @param string message category
  62. */
  63. private static function loadMessages($category) {
  64. $messageFile = dirname(__FILE__).DIRECTORY_SEPARATOR.$category.DIRECTORY_SEPARATOR.'messages'.DIRECTORY_SEPARATOR.self::$language.'.php';
  65. if (file_exists($messageFile)) {
  66. self::$messages[$category] = require_once($messageFile);
  67. }
  68. }
  69. }