3 namespace Org\Mxchange\CoreFramework\Localization;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\ObjectArray\FrameworkArrayObject;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
16 use \InvalidArgumentException;
19 * The language sub-system for handling language strings being used in the
20 * application and whole framework
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
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 private 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 InvalidArgumentException If $languageBasePath is no string
79 * @throws InvalidArgumentException If $languageBasePath is no
80 * directory or not found
81 * @throws InvalidArgumentException If $languageBasePath is
84 public static final function createLanguageSystem (string $languageBasePath = '') {
86 $langInstance = new LanguageSystem();
88 // Is the base path set?
89 if (empty($languageBasePath)) {
90 // No, then attempt "auto-dection":
92 $applicationInstance = ApplicationHelper::getSelfInstance();
95 $languageBasePath = sprintf('%s%s/language/',
96 FrameworkBootstrap::getConfigurationInstance()->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', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
105 } elseif (!is_dir($languageBasePath)) {
107 throw new InvalidArgumentException(sprintf('languageBasePath=%s not found', $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
108 } elseif (!is_readable($languageBasePath)) {
110 throw new InvalidArgumentException(sprintf('Cannot read from languageBasePath=%s', $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 (string $languageBasePath) {
146 $this->languageBasePath = $languageBasePath;
150 * Setter for language code
152 * @param $langCode The language code for the current application
155 protected final function setLanguageCode (string $langCode) {
156 // And set it (only 2 chars)
157 $this->langCode = substr($langCode, 0, 2);
161 * Initialize the array-object for all later language strings
165 public function initLanguageStrings () {
167 * Locale category constants are usually predefined, but may not be
168 * on some systems such as Win32.
170 * Origin: StatusNet's lib/language.php
180 ] as $key => $name) {
182 if (!defined($name)) {
188 // Init language strings array
189 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
193 * Getter for language code
195 * @return $langCode The language code for the current application
197 public final function getLanguageCode () {
198 return $this->langCode;
202 * Get the plain message from the cache variable for the given message id
204 * @param $messageId The message id we shall find in the cache variable
205 * @return $messageText The plain message text
207 public function getMessage (string $messageId) {
208 // Default is missing message text
209 $messageText = sprintf('!%s!',
213 // Try to look it up in the cache variable
214 if ($this->langStrings->offsetExists($messageId)) {
215 // Return the message string
216 $messageText = $this->langStrings->offsetGet($messageId);