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