7d2ac7503400303008afef2842d35da5a124b32e
[core.git] / inc / main / classes / language / class_LanguageSystem.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Localization;
4
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registerable;
9
10 /**
11  * The language sub-system for handling language strings being used in the
12  * application and whole framework
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
34         /**
35          * The full-qualified base path for the language include files
36          */
37         private $languageBasePath = '';
38
39         /**
40          * The 2-char language code
41          */
42         private $langCode = 'xx'; // This will later be overwritten!
43
44         /**
45          * The array-object for all language strings
46          */
47         private $langStrings = NULL;
48
49         /**
50          * An instance of this class
51          */
52         private static $selfInstance = NULL;
53
54         /**
55          * Protected constructor
56          *
57          * @return      void
58          */
59         protected function __construct () {
60                 // Call parent constructor
61                 parent::__construct(__CLASS__);
62         }
63
64         /**
65          * Creates an instance of the class LanguageSystem and prepares it for usage
66          *
67          * @param       $languageBasePath       The local base path for all language strings or emty for auto-detection
68          * @return      $langInstance   An instance of LanguageSystem
69          * @throws      LanguagePathIsEmptyException    If the provided $languageBasePath is empty
70          * @throws      InvalidLanguagePathStringException      If $languageBasePath is no string
71          * @throws      LanguagePathIsNoDirectoryException      If $languageBasePath is no
72          *                                                                              directory or not found
73          * @throws      LanguagePathReadProtectedException      If $languageBasePath is
74          *                                                                              read-protected
75          */
76         public static final function createLanguageSystem ($languageBasePath = '') {
77                 // Get a new instance
78                 $langInstance = new LanguageSystem();
79
80                 // Is the base path set?
81                 if (empty($languageBasePath)) {
82                         // No, then attempt "auto-dection":
83                         // 1) Get application
84                         $applicationInstance = Registry::getRegistry()->getInstance('app');
85
86                         // 2) Try to build it
87                         $languageBasePath = sprintf('%sapplication/%s/language/',
88                                 $langInstance->getConfigInstance()->getConfigEntry('base_path'),
89                                 // Don't allow any underscores/dashes in application names
90                                 str_replace(array('_', '-'), array('', ''), $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(FrameworkConfiguration::getSelfInstance()->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 }