]> 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      InvalidArgumentException        If $languageBasePath is no string
77          * @throws      InvalidArgumentException        If $languageBasePath is no
78          *                                                                              directory or not found
79          * @throws      InvalidArgumentException        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_dir($languageBasePath)) {
104                         // Is not a path
105                         throw new InvalidArgumentException(sprintf('languageBasePath=%s not found', $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
106                 } elseif (!is_readable($languageBasePath)) {
107                         // Is not readable
108                         throw new InvalidArgumentException(sprintf('Cannot read from languageBasePath=%s', $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
109                 }
110
111                 // Set the base path
112                 $langInstance->setLanguageBasePath($languageBasePath);
113
114                 // Initialize the variable stack
115                 $langInstance->initLanguageStrings();
116
117                 // Set language code from default config
118                 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
119
120                 // Remember this instance
121                 self::$selfInstance = $langInstance;
122
123                 // Return the prepared instance
124                 return $langInstance;
125         }
126
127         /**
128          * Singleton getter for this instance
129          *
130          * @return      $selfInstance   An instance of this class
131          */
132         public static final function getSelfInstance () {
133                 return self::$selfInstance;
134         }
135
136         /**
137          * Setter for base path
138          *
139          * @param       $languageBasePath       The relative base path for all language files
140          * @return      void
141          */
142         protected final function setLanguageBasePath (string $languageBasePath) {
143                 // And set it
144                 $this->languageBasePath = (string) $languageBasePath;
145         }
146
147         /**
148          * Setter for language code
149          *
150          * @param       $langCode       The language code for the current application
151          * @return      void
152          */
153         protected final function setLanguageCode (string $langCode) {
154                 // And set it (only 2 chars)
155                 $this->langCode = substr($langCode, 0, 2);
156         }
157
158         /**
159          * Initialize the array-object for all later language strings
160          *
161          * @return      void
162          */
163         public function initLanguageStrings () {
164                 /*
165                  * Locale category constants are usually predefined, but may not be
166                  * on some systems such as Win32.
167                  *
168                  * Origin: StatusNet's lib/language.php
169                  */
170                 $localeCategories = array(
171                         'LC_CTYPE',
172                         'LC_NUMERIC',
173                         'LC_TIME',
174                         'LC_COLLATE',
175                         'LC_MONETARY',
176                         'LC_MESSAGES',
177                         'LC_ALL'
178                 );
179
180                 // Set all, if not defined
181                 foreach ($localeCategories as $key => $name) {
182                         // Is it set?
183                         if (!defined($name)) {
184                                 // No, then set it
185                                 define($name, $key);
186                         } // END - if
187                 } // END - foreach
188
189                 // Init language strings array
190                 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
191         }
192
193         /**
194          * Getter for language code
195          *
196          * @return      $langCode       The language code for the current application
197          */
198         public final function getLanguageCode () {
199                 return $this->langCode;
200         }
201
202         /**
203          * Get the plain message from the cache variable for the given message id
204          *
205          * @param       $messageId              The message id we shall find in the cache variable
206          * @return      $messageText    The plain message text
207          */
208         public function getMessage (string $messageId) {
209                 // Default is missing message text
210                 $messageText = sprintf('!%s!',
211                         $messageId
212                 );
213
214                 // Try to look it up in the cache variable
215                 if ($this->langStrings->offsetExists($messageId)) {
216                         // Return the message string
217                         $messageText = $this->langStrings->offsetGet($messageId);
218                 } // END - if
219
220                 // Return the text
221                 return $messageText;
222         }
223
224 }