lang.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php namespace Laravel;
  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. * @param string $key
  55. * @param array $replacements
  56. * @param string $language
  57. * @param array $paths
  58. * @return Lang
  59. */
  60. public static function line($key, $replacements = array(), $language = null, $paths = array())
  61. {
  62. if (count($paths) == 0) $paths = array(SYS_LANG_PATH, LANG_PATH);
  63. return new static($key, $replacements, $language, $paths);
  64. }
  65. /**
  66. * Get the language line.
  67. *
  68. * @param string $language
  69. * @param string $default
  70. * @return string
  71. */
  72. public function get($language = null, $default = null)
  73. {
  74. if ( ! is_null($language)) $this->language = $language;
  75. list($file, $line) = $this->parse($this->key);
  76. if ( ! $this->load($file))
  77. {
  78. return ($default instanceof \Closure) ? call_user_func($default) : $default;
  79. }
  80. $line = Arr::get(static::$lines[$this->language.$file], $line, $default);
  81. foreach ($this->replacements as $key => $value)
  82. {
  83. $line = str_replace(':'.$key, $value, $line);
  84. }
  85. return $line;
  86. }
  87. /**
  88. * Parse a language key.
  89. *
  90. * @param string $key
  91. * @return array
  92. */
  93. protected function parse($key)
  94. {
  95. if (count($segments = explode('.', $key)) > 1)
  96. {
  97. return array($segments[0], implode('.', array_slice($segments, 1)));
  98. }
  99. throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
  100. }
  101. /**
  102. * Load a language file.
  103. *
  104. * @param string $file
  105. * @return bool
  106. */
  107. protected function load($file)
  108. {
  109. if (isset(static::$lines[$this->language.$file])) return;
  110. $language = array();
  111. foreach ($this->paths as $directory)
  112. {
  113. if (file_exists($path = $directory.$this->language.'/'.$file.EXT))
  114. {
  115. $language = array_merge($language, require $path);
  116. }
  117. }
  118. if (count($language) > 0) static::$lines[$this->language.$file] = $language;
  119. return isset(static::$lines[$this->language.$file]);
  120. }
  121. /**
  122. * Get the string content of the language line.
  123. */
  124. public function __toString()
  125. {
  126. return $this->get();
  127. }
  128. }