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