3 * The language sub-system for handling language strings being used in the
4 * application and whole framework
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
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.
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.
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/>.
25 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage {
27 * The full-qualified base path for the language include files
29 private $basePath = "";
32 * The 2-char language code
34 private $langCode = "xx"; // This will later be overwritten!
37 * The array-object for all language strings
39 private $langStrings = null;
42 * An instance of this class
44 private static $thisInstance = null;
47 * Protected constructor
51 protected function __construct () {
52 // Call parent constructor
53 parent::__construct(__CLASS__);
55 // Set part description
56 $this->setObjectDescription("Language sub-system");
58 // Create unique ID number
59 $this->generateUniqueId();
62 $this->removeNumberFormaters();
63 $this->removeSystemArray();
67 * Creates an instance of the class LanguageSystem and prepares it for usage
69 * @param $basePath The local base path for all language strings
70 * @return $langInstance An instance of LanguageSystem
71 * @throws LanguagePathIsEmptyException If the provided $basePath is empty
72 * @throws InvalidLanguagePathStringException If $basePath is no string
73 * @throws LanguagePathIsNoDirectoryException If $basePath is no
74 * directory or not found
75 * @throws LanguagePathReadProtectedException If $basePath is
78 public final static function createLanguageSystem ($basePath) {
80 $langInstance = new LanguageSystem();
82 // Is the base path valid?
83 if (empty($basePath)) {
84 // Language path is empty
85 throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
86 } elseif (!is_string($basePath)) {
88 throw new InvalidLanguagePathStringException(array($langInstance, $basePath), self::EXCEPTION_INVALID_STRING);
89 } elseif (!is_dir($basePath)) {
91 throw new LanguagePathIsNoDirectoryException(array($langInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
92 } elseif (!is_readable($basePath)) {
94 throw new LanguagePathReadProtectedException(array($langInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
98 $langInstance->setBasePath($basePath);
100 // Initialize the variable stack
101 $langInstance->initLanguageStrings();
103 // Set language code from default config
104 $langInstance->setLanguageCode(FrameworkConfiguration::getInstance()->readConfig('default_lang'));
106 // Remember this instance
107 self::$thisInstance = $langInstance;
109 // Return the prepared instance
110 return $langInstance;
114 * Singleton getter for this instance
116 * @return $thisInstance An instance of this class
118 public final static function getInstance () {
119 return self::$thisInstance;
123 * Setter for base path
125 * @param $basePath The local base path for all templates
128 protected final function setBasePath ($basePath) {
130 $this->basePath = (string) $basePath;
134 * Setter for language code
136 * @param $langCode The language code for the current application
139 protected final function setLanguageCode ($langCode) {
141 $langCode = (string) $langCode;
143 // And set it (only 2 chars)
144 $this->langCode = substr($langCode, 0, 2);
148 * Initialize the array-object for all later language strings
152 public function initLanguageStrings () {
153 $this->langStrings = new FrameworkArrayObject("FakedLanguageStrings");
157 * Getter for language code
159 * @return $langCode The language code for the current application
161 public final function getLanguageCode () {
162 return $this->langCode;
166 * Get the plain message from the cache variable for the given message id
168 * @param $messageId The message id we shall find in the cache variable
169 * @return $messageText The plain message text
171 public function getMessage ($messageId) {
172 // Default is missing message text
173 $messageText = sprintf("!%s!",
177 // Try to look it up in the cache variable
178 if ($this->langStrings->offsetExists($messageId)) {
179 // Return the message string
180 $messageText = $this->langStrings->offsetGet($messageId);