lang.php 5.97 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2

3 4 5
class Lang {

	/**
6
	 * The key of the language line being retrieved.
7
	 *
8
	 * @var string
9
	 */
10
	protected $key;
11 12

	/**
13
	 * The replacements that should be made on the language line.
14 15 16
	 *
	 * @var array
	 */
17
	protected $replacements;
18 19

	/**
20
	 * The language in which the line should be retrieved.
21 22 23
	 *
	 * @var string
	 */
24
	protected $language;
25 26

	/**
27 28
	 * All of the loaded language lines.
	 *
29
	 * The array is keyed by [$bundle][$language][$file].
30 31 32
	 *
	 * @var array
	 */
33
	protected static $lines = array();
34 35

	/**
36 37 38 39 40 41 42
	 * The language loader event name.
	 *
	 * @var string
	 */
	const loader = 'laravel.language.loader';

	/**
43
	 * Create a new Lang instance.
44
	 *
45
	 * @param  string  $key
46
	 * @param  array   $replacements
47 48
	 * @param  string  $language
	 * @return void
49
	 */
50
	protected function __construct($key, $replacements = array(), $language = null)
51
	{
52
		$this->key = $key;
53
		$this->language = $language;
54
		$this->replacements = (array) $replacements;
55 56 57
	}

	/**
58 59
	 * Create a new language line instance.
	 *
60 61 62 63
	 * <code>
	 *		// Create a new language line instance for a given line
	 *		$line = Lang::line('validation.required');
	 *
64 65 66
	 *		// Create a new language line for a line belonging to a bundle
	 *		$line = Lang::line('admin::messages.welcome');
	 *
67 68 69 70
	 *		// Specify some replacements for the language line
	 *		$line = Lang::line('validation.required', array('attribute' => 'email'));
	 * </code>
	 *
71 72 73 74 75
	 * @param  string  $key
	 * @param  array   $replacements
	 * @param  string  $language
	 * @return Lang
	 */
76
	public static function line($key, $replacements = array(), $language = null)
77
	{
78
		if (is_null($language)) $language = Config::get('application.language');
79

80
		return new static($key, $replacements, $language);
81 82 83
	}

	/**
Taylor Otwell committed
84 85 86 87 88 89 90 91 92 93 94 95
	 * Determine if a language line exists.
	 *
	 * @param  string  $key
	 * @param  string  $language
	 * @return bool
	 */
	public static function has($key, $language = null)
	{
		return ! is_null(static::line($key, array(), $language)->get());
	}

	/**
96 97 98 99 100 101 102 103 104 105 106 107
	 * Get the language line as a string.
	 *
	 * <code>
	 *		// Get a language line
	 *		$line = Lang::line('validation.required')->get();
	 *
	 *		// Get a language line in a specified language
	 *		$line = Lang::line('validation.required')->get('sp');
	 *
	 *		// Return a default value if the line doesn't exist
	 *		$line = Lang::line('validation.required', null, 'Default');
	 * </code>
108
	 *
109
	 * @param  string  $language
110
	 * @param  string  $default
111 112
	 * @return string
	 */
113
	public function get($language = null, $default = null)
114
	{
115 116 117 118 119
		// If no default value is specified by the developer, we'll just return the
		// key of the language line. This should indicate which language line we
		// were attempting to render and is better than giving nothing back.
		if (is_null($default)) $default = $this->key;

120
		if (is_null($language)) $language = $this->language;
121

122
		list($bundle, $file, $line) = $this->parse($this->key);
123

124
		// If the file does not exist, we'll just return the default value that was
125
		// given to the method. The default value is also returned even when the
126
		// file exists and that file does not actually contain any lines.
127
		if ( ! static::load($bundle, $language, $file))
128
		{
129
			return value($default);
130 131
		}

132
		$lines = static::$lines[$bundle][$language][$file];
133

134 135 136 137 138 139
		$line = array_get($lines, $line, $default);

		// If the line is not a string, it probably means the developer asked for
		// the entire langauge file and the value of the requested value will be
		// an array containing all of the lines in the file.
		if (is_string($line))
140
		{
141 142 143 144
			foreach ($this->replacements as $key => $value)
			{
				$line = str_replace(':'.$key, $value, $line);
			}
145 146 147 148 149 150
		}

		return $line;
	}

	/**
151
	 * Parse a language key into its bundle, file, and line segments.
152
	 *
153
	 * Language lines follow a {bundle}::{file}.{line} naming convention.
154
	 *
155 156 157
	 * @param  string  $key
	 * @return array
	 */
158
	protected function parse($key)
159
	{
160 161 162 163 164 165 166 167
		$bundle = Bundle::name($key);

		$segments = explode('.', Bundle::element($key));

		// If there are not at least two segments in the array, it means that
		// the developer is requesting the entire language line array to be
		// returned. If that is the case, we'll make the item "null".
		if (count($segments) >= 2)
Taylor Otwell committed
168
		{
169
			$line = implode('.', array_slice($segments, 1));
170

171 172 173 174 175 176
			return array($bundle, $segments[0], $line);
		}
		else
		{
			return array($bundle, $segments[0], null);
		}
177 178 179
	}

	/**
180
	 * Load all of the language lines from a language file.
181
	 *
182 183
	 * @param  string  $bundle
	 * @param  string  $language
184
	 * @param  string  $file
Taylor Otwell committed
185
	 * @return bool
186
	 */
187
	public static function load($bundle, $language, $file)
188
	{
189
		if (isset(static::$lines[$bundle][$language][$file]))
190
		{
191
			return true;
192
		}
Taylor Otwell committed
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		// We use a "loader" event to delegate the loading of the language
		// array, which allows the develop to organize the language line
		// arrays for their application however they wish.
		$lines = Event::first(static::loader, func_get_args());

		static::$lines[$bundle][$language][$file] = $lines;

		return count($lines) > 0;
	}

	/**
	 * Load a language array from a language file.
	 *
	 * @param  string  $bundle
	 * @param  string  $language
	 * @param  string  $file
	 * @return array
	 */
	public static function file($bundle, $language, $file)
	{
214 215 216 217
		$lines = array();

		// Language files can belongs to the application or to any bundle
		// that is installed for the application. So, we'll need to use
218 219
		// the bundle's path when looking for the file.
		$path = static::path($bundle, $language, $file);
220

221 222 223 224
		if (file_exists($path))
		{
			$lines = require $path;
		}
225

226
		return $lines;
227 228 229
	}

	/**
230 231 232 233 234 235 236 237 238 239 240 241 242
	 * Get the path to a bundle's language file.
	 *
	 * @param  string  $bundle
	 * @param  string  $language
	 * @param  string  $file
	 * @return string
	 */
	protected static function path($bundle, $language, $file)
	{
		return Bundle::path($bundle)."language/{$language}/{$file}".EXT;
	}

	/**
243
	 * Get the string content of the language line.
244 245
	 *
	 * @return string
246
	 */
247 248
	public function __toString()
	{
249
		return (string) $this->get();
250
	}
251

252
}