3 namespace CoreFramework\Localization;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
9 * The language sub-system for handling language strings being used in the
10 * application and whole framework
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
33 * The full-qualified base path for the language include files
35 private $languageBasePath = '';
38 * The 2-char language code
40 private $langCode = 'xx'; // This will later be overwritten!
43 * The array-object for all language strings
45 private $langStrings = NULL;
48 * An instance of this class
50 private static $selfInstance = NULL;
53 * Protected constructor
57 protected function __construct () {
58 // Call parent constructor
59 parent::__construct(__CLASS__);
63 * Creates an instance of the class LanguageSystem and prepares it for usage
65 * @param $languageBasePath The local base path for all language strings or emty for auto-detection
66 * @return $langInstance An instance of LanguageSystem
67 * @throws LanguagePathIsEmptyException If the provided $languageBasePath is empty
68 * @throws InvalidLanguagePathStringException If $languageBasePath is no string
69 * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
70 * directory or not found
71 * @throws LanguagePathReadProtectedException If $languageBasePath is
74 public static final function createLanguageSystem ($languageBasePath = '') {
76 $langInstance = new LanguageSystem();
78 // Is the base path set?
79 if (empty($languageBasePath)) {
80 // No, then attempt "auto-dection":
82 $applicationInstance = Registry::getRegistry()->getInstance('app');
85 $languageBasePath = sprintf('%sapplication/%s/language/',
86 $langInstance->getConfigInstance()->getConfigEntry('base_path'),
87 // Don't allow any underscores/dashes in application names
88 str_replace(array('_', '-'), array('', ''), $applicationInstance->getAppShortName())
92 // Is the base path valid?
93 if (empty($languageBasePath)) {
94 // Language path is empty
95 throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96 } elseif (!is_string($languageBasePath)) {
98 throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
99 } elseif (!is_dir($languageBasePath)) {
101 throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
102 } elseif (!is_readable($languageBasePath)) {
104 throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
108 $langInstance->setLanguageBasePath($languageBasePath);
110 // Initialize the variable stack
111 $langInstance->initLanguageStrings();
113 // Set language code from default config
114 $langInstance->setLanguageCode(FrameworkConfiguration::getSelfInstance()->getConfigEntry('default_lang'));
116 // Remember this instance
117 self::$selfInstance = $langInstance;
119 // Return the prepared instance
120 return $langInstance;
124 * Singleton getter for this instance
126 * @return $selfInstance An instance of this class
128 public static final function getSelfInstance () {
129 return self::$selfInstance;
133 * Setter for base path
135 * @param $languageBasePath The relative base path for all language files
138 protected final function setLanguageBasePath ($languageBasePath) {
140 $this->languageBasePath = (string) $languageBasePath;
144 * Setter for language code
146 * @param $langCode The language code for the current application
149 protected final function setLanguageCode ($langCode) {
151 $langCode = (string) $langCode;
153 // And set it (only 2 chars)
154 $this->langCode = substr($langCode, 0, 2);
158 * Initialize the array-object for all later language strings
162 public function initLanguageStrings () {
164 * Locale category constants are usually predefined, but may not be
165 * on some systems such as Win32.
167 * Origin: StatusNet's lib/language.php
169 $localeCategories = array(
179 // Set all, if not defined
180 foreach ($localeCategories as $key => $name) {
182 if (!defined($name)) {
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 ($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);