+++ /dev/null
-<?php
-// Own namespace
-namespace CoreFramework\Localization;
-
-// Import framework stuff
-use CoreFramework\Configuration\FrameworkConfiguration;
-use CoreFramework\Object\BaseFrameworkSystem;
-use CoreFramework\Registry\Registerable;
-use CoreFramework\Registry\Registry;
-
-/**
- * The language sub-system for handling language strings being used in the
- * application and whole framework
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
- * @license GNU GPL 3.0 or any newer version
- * @link http://www.shipsimu.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, Registerable {
- /**
- * The full-qualified base path for the language include files
- */
- private $languageBasePath = '';
-
- /**
- * The 2-char language code
- */
- private $langCode = 'xx'; // This will later be overwritten!
-
- /**
- * The array-object for all language strings
- */
- private $langStrings = NULL;
-
- /**
- * An instance of this class
- */
- private static $selfInstance = NULL;
-
- /**
- * Protected constructor
- *
- * @return void
- */
- protected function __construct () {
- // Call parent constructor
- parent::__construct(__CLASS__);
- }
-
- /**
- * Creates an instance of the class LanguageSystem and prepares it for usage
- *
- * @param $languageBasePath The local base path for all language strings or emty for auto-detection
- * @return $langInstance An instance of LanguageSystem
- * @throws LanguagePathIsEmptyException If the provided $languageBasePath is empty
- * @throws InvalidLanguagePathStringException If $languageBasePath is no string
- * @throws LanguagePathIsNoDirectoryException If $languageBasePath is no
- * directory or not found
- * @throws LanguagePathReadProtectedException If $languageBasePath is
- * read-protected
- */
- public static final function createLanguageSystem ($languageBasePath = '') {
- // Get a new instance
- $langInstance = new LanguageSystem();
-
- // Is the base path set?
- if (empty($languageBasePath)) {
- // No, then attempt "auto-dection":
- // 1) Get application
- $applicationInstance = Registry::getRegistry()->getInstance('app');
-
- // 2) Try to build it
- $languageBasePath = sprintf('%sapplication/%s/language/',
- $langInstance->getConfigInstance()->getConfigEntry('base_path'),
- // Don't allow any underscores/dashes in application names
- str_replace(array('_', '-'), array('', ''), $applicationInstance->getAppShortName())
- );
- } // END - if
-
- // Is the base path valid?
- if (empty($languageBasePath)) {
- // Language path is empty
- throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
- } elseif (!is_string($languageBasePath)) {
- // Is not a string
- throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
- } elseif (!is_dir($languageBasePath)) {
- // Is not a path
- throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
- } elseif (!is_readable($languageBasePath)) {
- // Is not readable
- throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
- }
-
- // Set the base path
- $langInstance->setLanguageBasePath($languageBasePath);
-
- // Initialize the variable stack
- $langInstance->initLanguageStrings();
-
- // Set language code from default config
- $langInstance->setLanguageCode(FrameworkConfiguration::getSelfInstance()->getConfigEntry('default_lang'));
-
- // Remember this instance
- self::$selfInstance = $langInstance;
-
- // Return the prepared instance
- return $langInstance;
- }
-
- /**
- * Singleton getter for this instance
- *
- * @return $selfInstance An instance of this class
- */
- public static final function getSelfInstance () {
- return self::$selfInstance;
- }
-
- /**
- * Setter for base path
- *
- * @param $languageBasePath The relative base path for all language files
- * @return void
- */
- protected final function setLanguageBasePath ($languageBasePath) {
- // And set it
- $this->languageBasePath = (string) $languageBasePath;
- }
-
- /**
- * Setter for language code
- *
- * @param $langCode The language code for the current application
- * @return void
- */
- protected final function setLanguageCode ($langCode) {
- // Cast it
- $langCode = (string) $langCode;
-
- // And set it (only 2 chars)
- $this->langCode = substr($langCode, 0, 2);
- }
-
- /**
- * Initialize the array-object for all later language strings
- *
- * @return void
- */
- public function initLanguageStrings () {
- /*
- * Locale category constants are usually predefined, but may not be
- * on some systems such as Win32.
- *
- * Origin: StatusNet's lib/language.php
- */
- $localeCategories = array(
- 'LC_CTYPE',
- 'LC_NUMERIC',
- 'LC_TIME',
- 'LC_COLLATE',
- 'LC_MONETARY',
- 'LC_MESSAGES',
- 'LC_ALL'
- );
-
- // Set all, if not defined
- foreach ($localeCategories as $key => $name) {
- // Is it set?
- if (!defined($name)) {
- // No, then set it
- define($name, $key);
- } // END - if
- } // END - foreach
-
- $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
- }
-
- /**
- * Getter for language code
- *
- * @return $langCode The language code for the current application
- */
- public final function getLanguageCode () {
- return $this->langCode;
- }
-
- /**
- * Get the plain message from the cache variable for the given message id
- *
- * @param $messageId The message id we shall find in the cache variable
- * @return $messageText The plain message text
- */
- public function getMessage ($messageId) {
- // Default is missing message text
- $messageText = sprintf('!%s!',
- $messageId
- );
-
- // Try to look it up in the cache variable
- if ($this->langStrings->offsetExists($messageId)) {
- // Return the message string
- $messageText = $this->langStrings->offsetGet($messageId);
- } // END - if
-
- // Return the text
- return $messageText;
- }
-
-}