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