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