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