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\Localization\ManageableLanguage;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\ObjectArray\FrameworkArrayObject;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
15 use \InvalidArgumentException;
18 * The language sub-system for handling language strings being used in the
19 * application and whole framework
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
42 * The full-qualified base path for the language include files
44 private $languageBasePath = '';
47 * The 2-char language code
49 private $langCode = 'xx'; // This will later be overwritten!
52 * The array-object for all language strings
54 private $langStrings = NULL;
57 * An instance of this class
59 private static $selfInstance = NULL;
62 * Protected constructor
66 private function __construct () {
67 // Call parent constructor
68 parent::__construct(__CLASS__);
72 * Creates an instance of the class LanguageSystem and prepares it for usage
74 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
75 * @return $langInstance An instance of LanguageSystem
76 * @throws InvalidArgumentException If languageBasePath remains empty (@TODO Get rid of that old-lost code)
77 * @throws InvalidArgumentException If $languageBasePath is no string
78 * @throws InvalidArgumentException If $languageBasePath is no
79 * directory or not found
80 * @throws InvalidArgumentException If $languageBasePath is
83 public static final function createLanguageSystem (string $languageBasePath = '') {
85 $langInstance = new LanguageSystem();
87 // Is the base path set?
88 if (empty($languageBasePath)) {
89 // No, then attempt "auto-dection":
91 $applicationInstance = ApplicationHelper::getSelfInstance();
94 $languageBasePath = sprintf('%s%s/language/',
95 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path'),
96 $applicationInstance->getAppShortName()
100 // Is the base path valid?
101 if (empty($languageBasePath)) {
102 // Language path is empty
103 throw new InvalidArgumentException('languageBasePath is still empty');
104 } elseif (!is_dir($languageBasePath)) {
106 throw new InvalidArgumentException(sprintf('languageBasePath=%s not found', $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107 } elseif (!is_readable($languageBasePath)) {
109 throw new InvalidArgumentException(sprintf('Cannot read from languageBasePath=%s', $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 (string $languageBasePath) {
145 $this->languageBasePath = $languageBasePath;
149 * Setter for language code
151 * @param $langCode The language code for the current application
154 protected final function setLanguageCode (string $langCode) {
155 // And set it (only 2 chars)
156 $this->langCode = substr($langCode, 0, 2);
160 * Initialize the array-object for all later language strings
164 public function initLanguageStrings () {
166 * Locale category constants are usually predefined, but may not be
167 * on some systems such as Win32.
169 * Origin: StatusNet's lib/language.php
179 ] as $key => $name) {
181 if (!defined($name)) {
187 // Init language strings array
188 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
192 * Getter for language code
194 * @return $langCode The language code for the current application
196 public final function getLanguageCode () {
197 return $this->langCode;
201 * Get the plain message from the cache variable for the given message id
203 * @param $messageId The message id we shall find in the cache variable
204 * @return $messageText The plain message text
206 public function getMessage (string $messageId) {
207 // Default is missing message text
208 $messageText = sprintf('!%s!',
212 // Try to look it up in the cache variable
213 if ($this->langStrings->offsetExists($messageId)) {
214 // Return the message string
215 $messageText = $this->langStrings->offsetGet($messageId);