3 namespace Org\Mxchange\CoreFramework\Localization;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\ObjectArray\FrameworkArrayObject;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
14 use \InvalidArgumentException;
17 * The language sub-system for handling language strings being used in the
18 * application and whole framework
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
41 * The full-qualified base path for the language include files
43 private $languageBasePath = '';
46 * The 2-char language code
48 private $langCode = 'xx'; // This will later be overwritten!
51 * The array-object for all language strings
53 private $langStrings = NULL;
56 * An instance of this class
58 private static $selfInstance = NULL;
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
71 * Creates an instance of the class LanguageSystem and prepares it for usage
73 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
74 * @return $langInstance An instance of LanguageSystem
75 * @throws InvalidArgumentException If languageBasePath remains empty (@TODO Get rid of that old-lost code)
76 * @throws InvalidLanguagePathStringException If $languageBasePath is no string
77 * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
78 * directory or not found
79 * @throws LanguagePathReadProtectedException If $languageBasePath is
82 public static final function createLanguageSystem ($languageBasePath = '') {
84 $langInstance = new LanguageSystem();
86 // Is the base path set?
87 if (empty($languageBasePath)) {
88 // No, then attempt "auto-dection":
90 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
93 $languageBasePath = sprintf('%s%s/language/',
94 $langInstance->getConfigInstance()->getConfigEntry('application_base_path'),
95 $applicationInstance->getAppShortName()
99 // Is the base path valid?
100 if (empty($languageBasePath)) {
101 // Language path is empty
102 throw new InvalidArgumentException('languageBasePath is still empty');
103 } elseif (!is_string($languageBasePath)) {
105 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
106 } elseif (!is_dir($languageBasePath)) {
108 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
109 } elseif (!is_readable($languageBasePath)) {
111 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
115 $langInstance->setLanguageBasePath($languageBasePath);
117 // Initialize the variable stack
118 $langInstance->initLanguageStrings();
120 // Set language code from default config
121 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
123 // Remember this instance
124 self::$selfInstance = $langInstance;
126 // Return the prepared instance
127 return $langInstance;
131 * Singleton getter for this instance
133 * @return $selfInstance An instance of this class
135 public static final function getSelfInstance () {
136 return self::$selfInstance;
140 * Setter for base path
142 * @param $languageBasePath The relative base path for all language files
145 protected final function setLanguageBasePath ($languageBasePath) {
147 $this->languageBasePath = (string) $languageBasePath;
151 * Setter for language code
153 * @param $langCode The language code for the current application
156 protected final function setLanguageCode ($langCode) {
158 $langCode = (string) $langCode;
160 // And set it (only 2 chars)
161 $this->langCode = substr($langCode, 0, 2);
165 * Initialize the array-object for all later language strings
169 public function initLanguageStrings () {
171 * Locale category constants are usually predefined, but may not be
172 * on some systems such as Win32.
174 * Origin: StatusNet's lib/language.php
176 $localeCategories = array(
186 // Set all, if not defined
187 foreach ($localeCategories as $key => $name) {
189 if (!defined($name)) {
195 // Init language strings array
196 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
200 * Getter for language code
202 * @return $langCode The language code for the current application
204 public final function getLanguageCode () {
205 return $this->langCode;
209 * Get the plain message from the cache variable for the given message id
211 * @param $messageId The message id we shall find in the cache variable
212 * @return $messageText The plain message text
214 public function getMessage ($messageId) {
215 // Default is missing message text
216 $messageText = sprintf('!%s!',
220 // Try to look it up in the cache variable
221 if ($this->langStrings->offsetExists($messageId)) {
222 // Return the message string
223 $messageText = $this->langStrings->offsetGet($messageId);