Project

General

Profile

1
<?php
2

    
3
/**
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

    
20
/**
21
 * TranslationTable.php
22
 *
23
 * @category     Core
24
 * @package      Core_Translation
25
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
26
 * @author       Werner v.d.Decken <wkl@isteam.de>
27
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
28
 * @version      0.0.1
29
 * @revision     $Revision: 2094 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/TranslateAdaptorWbOldStyle.php $
31
 * @lastmodified $Date: 2014-01-30 13:45:33 +0100 (Thu, 30 Jan 2014) $
32
 * @since        File available since 12.01.2013
33
 * @description  Loads translation table from old languagefiles before WB-2.9.0.
34
 *               Can handle Languagecodes like 'de_DE_BAY' (2ALPHA_2ALPHA_2-4ALNUM)
35
 */
36
class TranslateAdaptorWbOldStyle implements TranslateAdaptorInterface {
37

    
38
	protected $sAddon     = '';
39
	protected $sFilePath  = '';
40
/**
41
 * Constructor
42
 * @param string descriptor of the Addon (i.e. '' || 'modules\myAddon'
43
 */
44
	public function __construct($sAddon = '')
45
	{
46
		$this->sAddon = $sAddon;
47
	}
48
/**
49
 * Load languagefile
50
 * @param string $sLangCode
51
 * @return array|bool an array of translations or FALSE on error
52
 */
53
	public function loadLanguage($sLangCode)
54
	{
55
        $aTranslations = array();
56
		$sLanguagePath = $this->_getAddonPath();
57
        if (is_readable($sLanguagePath)) {
58
            $sLangFile = strtolower($sLangCode.'.php');
59
            if( ($aDirContent = scandir($sLanguagePath)) !== false) {
60
                foreach($aDirContent as $sFile) {
61
                    if($sLangFile === strtolower($sFile)) {
62
                        $sLangFile = $sLanguagePath.$sFile;
63
                        if(is_readable($sLangFile)) {
64
                            $aTmp = $this->_importArrays($sLangFile);
65
                            $aTranslations = array_merge($aTranslations, $aTmp);
66
                            break;
67
                        }
68
                    }
69
                }
70
            }
71
        }
72
		return (sizeof($aTranslations) > 0 ? $aTranslations : false);
73
	}
74
/**
75
 * Find first existing language
76
 * @return string Code of first found basic language
77
 */
78
	public function findFirstLanguage()
79
	{
80
		$sLanguagePath = $this->_getAddonPath();
81
	// search for first available and readable language file
82
		$sRetval = '';
83
		if(is_readable($sLanguagePath)) {
84
			$iterator = new DirectoryIterator($sLanguagePath);
85
			foreach ($iterator as $oFileInfo) {
86
				$sPattern = '/^[a-z]{2,3}\.php/siU';
87
				if(!preg_match($sPattern, $oFileInfo->getBasename())) { continue; }
88
				if($oFileInfo->isReadable()) {
89
					$sRetval = $oFileInfo->getBasename('.php');
90
					break;
91
				}
92
			}
93
		}
94
		return $sRetval;
95
	}
96
/**
97
 * set path to translation files
98
 * @throws TranslationException
99
 */
100
	private function _getAddonPath()
101
	{
102
	// set environment
103
		$sAddon   = trim(str_replace('\\', '/', $this->sAddon), '/');
104
		$sAppDirname = str_replace('\\', '/', dirname(dirname(__FILE__))).'/';
105
		$sLanguagePath = $sAppDirname.$sAddon.'/languages/';
106
		if(is_dir($sLanguagePath) && is_readable($sLanguagePath)) {
107
		// valid directory found
108
			return $sLanguagePath;
109
		}
110
		if(preg_match('/^admin.*/sU', $sAddon))
111
		{
112
		// get used acp dirname
113
			$sTmp = trim(WbAdaptor::getInstance()->AcpDir, '/');
114
		// if path starts with 'admin/' then replace with used acp dirname
115
			$sLanguagePath = $sAppDirname.preg_replace('/^admin/sU', $sTmp, $sAddon).'/languages/';
116
			if(is_dir($sLanguagePath) && is_readable($sLanguagePath)) {
117
			// valid directory found
118
				return $sLanguagePath;
119
			}
120
		}
121
        return '';
122
	}
123
/**
124
 * Import language definitions into array
125
 * @param string load language from filename
126
 * @return array contains all found translations
127
 */
128
	private function _importArrays($sLanguageFile)
129
	{
130
		// include the file
131
		include($sLanguageFile);
132
		// get all available loaded vars of this method
133
		$aAllVars = get_defined_vars();
134
		$aLangSections = array();
135
		$aLanguageTable = array();
136
		foreach($aAllVars as $key=>$value) {
137
		// extract the names of arrays from language file
138
			if(is_array($value)) {
139
				$aLangSections[] = $key;
140
			}
141
		}
142
		foreach($aLangSections as $sSection) {
143
		// walk through all arrays
144
			foreach(${$sSection} as $key => $value) {
145
			// and import all found translations
146
				if(!is_array($value)) {
147
				// skip all multiarray definitions from compatibility mode
148
					$aLanguageTable[$sSection.'_'.$key] = $value;
149
				}
150
			}
151
		}
152
		return $aLanguageTable;
153
	}
154
} // end of class TranslateAdaptorWbOldStyle
(13-13/36)