Language base path is now optional.
[core.git] / inc / classes / main / language / class_LanguageSystem.php
index fe66586aadaae8a5240595705bc910090b8259c4..ec094c9eef6ca37bc2438fd481fa759cc006183e 100644 (file)
@@ -3,11 +3,11 @@
  * The language sub-system for handling language strings being used in the
  * application and whole framework
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007 - 2009 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @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
@@ -26,7 +26,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
        /**
         * The full-qualified base path for the language include files
         */
-       private $basePath = '';
+       private $languageBasePath = '';
 
        /**
         * The 2-char language code
@@ -36,12 +36,12 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
        /**
         * The array-object for all language strings
         */
-       private $langStrings = null;
+       private $langStrings = NULL;
 
        /**
         * An instance of this class
         */
-       private static $thisInstance = null;
+       private static $selfInstance = NULL;
 
        /**
         * Protected constructor
@@ -51,54 +51,63 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
        }
 
        /**
         * Creates an instance of the class LanguageSystem and prepares it for usage
         *
-        * @param       $basePath               The local base path for all language strings
+        * @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 $basePath is empty
-        * @throws      InvalidLanguagePathStringException      If $basePath is no string
-        * @throws      LanguagePathIsNoDirectoryException      If $basePath is no
+        * @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 $basePath is
+        * @throws      LanguagePathReadProtectedException      If $languageBasePath is
         *                                                                              read-protected
         */
-       public final static function createLanguageSystem ($basePath) {
+       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'),
+                               $applicationInstance->getAppShortName()
+                       );
+               } // END - if
+
                // Is the base path valid?
-               if (empty($basePath)) {
+               if (empty($languageBasePath)) {
                        // Language path is empty
                        throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!is_string($basePath)) {
+               } elseif (!is_string($languageBasePath)) {
                        // Is not a string
-                       throw new InvalidLanguagePathStringException(array($langInstance, $basePath), self::EXCEPTION_INVALID_STRING);
-               } elseif (!is_dir($basePath)) {
+                       throw new InvalidLanguagePathStringException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_STRING);
+               } elseif (!is_dir($languageBasePath)) {
                        // Is not a path
-                       throw new LanguagePathIsNoDirectoryException(array($langInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
-               } elseif (!is_readable($basePath)) {
+                       throw new LanguagePathIsNoDirectoryException(array($langInstance, $languageBasePath), self::EXCEPTION_INVALID_PATH_NAME);
+               } elseif (!is_readable($languageBasePath)) {
                        // Is not readable
-                       throw new LanguagePathReadProtectedException(array($langInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
+                       throw new LanguagePathReadProtectedException(array($langInstance, $languageBasePath), self::EXCEPTION_READ_PROTECED_PATH);
                }
 
                // Set the base path
-               $langInstance->setBasePath($basePath);
+               $langInstance->setLanguageBasePath($languageBasePath);
 
                // Initialize the variable stack
                $langInstance->initLanguageStrings();
 
                // Set language code from default config
-               $langInstance->setLanguageCode(FrameworkConfiguration::getInstance()->readConfig('default_lang'));
+               $langInstance->setLanguageCode(FrameworkConfiguration::getSelfInstance()->getConfigEntry('default_lang'));
 
                // Remember this instance
-               self::$thisInstance = $langInstance;
+               self::$selfInstance = $langInstance;
 
                // Return the prepared instance
                return $langInstance;
@@ -107,27 +116,27 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
        /**
         * Singleton getter for this instance
         *
-        * @return      $thisInstance           An instance of this class
+        * @return      $selfInstance   An instance of this class
         */
-       public final static function getInstance () {
-               return self::$thisInstance;
+       public static final function getSelfInstance () {
+               return self::$selfInstance;
        }
 
        /**
         * Setter for base path
         *
-        * @param               $basePath               The local base path for all templates
+        * @param       $languageBasePath       The relative base path for all language files
         * @return      void
         */
-       protected final function setBasePath ($basePath) {
+       protected final function setLanguageBasePath ($languageBasePath) {
                // And set it
-               $this->basePath = (string) $basePath;
+               $this->languageBasePath = (string) $languageBasePath;
        }
 
        /**
         * Setter for language code
         *
-        * @param               $langCode               The language code for the current application
+        * @param       $langCode       The language code for the current application
         * @return      void
         */
        protected final function setLanguageCode ($langCode) {
@@ -144,13 +153,13 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
         * @return      void
         */
        public function initLanguageStrings () {
-               $this->langStrings = new FrameworkArrayObject("FakedLanguageStrings");
+               $this->langStrings = new FrameworkArrayObject('FakedLanguageStrings');
        }
 
        /**
         * Getter for language code
         *
-        * @return      $langCode               The language code for the current application
+        * @return      $langCode       The language code for the current application
         */
        public final function getLanguageCode () {
                return $this->langCode;
@@ -164,7 +173,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
         */
        public function getMessage ($messageId) {
                // Default is missing message text
-               $messageText = sprintf("!%s!",
+               $messageText = sprintf('!%s!',
                        $messageId
                );
 
@@ -172,7 +181,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage,
                if ($this->langStrings->offsetExists($messageId)) {
                        // Return the message string
                        $messageText = $this->langStrings->offsetGet($messageId);
-               }
+               } // END - if
 
                // Return the text
                return $messageText;