createUniqueID -> generateUniqueId renamed, dataset criteria added, registration...
[shipsimu.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                 // Set part description
56                 $this->setObjectDescription("Language sub-system");
57
58                 // Create unique ID number
59                 $this->generateUniqueId();
60
61                 // Clean up a little
62                 $this->removeNumberFormaters();
63                 $this->removeSystemArray();
64         }
65
66         /**
67          * Creates an instance of the class LanguageSystem and prepares it for usage
68          *
69          * @param       $basePath               The local base path for all language strings
70          * @return      $langInstance   An instance of LanguageSystem
71          * @throws      LanguagePathIsEmptyException            If the provided $basePath is empty
72          * @throws      InvalidLanguagePathStringException      If $basePath is no string
73          * @throws      LanguagePathIsNoDirectoryException      If $basePath is no
74          *                                                                              directory or not found
75          * @throws      LanguagePathReadProtectedException      If $basePath is
76          *                                                                              read-protected
77          */
78         public final static function createLanguageSystem ($basePath) {
79                 // Get a new instance
80                 $langInstance = new LanguageSystem();
81
82                 // Is the base path valid?
83                 if (empty($basePath)) {
84                         // Language path is empty
85                         throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
86                 } elseif (!is_string($basePath)) {
87                         // Is not a string
88                         throw new InvalidLanguagePathStringException(array($langInstance, $basePath), self::EXCEPTION_INVALID_STRING);
89                 } elseif (!is_dir($basePath)) {
90                         // Is not a path
91                         throw new LanguagePathIsNoDirectoryException(array($langInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
92                 } elseif (!is_readable($basePath)) {
93                         // Is not readable
94                         throw new LanguagePathReadProtectedException(array($langInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
95                 }
96
97                 // Set the base path
98                 $langInstance->setBasePath($basePath);
99
100                 // Initialize the variable stack
101                 $langInstance->initLanguageStrings();
102
103                 // Set language code from default config
104                 $langInstance->setLanguageCode(FrameworkConfiguration::getInstance()->readConfig('default_lang'));
105
106                 // Remember this instance
107                 self::$thisInstance = $langInstance;
108
109                 // Return the prepared instance
110                 return $langInstance;
111         }
112
113         /**
114          * Singleton getter for this instance
115          *
116          * @return      $thisInstance           An instance of this class
117          */
118         public final static function getInstance () {
119                 return self::$thisInstance;
120         }
121
122         /**
123          * Setter for base path
124          *
125          * @param               $basePath               The local base path for all templates
126          * @return      void
127          */
128         protected final function setBasePath ($basePath) {
129                 // And set it
130                 $this->basePath = (string) $basePath;
131         }
132
133         /**
134          * Setter for language code
135          *
136          * @param               $langCode               The language code for the current application
137          * @return      void
138          */
139         protected final function setLanguageCode ($langCode) {
140                 // Cast it
141                 $langCode = (string) $langCode;
142
143                 // And set it (only 2 chars)
144                 $this->langCode = substr($langCode, 0, 2);
145         }
146
147         /**
148          * Initialize the array-object for all later language strings
149          *
150          * @return      void
151          */
152         public function initLanguageStrings () {
153                 $this->langStrings = new FrameworkArrayObject("FakedLanguageStrings");
154         }
155
156         /**
157          * Getter for language code
158          *
159          * @return      $langCode               The language code for the current application
160          */
161         public final function getLanguageCode () {
162                 return $this->langCode;
163         }
164
165         /**
166          * Get the plain message from the cache variable for the given message id
167          *
168          * @param       $messageId              The message id we shall find in the cache variable
169          * @return      $messageText    The plain message text
170          */
171         public function getMessage ($messageId) {
172                 // Default is missing message text
173                 $messageText = sprintf("!%s!",
174                         $messageId
175                 );
176
177                 // Try to look it up in the cache variable
178                 if ($this->langStrings->offsetExists($messageId)) {
179                         // Return the message string
180                         $messageText = $this->langStrings->offsetGet($messageId);
181                 }
182
183                 // Return the text
184                 return $messageText;
185         }
186 }
187
188 // [EOF]
189 ?>