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\ObjectArray\FrameworkArrayObject;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use \InvalidArgumentException;
16 * The language sub-system for handling language strings being used in the
17 * application and whole framework
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
40 * The full-qualified base path for the language include files
42 private $languageBasePath = '';
45 * The 2-char language code
47 private $langCode = 'xx'; // This will later be overwritten!
50 * The array-object for all language strings
52 private $langStrings = NULL;
55 * An instance of this class
57 private static $selfInstance = NULL;
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates an instance of the class LanguageSystem and prepares it for usage
72 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
73 * @return $langInstance An instance of LanguageSystem
74 * @throws InvalidArgumentException If languageBasePath remains empty (@TODO Get rid of that old-lost code)
75 * @throws InvalidLanguagePathStringException If $languageBasePath is no string
76 * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
77 * directory or not found
78 * @throws LanguagePathReadProtectedException If $languageBasePath is
81 public static final function createLanguageSystem ($languageBasePath = '') {
83 $langInstance = new LanguageSystem();
85 // Is the base path set?
86 if (empty($languageBasePath)) {
87 // No, then attempt "auto-dection":
89 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
92 $languageBasePath = sprintf('%s%s/language/',
93 $langInstance->getConfigInstance()->getConfigEntry('application_base_path'),
94 $applicationInstance->getAppShortName()
98 // Is the base path valid?
99 if (empty($languageBasePath)) {
100 // Language path is empty
101 throw new InvalidArgumentException('languageBasePath is still empty');
102 } elseif (!is_string($languageBasePath)) {
104 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
105 } elseif (!is_dir($languageBasePath)) {
107 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
108 } elseif (!is_readable($languageBasePath)) {
110 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
114 $langInstance->setLanguageBasePath($languageBasePath);
116 // Initialize the variable stack
117 $langInstance->initLanguageStrings();
119 // Set language code from default config
120 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
122 // Remember this instance
123 self::$selfInstance = $langInstance;
125 // Return the prepared instance
126 return $langInstance;
130 * Singleton getter for this instance
132 * @return $selfInstance An instance of this class
134 public static final function getSelfInstance () {
135 return self::$selfInstance;
139 * Setter for base path
141 * @param $languageBasePath The relative base path for all language files
144 protected final function setLanguageBasePath ($languageBasePath) {
146 $this->languageBasePath = (string) $languageBasePath;
150 * Setter for language code
152 * @param $langCode The language code for the current application
155 protected final function setLanguageCode ($langCode) {
157 $langCode = (string) $langCode;
159 // And set it (only 2 chars)
160 $this->langCode = substr($langCode, 0, 2);
164 * Initialize the array-object for all later language strings
168 public function initLanguageStrings () {
170 * Locale category constants are usually predefined, but may not be
171 * on some systems such as Win32.
173 * Origin: StatusNet's lib/language.php
175 $localeCategories = array(
185 // Set all, if not defined
186 foreach ($localeCategories as $key => $name) {
188 if (!defined($name)) {
194 // Init language strings array
195 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
199 * Getter for language code
201 * @return $langCode The language code for the current application
203 public final function getLanguageCode () {
204 return $this->langCode;
208 * Get the plain message from the cache variable for the given message id
210 * @param $messageId The message id we shall find in the cache variable
211 * @return $messageText The plain message text
213 public function getMessage ($messageId) {
214 // Default is missing message text
215 $messageText = sprintf('!%s!',
219 // Try to look it up in the cache variable
220 if ($this->langStrings->offsetExists($messageId)) {
221 // Return the message string
222 $messageText = $this->langStrings->offsetGet($messageId);