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