lang.php 4.77 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
	 * Create a new Lang instance.
37
	 *
38
	 * @param  string  $key
39
	 * @param  array   $replacements
40 41
	 * @param  string  $language
	 * @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
	}

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

73
		return new static($key, $replacements, $language);
74 75 76
	}

	/**
77 78 79 80 81 82 83 84 85 86 87 88
	 * 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>
89
	 *
90
	 * @param  string  $language
91
	 * @param  string  $default
92 93
	 * @return string
	 */
94
	public function get($language = null, $default = null)
95
	{
96
		if (is_null($language)) $language = $this->language;
97

98
		list($bundle, $file, $line) = $this->parse($this->key);
99

100 101 102 103
		// If the file doesn't exist, we'll just return the default value that was
		// given to the method. The default value is also returned even when the
		// file exists and the file does not actually contain any lines.
		if ( ! static::load($bundle, $language, $file))
104
		{
105
			return value($default);
106 107
		}

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

110 111 112 113 114 115
		$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))
116
		{
117 118 119 120
			foreach ($this->replacements as $key => $value)
			{
				$line = str_replace(':'.$key, $value, $line);
			}
121 122 123 124 125 126
		}

		return $line;
	}

	/**
127
	 * Parse a language key into its bundle, file, and line segments.
128
	 *
129
	 * Language lines follow a {bundle}::{file}.{line} naming convention.
130
	 *
131 132 133
	 * @param  string  $key
	 * @return array
	 */
134
	protected function parse($key)
135
	{
136 137 138 139 140 141 142 143
		$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
144
		{
145
			$line = implode('.', array_slice($segments, 1));
146

147 148 149 150 151 152
			return array($bundle, $segments[0], $line);
		}
		else
		{
			return array($bundle, $segments[0], null);
		}
153 154 155
	}

	/**
156
	 * Load all of the language lines from a language file.
157
	 *
158 159
	 * @param  string  $bundle
	 * @param  string  $language
160
	 * @param  string  $file
Taylor Otwell committed
161
	 * @return bool
162
	 */
163
	public static function load($bundle, $language, $file)
164
	{
165
		if (isset(static::$lines[$bundle][$language][$file]))
166
		{
167
			return true;
168
		}
Taylor Otwell committed
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		$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
		// the bundle's path when checking for the file.
		//
		// This is similar to the loading method for configuration files,
		// but we do not need to cascade across directories since most
		// likely language files are static across environments.
		$path = Bundle::path($bundle)."language/{$language}/{$file}".EXT;

		if (file_exists($path)) $lines = require $path;

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

		return count($lines) > 0;
186 187 188
	}

	/**
189
	 * Get the string content of the language line.
190 191
	 *
	 * @return string
192
	 */
193 194
	public function __toString()
	{
195
		return (string) $this->get();
196
	}
197

198
}