1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2015 Twin Huang
6: * @license http://opensource.org/licenses/mit-license.php MIT License
7: */
8:
9: namespace Wei;
10:
11: /**
12: * A translator wei
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: */
16: class T extends Base
17: {
18: /**
19: * The current locale
20: *
21: * @var string
22: */
23: protected $locale = 'en';
24:
25: /**
26: * The default locale
27: *
28: * @var string
29: */
30: protected $defaultLocale = 'en';
31:
32: /**
33: * The translator messages
34: *
35: * @var array
36: */
37: protected $data = array();
38:
39: /**
40: * The loaded translation files
41: *
42: * @var array
43: */
44: protected $files = array();
45:
46: /**
47: * Translate a message
48: *
49: * @param string $message
50: * @param array $parameters
51: * @return string
52: */
53: public function __invoke($message, array $parameters = array())
54: {
55: if (isset($this->data[$message])) {
56: $message = $this->data[$message];
57: }
58: return $parameters ? strtr($message, $parameters) : $message;
59: }
60:
61: /**
62: * Translates a message
63: *
64: * @param string $message
65: * @param array $parameters
66: * @return string
67: */
68: public function trans($message, array $parameters = array())
69: {
70: return $this($message, $parameters);
71: }
72:
73: /**
74: * Sets the current locale
75: *
76: * @param string $locale
77: * @return $this
78: */
79: public function setLocale($locale)
80: {
81: $this->locale = $locale;
82: return $this;
83: }
84:
85: /**
86: * Returns the default locale
87: *
88: * @return string
89: */
90: public function getLocale()
91: {
92: return $this->locale;
93: }
94:
95: /**
96: * Set the fallback locale
97: *
98: * @param string $locale
99: * @return $this
100: */
101: public function setDefaultLocale($locale)
102: {
103: $this->defaultLocale = $locale;
104: return $this;
105: }
106:
107: /**
108: * Returns the fallback locale
109: *
110: * @return string
111: */
112: public function getDefaultLocale()
113: {
114: return $this->defaultLocale;
115: }
116:
117: /**
118: * Loads translator messages from file
119: *
120: * @param string $pattern The file path, which can contains %s that would be convert the current locale or fallback locale
121: * @return $this
122: * @throws \InvalidArgumentException When file not found or not readable
123: */
124: public function loadFromFile($pattern)
125: {
126: if (isset($this->files[$pattern])) {
127: return $this;
128: }
129:
130: $file = sprintf($pattern, $this->locale);
131: if (!is_file($file)) {
132: $defaultFile = sprintf($pattern, $this->defaultLocale);
133: if (!is_file($defaultFile)) {
134: throw new \InvalidArgumentException(sprintf('File "%s" and "%s" not found or not readable', $file, $defaultFile));
135: } else {
136: $file = $defaultFile;
137: }
138: }
139:
140: $this->files[$pattern] = true;
141:
142: return $this->loadFromArray(require $file);
143: }
144:
145: /**
146: * Loads translator messages from array
147: *
148: * @param array $messages
149: * @return $this
150: */
151: public function loadFromArray(array $messages)
152: {
153: $this->data = $messages + $this->data;
154: return $this;
155: }
156:
157: /**
158: * Loads translator messages from closure
159: *
160: * @param \Closure $fn
161: * @return $this
162: */
163: public function load(\Closure $fn)
164: {
165: return $this->loadFromArray($fn());
166: }
167: }
168: