Rewrites:
[core.git] / framework / main / classes / language / class_LanguageSystem.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Localization;
4
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registerable;
9 use CoreFramework\Registry\Registry;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13
14 /**
15  * The language sub-system for handling language strings being used in the
16  * application and whole framework
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
38         /**
39          * The full-qualified base path for the language include files
40          */
41         private $languageBasePath = '';
42
43         /**
44          * The 2-char language code
45          */
46         private $langCode = 'xx'; // This will later be overwritten!
47
48         /**
49          * The array-object for all language strings
50          */
51         private $langStrings = NULL;
52
53         /**
54          * An instance of this class
55          */
56         private static $selfInstance = NULL;
57
58         /**
59          * Protected constructor
60          *
61          * @return      void
62          */
63         protected function __construct () {
64                 // Call parent constructor
65                 parent::__construct(__CLASS__);
66         }
67
68         /**
69          * Creates an instance of the class LanguageSystem and prepares it for usage
70          *
71          * @param       $languageBasePath       The local base path for all language strings or emty for auto-detection
72          * @return      $langInstance   An instance of LanguageSystem
73          * @throws      InvalidArgumentException        If languageBasePath remains empty (@TODO Get rid of that old-lost code)
74          * @throws      InvalidLanguagePathStringException      If $languageBasePath is no string
75          * @throws      LanguagePathIsNoDirectoryException      If $languageBasePath is no
76          *                                                                              directory or not found
77          * @throws      LanguagePathReadProtectedException      If $languageBasePath is
78          *                                                                              read-protected
79          */
80         public static final function createLanguageSystem ($languageBasePath = '') {
81                 // Get a new instance
82                 $langInstance = new LanguageSystem();
83
84                 // Is the base path set?
85                 if (empty($languageBasePath)) {
86                         // No, then attempt "auto-dection":
87                         // 1) Get application
88                         $applicationInstance = Registry::getRegistry()->getInstance('app');
89
90                         // 2) Try to build it
91                         $languageBasePath = sprintf('%s%s/language/',
92                                 $langInstance->getConfigInstance()->getConfigEntry('application_base_path'),
93                                 $applicationInstance->getAppShortName()
94                         );
95                 } // END - if
96
97                 // Is the base path valid?
98                 if (empty($languageBasePath)) {
99                         // Language path is empty
100                         throw new InvalidArgumentException('languageBasePath is still empty');
101                 } elseif (!is_string($languageBasePath)) {
102                         // Is not a string
103                         throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
104                 } elseif (!is_dir($languageBasePath)) {
105                         // Is not a path
106                         throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107                 } elseif (!is_readable($languageBasePath)) {
108                         // Is not readable
109                         throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
110                 }
111
112                 // Set the base path
113                 $langInstance->setLanguageBasePath($languageBasePath);
114
115                 // Initialize the variable stack
116                 $langInstance->initLanguageStrings();
117
118                 // Set language code from default config
119                 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
120
121                 // Remember this instance
122                 self::$selfInstance = $langInstance;
123
124                 // Return the prepared instance
125                 return $langInstance;
126         }
127
128         /**
129          * Singleton getter for this instance
130          *
131          * @return      $selfInstance   An instance of this class
132          */
133         public static final function getSelfInstance () {
134                 return self::$selfInstance;
135         }
136
137         /**
138          * Setter for base path
139          *
140          * @param       $languageBasePath       The relative base path for all language files
141          * @return      void
142          */
143         protected final function setLanguageBasePath ($languageBasePath) {
144                 // And set it
145                 $this->languageBasePath = (string) $languageBasePath;
146         }
147
148         /**
149          * Setter for language code
150          *
151          * @param       $langCode       The language code for the current application
152          * @return      void
153          */
154         protected final function setLanguageCode ($langCode) {
155                 // Cast it
156                 $langCode = (string) $langCode;
157
158                 // And set it (only 2 chars)
159                 $this->langCode = substr($langCode, 0, 2);
160         }
161
162         /**
163          * Initialize the array-object for all later language strings
164          *
165          * @return      void
166          */
167         public function initLanguageStrings () {
168                 /*
169                  * Locale category constants are usually predefined, but may not be
170                  * on some systems such as Win32.
171                  *
172                  * Origin: StatusNet's lib/language.php
173                  */
174                 $localeCategories = array(
175                         'LC_CTYPE',
176                         'LC_NUMERIC',
177                         'LC_TIME',
178                         'LC_COLLATE',
179                         'LC_MONETARY',
180                         'LC_MESSAGES',
181                         'LC_ALL'
182                 );
183
184                 // Set all, if not defined
185                 foreach ($localeCategories as $key => $name) {
186                         // Is it set?
187                         if (!defined($name)) {
188                                 // No, then set it
189                                 define($name, $key);
190                         } // END - if
191                 } // END - foreach
192
193                 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
194         }
195
196         /**
197          * Getter for language code
198          *
199          * @return      $langCode       The language code for the current application
200          */
201         public final function getLanguageCode () {
202                 return $this->langCode;
203         }
204
205         /**
206          * Get the plain message from the cache variable for the given message id
207          *
208          * @param       $messageId              The message id we shall find in the cache variable
209          * @return      $messageText    The plain message text
210          */
211         public function getMessage ($messageId) {
212                 // Default is missing message text
213                 $messageText = sprintf('!%s!',
214                         $messageId
215                 );
216
217                 // Try to look it up in the cache variable
218                 if ($this->langStrings->offsetExists($messageId)) {
219                         // Return the message string
220                         $messageText = $this->langStrings->offsetGet($messageId);
221                 } // END - if
222
223                 // Return the text
224                 return $messageText;
225         }
226
227 }