lang.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php namespace System;
  2. class Lang {
  3. /**
  4. * All of the loaded language files.
  5. *
  6. * @var array
  7. */
  8. private static $loaded = array();
  9. /**
  10. * All of the loaded language lines.
  11. *
  12. * @var array
  13. */
  14. private static $lines = array();
  15. /**
  16. * The key of the line that is being requested.
  17. *
  18. * @var string
  19. */
  20. private $key;
  21. /**
  22. * The place-holder replacements.
  23. *
  24. * @var array
  25. */
  26. private $replacements = array();
  27. /**
  28. * Create a new Lang instance.
  29. *
  30. * @param string $line
  31. * @return void
  32. */
  33. public function __construct($key)
  34. {
  35. $this->key = $key;
  36. }
  37. /**
  38. * Create a Lang instance for a language line.
  39. *
  40. * @param string $key
  41. * @return Lang
  42. */
  43. public static function line($key)
  44. {
  45. return new static($key);
  46. }
  47. /**
  48. * Get the language line for a given language.
  49. *
  50. * @param string $language
  51. * @return string
  52. */
  53. public function get($language = null)
  54. {
  55. if (is_null($language))
  56. {
  57. $language = Config::get('application.language');
  58. }
  59. list($file, $line) = $this->parse($this->key);
  60. $this->load($file, $language);
  61. // --------------------------------------------------------------
  62. // Get the language line.
  63. // --------------------------------------------------------------
  64. if (array_key_exists($line, static::$lines[$language.$file]))
  65. {
  66. $line = static::$lines[$language.$file][$line];
  67. }
  68. else
  69. {
  70. throw new \Exception("Language line [$line] does not exist for language [$language]");
  71. }
  72. // --------------------------------------------------------------
  73. // Make all place-holder replacements.
  74. // --------------------------------------------------------------
  75. foreach ($this->replacements as $key => $value)
  76. {
  77. $line = str_replace(':'.$key, $value, $line);
  78. }
  79. return $line;
  80. }
  81. /**
  82. * Parse a language key.
  83. *
  84. * @param string $key
  85. * @return array
  86. */
  87. private function parse($key)
  88. {
  89. $segments = explode('.', $key);
  90. if (count($segments) < 2)
  91. {
  92. throw new \Exception("Invalid language key [$key].");
  93. }
  94. return array($segments[0], implode('.', array_slice($segments, 1)));
  95. }
  96. /**
  97. * Load a language file.
  98. *
  99. * @param string $file
  100. * @param string $language
  101. * @return void
  102. */
  103. private function load($file, $language)
  104. {
  105. if (in_array($language.$file, static::$loaded))
  106. {
  107. return;
  108. }
  109. // --------------------------------------------------------------
  110. // Load the language file into the array of lines.
  111. // --------------------------------------------------------------
  112. if (file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT))
  113. {
  114. static::$lines[$language.$file] = require $path;
  115. }
  116. else
  117. {
  118. throw new \Exception("Language file [$file] does not exist for language [$language].");
  119. }
  120. static::$loaded[] = $language.$file;
  121. }
  122. /**
  123. * Set the place-holder replacements.
  124. *
  125. * @param array $replacements
  126. * @return Lang
  127. */
  128. public function replace($replacements)
  129. {
  130. $this->replacements = $replacements;
  131. return $this;
  132. }
  133. }