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\GenericRegistry;
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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
39 * The full-qualified base path for the language include files
41 private $languageBasePath = '';
44 * The 2-char language code
46 private $langCode = 'xx'; // This will later be overwritten!
49 * The array-object for all language strings
51 private $langStrings = NULL;
54 * An instance of this class
56 private static $selfInstance = NULL;
59 * Protected constructor
63 protected function __construct () {
64 // Call parent constructor
65 parent::__construct(__CLASS__);
69 * Creates an instance of the class LanguageSystem and prepares it for usage
71 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
72 * @return $langInstance An instance of LanguageSystem
73 * @throws InvalidArgumentException If languageBasePath remains empty (@TODO Get rid of that old-lost code)
74 * @throws InvalidLanguagePathStringException If $languageBasePath is no string
75 * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
76 * directory or not found
77 * @throws LanguagePathReadProtectedException If $languageBasePath is
80 public static final function createLanguageSystem ($languageBasePath = '') {
82 $langInstance = new LanguageSystem();
84 // Is the base path set?
85 if (empty($languageBasePath)) {
86 // No, then attempt "auto-dection":
88 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
91 $languageBasePath = sprintf('%s%s/language/',
92 $langInstance->getConfigInstance()->getConfigEntry('application_base_path'),
93 $applicationInstance->getAppShortName()
97 // Is the base path valid?
98 if (empty($languageBasePath)) {
99 // Language path is empty
100 throw new InvalidArgumentException('languageBasePath is still empty');
101 } elseif (!is_string($languageBasePath)) {
103 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
104 } elseif (!is_dir($languageBasePath)) {
106 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107 } elseif (!is_readable($languageBasePath)) {
109 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
113 $langInstance->setLanguageBasePath($languageBasePath);
115 // Initialize the variable stack
116 $langInstance->initLanguageStrings();
118 // Set language code from default config
119 $langInstance->setLanguageCode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_lang'));
121 // Remember this instance
122 self::$selfInstance = $langInstance;
124 // Return the prepared instance
125 return $langInstance;
129 * Singleton getter for this instance
131 * @return $selfInstance An instance of this class
133 public static final function getSelfInstance () {
134 return self::$selfInstance;
138 * Setter for base path
140 * @param $languageBasePath The relative base path for all language files
143 protected final function setLanguageBasePath ($languageBasePath) {
145 $this->languageBasePath = (string) $languageBasePath;
149 * Setter for language code
151 * @param $langCode The language code for the current application
154 protected final function setLanguageCode ($langCode) {
156 $langCode = (string) $langCode;
158 // And set it (only 2 chars)
159 $this->langCode = substr($langCode, 0, 2);
163 * Initialize the array-object for all later language strings
167 public function initLanguageStrings () {
169 * Locale category constants are usually predefined, but may not be
170 * on some systems such as Win32.
172 * Origin: StatusNet's lib/language.php
174 $localeCategories = array(
184 // Set all, if not defined
185 foreach ($localeCategories as $key => $name) {
187 if (!defined($name)) {
193 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
197 * Getter for language code
199 * @return $langCode The language code for the current application
201 public final function getLanguageCode () {
202 return $this->langCode;
206 * Get the plain message from the cache variable for the given message id
208 * @param $messageId The message id we shall find in the cache variable
209 * @return $messageText The plain message text
211 public function getMessage ($messageId) {
212 // Default is missing message text
213 $messageText = sprintf('!%s!',
217 // Try to look it up in the cache variable
218 if ($this->langStrings->offsetExists($messageId)) {
219 // Return the message string
220 $messageText = $this->langStrings->offsetGet($messageId);