1 | <?php |
||
21 | class Lang |
||
22 | { |
||
23 | const COOKIE_NAME = 'pHSLang'; |
||
24 | |||
25 | private $_oConfig, $_sDefaultLang, $_sUserLang, $_sLangName; |
||
|
|||
26 | |||
27 | public function __construct() |
||
28 | { |
||
29 | $this->_oConfig = Config::getInstance(); |
||
30 | $oCookie = new Cookie; |
||
31 | |||
32 | // Check a template name has been entered and if it meets the required length. |
||
33 | if (!empty($_REQUEST['l']) && strlen($_REQUEST['l']) == 5) { |
||
34 | $this->_sUserLang = $_REQUEST['l']; |
||
35 | $oCookie->set(static::COOKIE_NAME, $this->_sUserLang, 60*60*48); |
||
36 | } elseif ($oCookie->exists(static::COOKIE_NAME)) { |
||
37 | $this->_sUserLang = $oCookie->get(static::COOKIE_NAME); |
||
38 | } else { |
||
39 | $this->_sUserLang = (new Browser)->getLanguage(); |
||
40 | } |
||
41 | |||
42 | unset($oCookie); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Set the default language name. |
||
47 | * |
||
48 | * @param string $sNewDefLang Prefix of the language. |
||
49 | * |
||
50 | * @return self |
||
51 | */ |
||
52 | public function setDefaultLang($sNewDefLang) |
||
53 | { |
||
54 | $this->_sDefaultLang = $sNewDefLang; |
||
55 | |||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Set the user language name. |
||
61 | * |
||
62 | * @param string $sNewUserLang Prefix of the language. |
||
63 | * |
||
64 | * @return self |
||
65 | */ |
||
66 | public function setUserLang($sNewUserLang) |
||
72 | |||
73 | /** |
||
74 | * Get the default language name. |
||
75 | * |
||
76 | * @return string The prefix of the language. |
||
77 | */ |
||
78 | public function getDefaultLang() |
||
82 | |||
83 | /** |
||
84 | * Get the current language name. |
||
85 | * |
||
86 | * @return string The prefix of the language. |
||
87 | */ |
||
88 | public function getLang() |
||
92 | |||
93 | /** |
||
94 | * Get JavaScript language file. |
||
95 | * |
||
96 | * @param string $sPath The path. |
||
97 | * @param string $sFileName The language name. Default is the constant: 'PH7_LANG_CODE' |
||
98 | * |
||
99 | * @return string Valid file name (with the extension). |
||
100 | * |
||
101 | * @throws Exception If the language file is not found. |
||
102 | */ |
||
103 | public static function getJsFile($sPath, $sFileName = PH7_LANG_CODE) |
||
104 | { |
||
105 | if (is_file($sPath . $sFileName . '.js')) { |
||
106 | return $sFileName . '.js'; |
||
107 | } |
||
108 | |||
109 | if (is_file($sPath . PH7_DEFAULT_LANG_CODE . '.js')) { |
||
110 | return PH7_DEFAULT_LANG_CODE . '.js'; |
||
111 | } |
||
112 | |||
113 | throw new Exception('Language file \'' . $sPath . PH7_DEFAULT_LANG_CODE . '.js\' not found.'); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Load the language file. |
||
118 | * |
||
119 | * @param string $sFileName The language filename (e.g., "global"). |
||
120 | * @param string $sPath If you want to change the default path (the path to the current module), you can specify the path. |
||
121 | * |
||
122 | * @return self |
||
123 | */ |
||
124 | public function load($sFileName, $sPath = null) |
||
125 | { |
||
126 | textdomain($sFileName); |
||
127 | bindtextdomain($sFileName, (empty($sPath) ? Registry::getInstance()->path_module_lang : $sPath) ); |
||
128 | bind_textdomain_codeset($sFileName, PH7_ENCODING); |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Loading language files. |
||
135 | * |
||
136 | * @return self |
||
137 | * |
||
138 | * @throws Exception If the language file is not found. |
||
139 | */ |
||
140 | public function init() |
||
170 | |||
171 | /** |
||
172 | * Set the correct charset to the site. |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | private function _setEncoding() |
||
188 | } |
||
189 | |||
240 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.