lang.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php namespace Laravel;
  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 = (array) $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 static::line($key, array(), $language)->get() !== $key;
  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')->get(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 no default value is specified by the developer, we'll just return the
  105. // key of the language line. This should indicate which language line we
  106. // were attempting to render and is better than giving nothing back.
  107. if (is_null($default)) $default = $this->key;
  108. if (is_null($language)) $language = $this->language;
  109. list($bundle, $file, $line) = $this->parse($this->key);
  110. // If the file does not exist, we'll just return the default value that was
  111. // given to the method. The default value is also returned even when the
  112. // file exists and that file does not actually contain any lines.
  113. if ( ! static::load($bundle, $language, $file))
  114. {
  115. return value($default);
  116. }
  117. $lines = static::$lines[$bundle][$language][$file];
  118. $line = array_get($lines, $line, $default);
  119. // If the line is not a string, it probably means the developer asked for
  120. // the entire language file and the value of the requested value will be
  121. // an array containing all of the lines in the file.
  122. if (is_string($line))
  123. {
  124. foreach ($this->replacements as $key => $value)
  125. {
  126. $line = str_replace(':'.$key, $value, $line);
  127. }
  128. }
  129. return $line;
  130. }
  131. /**
  132. * Parse a language key into its bundle, file, and line segments.
  133. *
  134. * Language lines follow a {bundle}::{file}.{line} naming convention.
  135. *
  136. * @param string $key
  137. * @return array
  138. */
  139. protected function parse($key)
  140. {
  141. $bundle = Bundle::name($key);
  142. $segments = explode('.', Bundle::element($key));
  143. // If there are not at least two segments in the array, it means that
  144. // the developer is requesting the entire language line array to be
  145. // returned. If that is the case, we'll make the item "null".
  146. if (count($segments) >= 2)
  147. {
  148. $line = implode('.', array_slice($segments, 1));
  149. return array($bundle, $segments[0], $line);
  150. }
  151. else
  152. {
  153. return array($bundle, $segments[0], null);
  154. }
  155. }
  156. /**
  157. * Load all of the language lines from a language file.
  158. *
  159. * @param string $bundle
  160. * @param string $language
  161. * @param string $file
  162. * @return bool
  163. */
  164. public static function load($bundle, $language, $file)
  165. {
  166. if (isset(static::$lines[$bundle][$language][$file]))
  167. {
  168. return true;
  169. }
  170. // We use a "loader" event to delegate the loading of the language
  171. // array, which allows the develop to organize the language line
  172. // arrays for their application however they wish.
  173. $lines = Event::first(static::loader, func_get_args());
  174. static::$lines[$bundle][$language][$file] = $lines;
  175. return count($lines) > 0;
  176. }
  177. /**
  178. * Load a language array from a language file.
  179. *
  180. * @param string $bundle
  181. * @param string $language
  182. * @param string $file
  183. * @return array
  184. */
  185. public static function file($bundle, $language, $file)
  186. {
  187. $lines = array();
  188. // Language files can belongs to the application or to any bundle
  189. // that is installed for the application. So, we'll need to use
  190. // the bundle's path when looking for the file.
  191. $path = static::path($bundle, $language, $file);
  192. if (file_exists($path))
  193. {
  194. $lines = require $path;
  195. }
  196. return $lines;
  197. }
  198. /**
  199. * Get the path to a bundle's language file.
  200. *
  201. * @param string $bundle
  202. * @param string $language
  203. * @param string $file
  204. * @return string
  205. */
  206. protected static function path($bundle, $language, $file)
  207. {
  208. return Bundle::path($bundle)."language/{$language}/{$file}".EXT;
  209. }
  210. /**
  211. * Get the string content of the language line.
  212. *
  213. * @return string
  214. */
  215. public function __toString()
  216. {
  217. return (string) $this->get();
  218. }
  219. }