lang.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php namespace Laravel; use Closure;
  2. class Lang {
  3. /**
  4. * All of the loaded language lines.
  5. *
  6. * The array is keyed by [$language.$file].
  7. *
  8. * @var array
  9. */
  10. protected static $lines = array();
  11. /**
  12. * The key of the language line being retrieved.
  13. *
  14. * @var string
  15. */
  16. protected $key;
  17. /**
  18. * The replacements that should be made on the language line.
  19. *
  20. * @var array
  21. */
  22. protected $replacements;
  23. /**
  24. * The language in which the line should be retrieved.
  25. *
  26. * @var string
  27. */
  28. protected $language;
  29. /**
  30. * The paths containing the language files.
  31. *
  32. * @var array
  33. */
  34. protected $paths;
  35. /**
  36. * Create a new Lang instance.
  37. *
  38. * @param string $key
  39. * @param array $replacements
  40. * @param string $language
  41. * @param array $paths
  42. * @return void
  43. */
  44. protected function __construct($key, $replacements = array(), $language = null, $paths = array())
  45. {
  46. $this->key = $key;
  47. $this->paths = $paths;
  48. $this->language = $language;
  49. $this->replacements = $replacements;
  50. }
  51. /**
  52. * Create a new language line instance.
  53. *
  54. * <code>
  55. * // Create a new language line instance for a given line
  56. * $line = Lang::line('validation.required');
  57. *
  58. * // Specify some replacements for the language line
  59. * $line = Lang::line('validation.required', array('attribute' => 'email'));
  60. * </code>
  61. *
  62. * @param string $key
  63. * @param array $replacements
  64. * @param string $language
  65. * @param array $paths
  66. * @return Lang
  67. */
  68. public static function line($key, $replacements = array(), $language = null, $paths = array())
  69. {
  70. if (count($paths) == 0) $paths = array(SYS_LANG_PATH, LANG_PATH);
  71. if (is_null($language)) $language = Config::get('application.language');
  72. return new static($key, $replacements, $language, $paths);
  73. }
  74. /**
  75. * Get the language line as a string.
  76. *
  77. * If a language is specified, it should correspond to a directory within
  78. * your application language directory.
  79. *
  80. * <code>
  81. * // Get a language line
  82. * $line = Lang::line('validation.required')->get();
  83. *
  84. * // Get a language line in a specified language
  85. * $line = Lang::line('validation.required')->get('sp');
  86. *
  87. * // Return a default value if the line doesn't exist
  88. * $line = Lang::line('validation.required', null, 'Default');
  89. * </code>
  90. *
  91. * @param string $language
  92. * @param string $default
  93. * @return string
  94. */
  95. public function get($language = null, $default = null)
  96. {
  97. // If a language was passed to the method, we will let it override the default
  98. if ( ! is_null($language)) $this->language = $language;
  99. list($file, $line) = $this->parse($this->key);
  100. if ( ! $this->load($file))
  101. {
  102. return ($default instanceof Closure) ? call_user_func($default) : $default;
  103. }
  104. $line = Arr::get(static::$lines[$this->language.$file], $line, $default);
  105. foreach ($this->replacements as $key => $value)
  106. {
  107. $line = str_replace(':'.$key, $value, $line);
  108. }
  109. return $line;
  110. }
  111. /**
  112. * Parse a language key into its file and line segments.
  113. *
  114. * @param string $key
  115. * @return array
  116. */
  117. protected function parse($key)
  118. {
  119. if (count($segments = explode('.', $key)) > 1)
  120. {
  121. return array($segments[0], implode('.', array_slice($segments, 1)));
  122. }
  123. throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
  124. }
  125. /**
  126. * Load all of the language lines from a language file.
  127. *
  128. * @param string $file
  129. * @return bool
  130. */
  131. protected function load($file)
  132. {
  133. if (isset(static::$lines[$this->language.$file])) return true;
  134. $language = array();
  135. // Language files cascade. Typically, the system language array is loaded first,
  136. // followed by the application array. This allows the convenient overriding of the
  137. // system language files (like validation) by the application developer.
  138. foreach ($this->paths as $directory)
  139. {
  140. if (file_exists($path = $directory.$this->language.'/'.$file.EXT))
  141. {
  142. $language = array_merge($language, require $path);
  143. }
  144. }
  145. // If language lines were actually found, they will be loaded into the array
  146. // containing all of the lines for all languages and files. The array is
  147. // keyed by the language and the file name.
  148. if (count($language) > 0) static::$lines[$this->language.$file] = $language;
  149. return isset(static::$lines[$this->language.$file]);
  150. }
  151. /**
  152. * Get the string content of the language line.
  153. */
  154. public function __toString() { return $this->get(); }
  155. }