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