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