lang.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php namespace Laravel;
  2. class Lang_Facade extends Facade {
  3. public static $resolve = 'lang';
  4. }
  5. class Lang_Factory {
  6. /**
  7. * The configuration manager instance.
  8. *
  9. * @var Config
  10. */
  11. protected $config;
  12. /**
  13. * The paths containing the language files.
  14. *
  15. * @var array
  16. */
  17. protected $paths;
  18. /**
  19. * Create a new language factory instance.
  20. *
  21. * @param Config $config
  22. * @param array $paths
  23. * @return void
  24. */
  25. public function __construct(Config $config, $paths)
  26. {
  27. $this->paths = $paths;
  28. $this->config = $config;
  29. }
  30. /**
  31. * Begin retrieving a language line.
  32. *
  33. * @param string $key
  34. * @param array $replacements
  35. * @return Lang
  36. */
  37. public function line($key, $replacements = array())
  38. {
  39. return new Lang($key, $replacements, $this->config->get('application.language'), $this->paths);
  40. }
  41. }
  42. class Lang {
  43. /**
  44. * All of the loaded language lines.
  45. *
  46. * The array is keyed by [$language.$file].
  47. *
  48. * @var array
  49. */
  50. private static $lines = array();
  51. /**
  52. * The key of the language line being retrieved.
  53. *
  54. * @var string
  55. */
  56. private $key;
  57. /**
  58. * The replacements that should be made on the language line.
  59. *
  60. * @var array
  61. */
  62. private $replacements;
  63. /**
  64. * The default language being used by the application.
  65. *
  66. * @var string
  67. */
  68. private $language;
  69. /**
  70. * The paths containing the language files.
  71. *
  72. * @var array
  73. */
  74. private $paths;
  75. /**
  76. * Create a new Lang instance.
  77. *
  78. * @param string $key
  79. * @param array $replacements
  80. * @param string $language
  81. * @param array $paths
  82. * @return void
  83. */
  84. public function __construct($key, $replacements, $language, $paths)
  85. {
  86. $this->key = $key;
  87. $this->paths = $paths;
  88. $this->language = $language;
  89. $this->replacements = $replacements;
  90. }
  91. /**
  92. * Get the language line.
  93. *
  94. * A default value may also be specified, which will be returned in the language line doesn't exist.
  95. *
  96. * @param string $language
  97. * @return string
  98. */
  99. public function get($default = null)
  100. {
  101. list($file, $line) = $this->parse($this->key);
  102. if ( ! $this->load($file))
  103. {
  104. return ($default instanceof \Closure) ? call_user_func($default) : $default;
  105. }
  106. $line = Arr::get(static::$lines[$this->language.$file], $line, $default);
  107. foreach ($this->replacements as $key => $value)
  108. {
  109. $line = str_replace(':'.$key, $value, $line);
  110. }
  111. return $line;
  112. }
  113. /**
  114. * Parse a language key.
  115. *
  116. * Language keys follow a {file}.{key} convention. If a specific language key is not
  117. * specified, an exception will be thrown. Setting entire language files at run-time
  118. * is not currently supported.
  119. *
  120. * @param string $key
  121. * @return array
  122. */
  123. private function parse($key)
  124. {
  125. if (count($segments = explode('.', $key)) > 1)
  126. {
  127. return array($segments[0], implode('.', array_slice($segments, 1)));
  128. }
  129. throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
  130. }
  131. /**
  132. * Load a language file.
  133. *
  134. * If the language file has already been loaded, it will not be loaded again.
  135. *
  136. * @param string $file
  137. * @return bool
  138. */
  139. private function load($file)
  140. {
  141. if (isset(static::$lines[$this->language.$file])) return;
  142. $language = array();
  143. foreach ($this->paths as $directory)
  144. {
  145. if (file_exists($path = $directory.$this->language.'/'.$file.EXT))
  146. {
  147. $language = array_merge($language, require $path);
  148. }
  149. }
  150. if (count($language) > 0)
  151. {
  152. static::$lines[$this->language.$file] = $language;
  153. }
  154. return isset(static::$lines[$this->language.$file]);
  155. }
  156. /**
  157. * Set the language the line should be returned in.
  158. *
  159. * The language specified in this method should correspond to a language directory in your application.
  160. *
  161. * @param string $language
  162. * @return Lang
  163. */
  164. public function in($language)
  165. {
  166. $this->language = $language;
  167. return $this;
  168. }
  169. /**
  170. * Get the string content of the language line.
  171. */
  172. public function __toString()
  173. {
  174. return $this->get();
  175. }
  176. }