lang.php 3.35 KB
Newer Older
1 2 3 4 5 6 7
<?php namespace System;

class Lang {

	/**
	 * All of the loaded language lines.
	 *
8 9
	 * The array is keyed by [$language.$file].
	 *
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	 * @var array
	 */
	private static $lines = array();

	/**
	 * The key of the line that is being requested.
	 *
	 * @var string
	 */
	private $key;

	/**
	 * The place-holder replacements.
	 *
	 * @var array
	 */
	private $replacements = array();

	/**
	 * Create a new Lang instance.
	 *
	 * @param  string  $line
	 * @return void
	 */
	public function __construct($key)
	{
		$this->key = $key;
	}

	/**
	 * Create a Lang instance for a language line.
	 *
	 * @param  string  $key
	 * @return Lang
	 */
	public static function line($key)
	{
		return new static($key);
	}

	/**
51
	 * Get the language line.
52
	 *
53
	 * @param  mixed   $default
54 55
	 * @return string
	 */
56
	public function get($default = null)
57
	{
58
		$language = Config::get('application.language');
59 60 61 62 63 64

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

		$this->load($file, $language);

		// --------------------------------------------------------------
65 66 67 68 69 70 71 72
		// If the language file did not exist, return the default value.
		// --------------------------------------------------------------
		if ( ! array_key_exists($language.$file, static::$lines))
		{
			return $default;
		}

		// --------------------------------------------------------------
73
		// Get the language line from the appropriate file array.
74
		// If the line doesn't exist, return the default value.
75 76 77 78 79 80 81
		// --------------------------------------------------------------
		if (array_key_exists($line, static::$lines[$language.$file]))
		{
			$line = static::$lines[$language.$file][$line];
		}
		else
		{
82
			return $default;
83 84 85
		}

		// --------------------------------------------------------------
86 87
		// Make all place-holder replacements. Place-holders are prefixed
		// with a colon for convenient location.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		// --------------------------------------------------------------
		foreach ($this->replacements as $key => $value)
		{
			$line = str_replace(':'.$key, $value, $line);
		}

		return $line;
	}

	/**
	 * Parse a language key.
	 *
	 * @param  string  $key
	 * @return array
	 */
	private function parse($key)
	{
105 106 107 108
		// --------------------------------------------------------------
		// The left side of the dot is the file name, while the right
		// side of the dot is the item within that file being requested.
		// --------------------------------------------------------------
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
		$segments = explode('.', $key);

		if (count($segments) < 2)
		{
			throw new \Exception("Invalid language key [$key].");
		}

		return array($segments[0], implode('.', array_slice($segments, 1)));
	}

	/**
	 * Load a language file.
	 *
	 * @param  string  $file
	 * @param  string  $language
	 * @return void
	 */
	private function load($file, $language)
	{
128
		// --------------------------------------------------------------
129 130
		// If we have already loaded the language file or the file
		// doesn't exist, bail out.
131
		// --------------------------------------------------------------
132
		if (array_key_exists($language.$file, static::$lines) or ! file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT))
133 134 135 136
		{
			return;
		}

137
		static::$lines[$language.$file] = require $path;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	}

	/**
	 * Set the place-holder replacements.
	 *
	 * @param  array  $replacements
	 * @return Lang 
	 */
	public function replace($replacements)
	{
		$this->replacements = $replacements;
		return $this;
	}

}