3 namespace Org\Mxchange\CoreFramework\Localization;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Registry\Registry;
12 use \InvalidArgumentException;
15 * The language sub-system for handling language strings being used in the
16 * application and whole framework
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 <<<<<<< HEAD:framework/main/classes/language/class_LanguageSystem.php
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
24 >>>>>>> Some updates::inc/main/classes/language/class_LanguageSystem.php
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
43 * The full-qualified base path for the language include files
45 private $languageBasePath = '';
48 * The 2-char language code
50 private $langCode = 'xx'; // This will later be overwritten!
53 * The array-object for all language strings
55 private $langStrings = NULL;
58 * An instance of this class
60 private static $selfInstance = NULL;
63 * Protected constructor
67 protected function __construct () {
68 // Call parent constructor
69 parent::__construct(__CLASS__);
73 * Creates an instance of the class LanguageSystem and prepares it for usage
75 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
76 * @return $langInstance An instance of LanguageSystem
77 * @throws InvalidArgumentException If languageBasePath remains empty (@TODO Get rid of that old-lost code)
78 * @throws InvalidLanguagePathStringException If $languageBasePath is no string
79 * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
80 * directory or not found
81 * @throws LanguagePathReadProtectedException If $languageBasePath is
84 public static final function createLanguageSystem ($languageBasePath = '') {
86 $langInstance = new LanguageSystem();
88 // Is the base path set?
89 if (empty($languageBasePath)) {
90 // No, then attempt "auto-dection":
92 $applicationInstance = Registry::getRegistry()->getInstance('app');
95 $languageBasePath = sprintf('%s%s/language/',
96 $langInstance->getConfigInstance()->getConfigEntry('application_base_path'),
97 $applicationInstance->getAppShortName()
101 // Is the base path valid?
102 if (empty($languageBasePath)) {
103 // Language path is empty
104 throw new InvalidArgumentException('languageBasePath is still empty');
105 } elseif (!is_string($languageBasePath)) {
107 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
108 } elseif (!is_dir($languageBasePath)) {
110 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
111 } elseif (!is_readable($languageBasePath)) {
113 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
117 $langInstance->setLanguageBasePath($languageBasePath);
119 // Initialize the variable stack
120 $langInstance->initLanguageStrings();
122 // Set language code from default config
123 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
125 // Remember this instance
126 self::$selfInstance = $langInstance;
128 // Return the prepared instance
129 return $langInstance;
133 * Singleton getter for this instance
135 * @return $selfInstance An instance of this class
137 public static final function getSelfInstance () {
138 return self::$selfInstance;
142 * Setter for base path
144 * @param $languageBasePath The relative base path for all language files
147 protected final function setLanguageBasePath ($languageBasePath) {
149 $this->languageBasePath = (string) $languageBasePath;
153 * Setter for language code
155 * @param $langCode The language code for the current application
158 protected final function setLanguageCode ($langCode) {
160 $langCode = (string) $langCode;
162 // And set it (only 2 chars)
163 $this->langCode = substr($langCode, 0, 2);
167 * Initialize the array-object for all later language strings
171 public function initLanguageStrings () {
173 * Locale category constants are usually predefined, but may not be
174 * on some systems such as Win32.
176 * Origin: StatusNet's lib/language.php
178 $localeCategories = array(
188 // Set all, if not defined
189 foreach ($localeCategories as $key => $name) {
191 if (!defined($name)) {
197 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
201 * Getter for language code
203 * @return $langCode The language code for the current application
205 public final function getLanguageCode () {
206 return $this->langCode;
210 * Get the plain message from the cache variable for the given message id
212 * @param $messageId The message id we shall find in the cache variable
213 * @return $messageText The plain message text
215 public function getMessage ($messageId) {
216 // Default is missing message text
217 $messageText = sprintf('!%s!',
221 // Try to look it up in the cache variable
222 if ($this->langStrings->offsetExists($messageId)) {
223 // Return the message string
224 $messageText = $this->langStrings->offsetGet($messageId);