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