summaryrefslogtreecommitdiff
path: root/lib/Minz/Translate.php
blob: 1b4102ca965ecd98e5fb124fd58f346616334f8e (plain)
1
2
3
4
5
6
7
8
9
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/** 
 * MINZ - Copyright 2011 Marien Fressinaud
 * Sous licence AGPL3 <http://www.gnu.org/licenses/>
 */

/**
 * This class is used for the internationalization.
 * It uses files in `./app/i18n/`
 */
class Minz_Translate {
	/**
	 * $lang_list is the list of available languages.
	 */
	private static $lang_list = array();

	/**
	 * $lang_name is the name of the current language to use.
	 */
	private static $lang_name;

	/**
	 * $lang_path is the pathname of i18n files (e.g. ./app/i18n/en/).
	 */
	private static $lang_path;

	/**
	 * $translates is a cache for i18n translation.
	 */
	private static $translates = array();

	/**
	 * Init the translation object.
	 * @param $lang_list the list of available languages.
	 * @param $lang_name the lang to show.
	 */
	public static function init($lang_list, $lang_name) {
		self::$lang_list = $lang_list;
		self::$lang_name = $lang_name;
		self::$lang_path = APP_PATH . '/i18n/' . self::$lang_name . '/';
	}

	/**
	 * Reset the translation object with a new language.
	 * @param $lang_name the new language to use
	 */
	public static function reset($lang_name) {
		self::init(self::$lang_list, $lang_name);
	}

	/**
	 * Return the list of available languages.
	 * @return an array.
	 */
	public static function availableLanguages() {
		return self::$lang_list;
	}

	/**
	 * Translate a key into its corresponding value based on selected language.
	 * @param $key the key to translate.
	 * @param additional parameters for variable keys.
	 * @return the value corresponding to the key.
	 *         If no value is found, return the key itself.
	 */ 
	public static function t($key) {
		$group = explode('.', $key);

		if (count($group) < 2) {
			Minz_Log::debug($key . ' is not in a valid format');
			$top_level = 'gen';
		} else {
			$top_level = array_shift($group);
		}

		$filename = self::$lang_path . $top_level . '.php';

		// Try to load the i18n file if it's not done yet.
		if (!isset(self::$translates[$top_level])) {
			if (!file_exists($filename)) {
				Minz_Log::debug($top_level . ' is not a valid top level key');
				return $key;
			}

			self::$translates[$top_level] = include($filename);
		}

		// Go through the i18n keys to get the correct translation value.
		$translates = self::$translates[$top_level];
		$size_group = count($group);
		$level_processed = 0;
		$translation_value = $key;
		foreach ($group as $i18n_level) {
			$level_processed++;
			if (!isset($translates[$i18n_level])) {
				Minz_Log::debug($key . ' is not a valid key');
				return $key;
			}

			if ($level_processed < $size_group) {
				$translates = $translates[$i18n_level];
			} else {
				$translation_value = $translates[$i18n_level];
			}
		}

		if (is_array($translation_value)) {
			if (isset($translation_value['_'])) {
				$translation_value = $translation_value['_'];
			} else {
				Minz_Log::debug($key . ' is not a valid key');
				return $key;
			}
		}

		// Get the facultative arguments to replace i18n variables.
		$args = func_get_args();
		unset($args[0]);

		return vsprintf($translation_value, $args);
	}

	/**
	 * Return the current language.
	 */
	public static function language() {
		return self::$lang_name;
	}
}


/**
 * Alias for Minz_Translate::t()
 */
function _t($key) {
	$args = func_get_args();
	unset($args[0]);
	array_unshift($args, $key);

	return call_user_func_array('Minz_Translate::t', $args);
}