lang.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 [$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. * // Specify some replacements for the language line
  51. * $line = Lang::line('validation.required', array('attribute' => 'email'));
  52. * </code>
  53. *
  54. * @param string $key
  55. * @param array $replacements
  56. * @param string $language
  57. * @return Lang
  58. */
  59. public static function line($key, $replacements = array(), $language = null)
  60. {
  61. if (is_null($language))
  62. {
  63. $language = Config::$items['application']['language'];
  64. }
  65. return new static($key, $replacements, $language);
  66. }
  67. /**
  68. * Get the language line as a string.
  69. *
  70. * If a language is specified, it should correspond to a directory
  71. * within your application language directory.
  72. *
  73. * <code>
  74. * // Get a language line
  75. * $line = Lang::line('validation.required')->get();
  76. *
  77. * // Get a language line in a specified language
  78. * $line = Lang::line('validation.required')->get('sp');
  79. *
  80. * // Return a default value if the line doesn't exist
  81. * $line = Lang::line('validation.required', null, 'Default');
  82. * </code>
  83. *
  84. * @param string $language
  85. * @param string $default
  86. * @return string
  87. */
  88. public function get($language = null, $default = null)
  89. {
  90. if (is_null($language)) $language = $this->language;
  91. list($file, $line) = $this->parse($this->key);
  92. if ( ! $this->load($file))
  93. {
  94. return ($default instanceof Closure) ? call_user_func($default) : $default;
  95. }
  96. return $this->replace(Arr::get(static::$lines[$language][$file], $line, $default));
  97. }
  98. /**
  99. * Make all necessary replacements on a language line.
  100. *
  101. * Replacements place-holder are prefixed with a colon, and are replaced
  102. * with the appropriate value based on the replacement array set for the
  103. * language line instance.
  104. *
  105. * @param string $line
  106. * @return string
  107. */
  108. protected function replace($line)
  109. {
  110. foreach ($this->replacements as $key => $value)
  111. {
  112. $line = str_replace(':'.$key, $value, $line);
  113. }
  114. return $line;
  115. }
  116. /**
  117. * Parse a language key into its file and line segments.
  118. *
  119. * Language keys are formatted similarly to configuration keys. The first
  120. * segment represents the language file, while the second segment
  121. * represents a language line within that file.
  122. *
  123. * @param string $key
  124. * @return array
  125. */
  126. protected function parse($key)
  127. {
  128. if (count($segments = explode('.', $key)) > 1)
  129. {
  130. return array($segments[0], implode('.', array_slice($segments, 1)));
  131. }
  132. throw new \InvalidArgumentException("Invalid language line [$key].");
  133. }
  134. /**
  135. * Load all of the language lines from a language file.
  136. *
  137. * If the language file is successfully loaded, true will be returned.
  138. *
  139. * @param string $file
  140. * @return bool
  141. */
  142. protected function load($file)
  143. {
  144. if (isset(static::$lines[$this->language][$file])) return true;
  145. $language = array();
  146. if (file_exists($path = LANG_PATH.$this->language.'/'.$file.EXT))
  147. {
  148. $language = array_merge($language, require $path);
  149. }
  150. // If language lines were actually found, they will be loaded into
  151. // the array containing all of the lines for all languages and files.
  152. // The array is keyed by the language and the file name.
  153. if (count($language) > 0)
  154. {
  155. static::$lines[$this->language][$file] = $language;
  156. }
  157. return isset(static::$lines[$this->language][$file]);
  158. }
  159. /**
  160. * Get the string content of the language line.
  161. */
  162. public function __toString()
  163. {
  164. return $this->get();
  165. }
  166. }