3 namespace CoreFramework\Localization;
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registerable;
9 use CoreFramework\Registry\Registry;
12 * The language sub-system for handling language strings being used in the
13 * application and whole framework
15 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
34 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
36 * The full-qualified base path for the language include files
38 private $languageBasePath = '';
41 * The 2-char language code
43 private $langCode = 'xx'; // This will later be overwritten!
46 * The array-object for all language strings
48 private $langStrings = NULL;
51 * An instance of this class
53 private static $selfInstance = NULL;
56 * Protected constructor
60 protected function __construct () {
61 // Call parent constructor
62 parent::__construct(__CLASS__);
66 * Creates an instance of the class LanguageSystem and prepares it for usage
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
77 public static final function createLanguageSystem ($languageBasePath = '') {
79 $langInstance = new LanguageSystem();
81 // Is the base path set?
82 if (empty($languageBasePath)) {
83 // No, then attempt "auto-dection":
85 $applicationInstance = Registry::getRegistry()->getInstance('app');
88 $languageBasePath = sprintf('%s%s/language/',
89 $langInstance->getConfigInstance()->getConfigEntry('application_base_path'),
90 $applicationInstance->getAppShortName()
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)) {
100 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
101 } elseif (!is_dir($languageBasePath)) {
103 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
104 } elseif (!is_readable($languageBasePath)) {
106 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
110 $langInstance->setLanguageBasePath($languageBasePath);
112 // Initialize the variable stack
113 $langInstance->initLanguageStrings();
115 // Set language code from default config
116 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
118 // Remember this instance
119 self::$selfInstance = $langInstance;
121 // Return the prepared instance
122 return $langInstance;
126 * Singleton getter for this instance
128 * @return $selfInstance An instance of this class
130 public static final function getSelfInstance () {
131 return self::$selfInstance;
135 * Setter for base path
137 * @param $languageBasePath The relative base path for all language files
140 protected final function setLanguageBasePath ($languageBasePath) {
142 $this->languageBasePath = (string) $languageBasePath;
146 * Setter for language code
148 * @param $langCode The language code for the current application
151 protected final function setLanguageCode ($langCode) {
153 $langCode = (string) $langCode;
155 // And set it (only 2 chars)
156 $this->langCode = substr($langCode, 0, 2);
160 * Initialize the array-object for all later language strings
164 public function initLanguageStrings () {
166 * Locale category constants are usually predefined, but may not be
167 * on some systems such as Win32.
169 * Origin: StatusNet's lib/language.php
171 $localeCategories = array(
181 // Set all, if not defined
182 foreach ($localeCategories as $key => $name) {
184 if (!defined($name)) {
190 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
194 * Getter for language code
196 * @return $langCode The language code for the current application
198 public final function getLanguageCode () {
199 return $this->langCode;
203 * Get the plain message from the cache variable for the given message id
205 * @param $messageId The message id we shall find in the cache variable
206 * @return $messageText The plain message text
208 public function getMessage ($messageId) {
209 // Default is missing message text
210 $messageText = sprintf('!%s!',
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);