]> 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\Helper\Application\ApplicationHelper;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\ObjectArray\FrameworkArrayObject;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
14
15 // Import SPL stuff
16 use \InvalidArgumentException;
17
18 /**
19  * The language sub-system for handling language strings being used in the
20  * application and whole framework
21  *
22  * @author              Roland Haeder <webmaster@shipsimu.org>
23  * @version             0.0.0
24  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
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         private 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      InvalidArgumentException        If $languageBasePath is no string
79          * @throws      InvalidArgumentException        If $languageBasePath is no
80          *                                                                              directory or not found
81          * @throws      InvalidArgumentException        If $languageBasePath is
82          *                                                                              read-protected
83          */
84         public static final function createLanguageSystem (string $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 = ApplicationHelper::getSelfInstance();
93
94                         // 2) Try to build it
95                         $languageBasePath = sprintf('%s%s/language/',
96                                 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path'),
97                                 $applicationInstance->getAppShortName()
98                         );
99                 }
100
101                 // Is the base path valid?
102                 if (empty($languageBasePath)) {
103                         // Language path is empty
104                         throw new InvalidArgumentException('languageBasePath is still empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
105                 } elseif (!is_dir($languageBasePath)) {
106                         // Is not a path
107                         throw new InvalidArgumentException(sprintf('languageBasePath=%s not found', $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
108                 } elseif (!is_readable($languageBasePath)) {
109                         // Is not readable
110                         throw new InvalidArgumentException(sprintf('Cannot read from languageBasePath=%s', $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 (string $languageBasePath) {
145                 // And set it
146                 $this->languageBasePath = $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 (string $langCode) {
156                 // And set it (only 2 chars)
157                 $this->langCode = substr($langCode, 0, 2);
158         }
159
160         /**
161          * Initialize the array-object for all later language strings
162          *
163          * @return      void
164          */
165         public function initLanguageStrings () {
166                 /*
167                  * Locale category constants are usually predefined, but may not be
168                  * on some systems such as Win32.
169                  *
170                  * Origin: StatusNet's lib/language.php
171                  */
172                 foreach ([
173                         'LC_CTYPE',
174                         'LC_NUMERIC',
175                         'LC_TIME',
176                         'LC_COLLATE',
177                         'LC_MONETARY',
178                         'LC_MESSAGES',
179                         'LC_ALL'
180                 ] as $key => $name) {
181                         // Is it set?
182                         if (!defined($name)) {
183                                 // No, then set it
184                                 define($name, $key);
185                         }
186                 }
187
188                 // Init language strings array
189                 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
190         }
191
192         /**
193          * Getter for language code
194          *
195          * @return      $langCode       The language code for the current application
196          */
197         public final function getLanguageCode () {
198                 return $this->langCode;
199         }
200
201         /**
202          * Get the plain message from the cache variable for the given message id
203          *
204          * @param       $messageId              The message id we shall find in the cache variable
205          * @return      $messageText    The plain message text
206          */
207         public function getMessage (string $messageId) {
208                 // Default is missing message text
209                 $messageText = sprintf('!%s!',
210                         $messageId
211                 );
212
213                 // Try to look it up in the cache variable
214                 if ($this->langStrings->offsetExists($messageId)) {
215                         // Return the message string
216                         $messageText = $this->langStrings->offsetGet($messageId);
217                 }
218
219                 // Return the text
220                 return $messageText;
221         }
222
223 }