lang.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php namespace Laravel; use Closure;
  2. class Lang {
  3. /**
  4. * The key of the language line being retrieved.
  5. *
  6. * @var string
  7. */
  8. protected $key;
  9. /**
  10. * The replacements that should be made on the language line.
  11. *
  12. * @var array
  13. */
  14. protected $replacements;
  15. /**
  16. * The language in which the line should be retrieved.
  17. *
  18. * @var string
  19. */
  20. protected $language;
  21. /**
  22. * All of the loaded language lines.
  23. *
  24. * The array is keyed by [$bundle][$language][$file].
  25. *
  26. * @var array
  27. */
  28. protected static $lines = array();
  29. /**
  30. * The language loader event name.
  31. *
  32. * @var string
  33. */
  34. const loader = 'laravel.language.loader';
  35. /**
  36. * Create a new Lang instance.
  37. *
  38. * @param string $key
  39. * @param array $replacements
  40. * @param string $language
  41. * @return void
  42. */
  43. protected function __construct($key, $replacements = array(), $language = null)
  44. {
  45. $this->key = $key;
  46. $this->language = $language;
  47. $this->replacements = $replacements;
  48. }
  49. /**
  50. * Create a new language line instance.
  51. *
  52. * <code>
  53. * // Create a new language line instance for a given line
  54. * $line = Lang::line('validation.required');
  55. *
  56. * // Create a new language line for a line belonging to a bundle
  57. * $line = Lang::line('admin::messages.welcome');
  58. *
  59. * // Specify some replacements for the language line
  60. * $line = Lang::line('validation.required', array('attribute' => 'email'));
  61. * </code>
  62. *
  63. * @param string $key
  64. * @param array $replacements
  65. * @param string $language
  66. * @return Lang
  67. */
  68. public static function line($key, $replacements = array(), $language = null)
  69. {
  70. if (is_null($language)) $language = Config::get('application.language');
  71. return new static($key, $replacements, $language);
  72. }
  73. /**
  74. * Determine if a language line exists.
  75. *
  76. * @param string $key
  77. * @param string $language
  78. * @return bool
  79. */
  80. public static function has($key, $language = null)
  81. {
  82. return ! is_null(static::line($key, array(), $language)->get());
  83. }
  84. /**
  85. * Get the language line as a string.
  86. *
  87. * <code>
  88. * // Get a language line
  89. * $line = Lang::line('validation.required')->get();
  90. *
  91. * // Get a language line in a specified language
  92. * $line = Lang::line('validation.required')->get('sp');
  93. *
  94. * // Return a default value if the line doesn't exist
  95. * $line = Lang::line('validation.required', null, 'Default');
  96. * </code>
  97. *
  98. * @param string $language
  99. * @param string $default
  100. * @return string
  101. */
  102. public function get($language = null, $default = null)
  103. {
  104. if (is_null($language)) $language = $this->language;
  105. list($bundle, $file, $line) = $this->parse($this->key);
  106. // If the file doesn't exist, we'll just return the default value that was
  107. // given to the method. The default value is also returned even when the
  108. // file exists and the file does not actually contain any lines.
  109. if ( ! static::load($bundle, $language, $file))
  110. {
  111. return value($default);
  112. }
  113. $lines = static::$lines[$bundle][$language][$file];
  114. $line = array_get($lines, $line, $default);
  115. // If the line is not a string, it probably means the developer asked for
  116. // the entire langauge file and the value of the requested value will be
  117. // an array containing all of the lines in the file.
  118. if (is_string($line))
  119. {
  120. foreach ($this->replacements as $key => $value)
  121. {
  122. $line = str_replace(':'.$key, $value, $line);
  123. }
  124. }
  125. return $line;
  126. }
  127. /**
  128. * Parse a language key into its bundle, file, and line segments.
  129. *
  130. * Language lines follow a {bundle}::{file}.{line} naming convention.
  131. *
  132. * @param string $key
  133. * @return array
  134. */
  135. protected function parse($key)
  136. {
  137. $bundle = Bundle::name($key);
  138. $segments = explode('.', Bundle::element($key));
  139. // If there are not at least two segments in the array, it means that
  140. // the developer is requesting the entire language line array to be
  141. // returned. If that is the case, we'll make the item "null".
  142. if (count($segments) >= 2)
  143. {
  144. $line = implode('.', array_slice($segments, 1));
  145. return array($bundle, $segments[0], $line);
  146. }
  147. else
  148. {
  149. return array($bundle, $segments[0], null);
  150. }
  151. }
  152. /**
  153. * Load all of the language lines from a language file.
  154. *
  155. * @param string $bundle
  156. * @param string $language
  157. * @param string $file
  158. * @return bool
  159. */
  160. public static function load($bundle, $language, $file)
  161. {
  162. if (isset(static::$lines[$bundle][$language][$file]))
  163. {
  164. return true;
  165. }
  166. // We use a "loader" event to delegate the loading of the language
  167. // array, which allows the develop to organize the language line
  168. // arrays for their application however they wish.
  169. $lines = Event::first(static::loader, func_get_args());
  170. static::$lines[$bundle][$language][$file] = $lines;
  171. return count($lines) > 0;
  172. }
  173. /**
  174. * Load a language array from a language file.
  175. *
  176. * @param string $bundle
  177. * @param string $language
  178. * @param string $file
  179. * @return array
  180. */
  181. public static function file($bundle, $language, $file)
  182. {
  183. $lines = array();
  184. // Language files can belongs to the application or to any bundle
  185. // that is installed for the application. So, we'll need to use
  186. // the bundle's path when looking for the file.
  187. $path = static::path($bundle, $language, $file);
  188. if (file_exists($path))
  189. {
  190. $lines = require $path;
  191. }
  192. return $lines;
  193. }
  194. /**
  195. * Get the path to a bundle's language file.
  196. *
  197. * @param string $bundle
  198. * @param string $language
  199. * @param string $file
  200. * @return string
  201. */
  202. protected static function path($bundle, $language, $file)
  203. {
  204. return Bundle::path($bundle)."language/{$language}/{$file}".EXT;
  205. }
  206. /**
  207. * Get the string content of the language line.
  208. *
  209. * @return string
  210. */
  211. public function __toString()
  212. {
  213. return (string) $this->get();
  214. }
  215. }