lang.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. * Create a new Lang instance.
  31. *
  32. * @param string $key
  33. * @param array $replacements
  34. * @param string $language
  35. * @return void
  36. */
  37. protected function __construct($key, $replacements = array(), $language = null)
  38. {
  39. $this->key = $key;
  40. $this->language = $language;
  41. $this->replacements = $replacements;
  42. }
  43. /**
  44. * Create a new language line instance.
  45. *
  46. * <code>
  47. * // Create a new language line instance for a given line
  48. * $line = Lang::line('validation.required');
  49. *
  50. * // Create a new language line for a line belonging to a bundle
  51. * $line = Lang::line('admin::messages.welcome');
  52. *
  53. * // Specify some replacements for the language line
  54. * $line = Lang::line('validation.required', array('attribute' => 'email'));
  55. * </code>
  56. *
  57. * @param string $key
  58. * @param array $replacements
  59. * @param string $language
  60. * @return Lang
  61. */
  62. public static function line($key, $replacements = array(), $language = null)
  63. {
  64. if (is_null($language)) $language = Config::get('application.language');
  65. return new static($key, $replacements, $language);
  66. }
  67. /**
  68. * Get the language line as a string.
  69. *
  70. * <code>
  71. * // Get a language line
  72. * $line = Lang::line('validation.required')->get();
  73. *
  74. * // Get a language line in a specified language
  75. * $line = Lang::line('validation.required')->get('sp');
  76. *
  77. * // Return a default value if the line doesn't exist
  78. * $line = Lang::line('validation.required', null, 'Default');
  79. * </code>
  80. *
  81. * @param string $language
  82. * @param string $default
  83. * @return string
  84. */
  85. public function get($language = null, $default = null)
  86. {
  87. if (is_null($language)) $language = $this->language;
  88. list($bundle, $file, $line) = $this->parse($this->key);
  89. // If the file doesn't exist, we'll just return the default value that was
  90. // given to the method. The default value is also returned even when the
  91. // file exists and the file does not actually contain any lines.
  92. if ( ! static::load($bundle, $language, $file))
  93. {
  94. return value($default);
  95. }
  96. $lines = static::$lines[$bundle][$language][$file];
  97. $line = array_get($lines, $line, $default);
  98. // If the line is not a string, it probably means the developer asked for
  99. // the entire langauge file and the value of the requested value will be
  100. // an array containing all of the lines in the file.
  101. if (is_string($line))
  102. {
  103. foreach ($this->replacements as $key => $value)
  104. {
  105. $line = str_replace(':'.$key, $value, $line);
  106. }
  107. }
  108. return $line;
  109. }
  110. /**
  111. * Parse a language key into its bundle, file, and line segments.
  112. *
  113. * Language lines follow a {bundle}::{file}.{line} naming convention.
  114. *
  115. * @param string $key
  116. * @return array
  117. */
  118. protected function parse($key)
  119. {
  120. $bundle = Bundle::name($key);
  121. $segments = explode('.', Bundle::element($key));
  122. // If there are not at least two segments in the array, it means that
  123. // the developer is requesting the entire language line array to be
  124. // returned. If that is the case, we'll make the item "null".
  125. if (count($segments) >= 2)
  126. {
  127. $line = implode('.', array_slice($segments, 1));
  128. return array($bundle, $segments[0], $line);
  129. }
  130. else
  131. {
  132. return array($bundle, $segments[0], null);
  133. }
  134. }
  135. /**
  136. * Load all of the language lines from a language file.
  137. *
  138. * @param string $bundle
  139. * @param string $language
  140. * @param string $file
  141. * @return bool
  142. */
  143. public static function load($bundle, $language, $file)
  144. {
  145. if (isset(static::$lines[$bundle][$language][$file]))
  146. {
  147. return true;
  148. }
  149. $lines = array();
  150. // Language files can belongs to the application or to any bundle
  151. // that is installed for the application. So, we'll need to use
  152. // the bundle's path when checking for the file.
  153. //
  154. // This is similar to the loading method for configuration files,
  155. // but we do not need to cascade across directories since most
  156. // likely language files are static across environments.
  157. $path = Bundle::path($bundle)."language/{$language}/{$file}".EXT;
  158. if (file_exists($path)) $lines = require $path;
  159. static::$lines[$bundle][$language][$file] = $lines;
  160. return count($lines) > 0;
  161. }
  162. /**
  163. * Get the string content of the language line.
  164. *
  165. * @return string
  166. */
  167. public function __toString()
  168. {
  169. return (string) $this->get();
  170. }
  171. }