Launcher scripts updated
[mailer.git] / inc / classes / main / language / class_LanguageSystem.php
1 <?php
2 /**
3  * The language sub-system for handling language strings being used in the
4  * application and whole framework
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage {
26         /**
27          * The full-qualified base path for the language include files
28          */
29         private $basePath = "";
30
31         /**
32          * The 2-char language code
33          */
34         private $langCode = "xx"; // This will later be overwritten!
35
36         /**
37          * The array-object for all language strings
38          */
39         private $langStrings = null;
40
41         /**
42          * An instance of this class
43          */
44         private static $thisInstance = null;
45
46         /**
47          * Protected constructor
48          *
49          * @return      void
50          */
51         protected function __construct () {
52                 // Call parent constructor
53                 parent::__construct(__CLASS__);
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Creates an instance of the class LanguageSystem and prepares it for usage
62          *
63          * @param       $basePath               The local base path for all language strings
64          * @return      $langInstance   An instance of LanguageSystem
65          * @throws      LanguagePathIsEmptyException            If the provided $basePath is empty
66          * @throws      InvalidLanguagePathStringException      If $basePath is no string
67          * @throws      LanguagePathIsNoDirectoryException      If $basePath is no
68          *                                                                              directory or not found
69          * @throws      LanguagePathReadProtectedException      If $basePath is
70          *                                                                              read-protected
71          */
72         public final static function createLanguageSystem ($basePath) {
73                 // Get a new instance
74                 $langInstance = new LanguageSystem();
75
76                 // Is the base path valid?
77                 if (empty($basePath)) {
78                         // Language path is empty
79                         throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
80                 } elseif (!is_string($basePath)) {
81                         // Is not a string
82                         throw new InvalidLanguagePathStringException(array($langInstance, $basePath), self::EXCEPTION_INVALID_STRING);
83                 } elseif (!is_dir($basePath)) {
84                         // Is not a path
85                         throw new LanguagePathIsNoDirectoryException(array($langInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
86                 } elseif (!is_readable($basePath)) {
87                         // Is not readable
88                         throw new LanguagePathReadProtectedException(array($langInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
89                 }
90
91                 // Set the base path
92                 $langInstance->setBasePath($basePath);
93
94                 // Initialize the variable stack
95                 $langInstance->initLanguageStrings();
96
97                 // Set language code from default config
98                 $langInstance->setLanguageCode(FrameworkConfiguration::getInstance()->readConfig('default_lang'));
99
100                 // Remember this instance
101                 self::$thisInstance = $langInstance;
102
103                 // Return the prepared instance
104                 return $langInstance;
105         }
106
107         /**
108          * Singleton getter for this instance
109          *
110          * @return      $thisInstance           An instance of this class
111          */
112         public final static function getInstance () {
113                 return self::$thisInstance;
114         }
115
116         /**
117          * Setter for base path
118          *
119          * @param               $basePath               The local base path for all templates
120          * @return      void
121          */
122         protected final function setBasePath ($basePath) {
123                 // And set it
124                 $this->basePath = (string) $basePath;
125         }
126
127         /**
128          * Setter for language code
129          *
130          * @param               $langCode               The language code for the current application
131          * @return      void
132          */
133         protected final function setLanguageCode ($langCode) {
134                 // Cast it
135                 $langCode = (string) $langCode;
136
137                 // And set it (only 2 chars)
138                 $this->langCode = substr($langCode, 0, 2);
139         }
140
141         /**
142          * Initialize the array-object for all later language strings
143          *
144          * @return      void
145          */
146         public function initLanguageStrings () {
147                 $this->langStrings = new FrameworkArrayObject("FakedLanguageStrings");
148         }
149
150         /**
151          * Getter for language code
152          *
153          * @return      $langCode               The language code for the current application
154          */
155         public final function getLanguageCode () {
156                 return $this->langCode;
157         }
158
159         /**
160          * Get the plain message from the cache variable for the given message id
161          *
162          * @param       $messageId              The message id we shall find in the cache variable
163          * @return      $messageText    The plain message text
164          */
165         public function getMessage ($messageId) {
166                 // Default is missing message text
167                 $messageText = sprintf("!%s!",
168                         $messageId
169                 );
170
171                 // Try to look it up in the cache variable
172                 if ($this->langStrings->offsetExists($messageId)) {
173                         // Return the message string
174                         $messageText = $this->langStrings->offsetGet($messageId);
175                 }
176
177                 // Return the text
178                 return $messageText;
179         }
180 }
181
182 // [EOF]
183 ?>