3 namespace CoreFramework\Localization;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registerable;
9 use CoreFramework\Registry\Registry;
12 * The language sub-system for handling language strings being used in the
13 * application and whole framework
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
36 * The full-qualified base path for the language include files
38 private $languageBasePath = '';
41 * The 2-char language code
43 private $langCode = 'xx'; // This will later be overwritten!
46 * The array-object for all language strings
48 private $langStrings = NULL;
51 * An instance of this class
53 private static $selfInstance = NULL;
56 * Protected constructor
60 protected function __construct () {
61 // Call parent constructor
62 parent::__construct(__CLASS__);
66 * Creates an instance of the class LanguageSystem and prepares it for usage
68 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
69 * @return $langInstance An instance of LanguageSystem
70 * @throws LanguagePathIsEmptyException If the provided $languageBasePath is empty
71 * @throws InvalidLanguagePathStringException If $languageBasePath is no string
72 * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
73 * directory or not found
74 * @throws LanguagePathReadProtectedException If $languageBasePath is
77 public static final function createLanguageSystem ($languageBasePath = '') {
79 $langInstance = new LanguageSystem();
81 // Is the base path set?
82 if (empty($languageBasePath)) {
83 // No, then attempt "auto-dection":
85 $applicationInstance = Registry::getRegistry()->getInstance('app');
88 $languageBasePath = sprintf('%sapplication/%s/language/',
89 $langInstance->getConfigInstance()->getConfigEntry('base_path'),
90 // Don't allow any underscores/dashes in application names
91 str_replace(array('_', '-'), array('', ''), $applicationInstance->getAppShortName())
95 // Is the base path valid?
96 if (empty($languageBasePath)) {
97 // Language path is empty
98 throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
99 } elseif (!is_string($languageBasePath)) {
101 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
102 } elseif (!is_dir($languageBasePath)) {
104 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
105 } elseif (!is_readable($languageBasePath)) {
107 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
111 $langInstance->setLanguageBasePath($languageBasePath);
113 // Initialize the variable stack
114 $langInstance->initLanguageStrings();
116 // Set language code from default config
117 $langInstance->setLanguageCode(FrameworkConfiguration::getSelfInstance()->getConfigEntry('default_lang'));
119 // Remember this instance
120 self::$selfInstance = $langInstance;
122 // Return the prepared instance
123 return $langInstance;
127 * Singleton getter for this instance
129 * @return $selfInstance An instance of this class
131 public static final function getSelfInstance () {
132 return self::$selfInstance;
136 * Setter for base path
138 * @param $languageBasePath The relative base path for all language files
141 protected final function setLanguageBasePath ($languageBasePath) {
143 $this->languageBasePath = (string) $languageBasePath;
147 * Setter for language code
149 * @param $langCode The language code for the current application
152 protected final function setLanguageCode ($langCode) {
154 $langCode = (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
172 $localeCategories = array(
182 // Set all, if not defined
183 foreach ($localeCategories as $key => $name) {
185 if (!defined($name)) {
191 $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
195 * Getter for language code
197 * @return $langCode The language code for the current application
199 public final function getLanguageCode () {
200 return $this->langCode;
204 * Get the plain message from the cache variable for the given message id
206 * @param $messageId The message id we shall find in the cache variable
207 * @return $messageText The plain message text
209 public function getMessage ($messageId) {
210 // Default is missing message text
211 $messageText = sprintf('!%s!',
215 // Try to look it up in the cache variable
216 if ($this->langStrings->offsetExists($messageId)) {
217 // Return the message string
218 $messageText = $this->langStrings->offsetGet($messageId);