lang.php 4.4 KB

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