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: 1926 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/TranslateAdaptorWbOldStyle.php $
31
 * @lastmodified $Date: 2013-06-10 12:38:17 +0200 (Mon, 10 Jun 2013) $
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
		$this->sFilePath = str_replace('\\', '/', dirname(dirname(__FILE__)).'/'.$sAddon);
48
	}
49
/**
50
 * Load languagefile
51
 * @param string $sLangCode
52
 * @return array|bool an array of translations or FALSE on error
53
 */
54
	public function loadLanguage($sLangCode)
55
	{
56
		$this->_getAddonPath();
57
		$aTranslations = array();
58
		$sLangFile = strtolower($sLangCode.'.php');
59
		if( ($aDirContent = scandir($this->sFilePath)) !== false) {
60
			foreach($aDirContent as $sFile) {
61
				if($sLangFile === strtolower($sFile)) {
62
					$sLangFile = $this->sFilePath.$sFile;
63
					if(is_readable($sLangFile)) {
64
						$aTmp = $this->_importArrays($sLangFile);
65
						$aTranslations = array_merge($aTranslations, $aTmp);
66
						break;
67
					}
68
				}
69
			}
70
		}
71
		return (sizeof($aTranslations) > 0 ? $aTranslations : false);
72
	}
73
/**
74
 * Find first existing language
75
 * @return string Code of first found basic language
76
 */
77
	public function findFirstLanguage()
78
	{
79
		$this->_getAddonPath();
80
	// search for first available and readable language file
81
		$sRetval = '';
82
		if(is_readable($this->sFilePath)) {
83
			$iterator = new DirectoryIterator($this->sFilePath);
84
			foreach ($iterator as $oFileInfo) {
85
				$sPattern = '/^[a-z]{2,3}\.php/siU';
86
				if(!preg_match($sPattern, $oFileInfo->getBasename())) { continue; }
87
				if($oFileInfo->isReadable()) {
88
					$sRetval = $oFileInfo->getBasename('.php');
89
					break;
90
				}
91
			}
92
		}
93
		return $sRetval;
94
	}
95
/**
96
 * set path to translation files
97
 * @throws TranslationException
98
 */
99
	private function _getAddonPath()
100
	{
101
		$sAddon   = str_replace('\\', '/', $this->sAddon);
102
		$sDirname = str_replace('\\', '/', dirname(dirname(__FILE__))).'/';
103
		$this->sFilePath = $sDirname.$sAddon.'/languages/';
104
		if(!is_readable($this->sFilePath) && (strpos('admin', $this->sAddon) === 0)) {
105
		// correct modified admin directory
106
			$sTmp = trim(WbAdaptor::getInstance()->AcpDir);
107
			$this->sFilePath = $sDirname.preg_replace('/^admin/', $sTmp, $sAddon).'/languages/';
108
			if(!is_readable($this->sFilePath)) {
109
				throw new TranslationException('missing language definitions in: '.$sAddon.'/languages');
110
			}
111
		}
112
	}
113
/**
114
 * Import language definitions into array
115
 * @param string load language from filename
116
 * @return array contains all found translations
117
 */
118
	private function _importArrays($sLanguageFile)
119
	{
120
		// include the file
121
		include($sLanguageFile);
122
		// get all available loaded vars of this method
123
		$aAllVars = get_defined_vars();
124
		$aLangSections = array();
125
		$aLanguageTable = array();
126
		foreach($aAllVars as $key=>$value) {
127
		// extract the names of arrays from language file
128
			if(is_array($value)) {
129
				$aLangSections[] = $key;
130
			}
131
		}
132
		foreach($aLangSections as $sSection) {
133
		// walk through all arrays
134
			foreach(${$sSection} as $key => $value) {
135
			// and import all found translations
136
				if(!is_array($value)) {
137
				// skip all multiarray definitions from compatibility mode
138
					$aLanguageTable[$sSection.'_'.$key] = $value;
139
				}
140
			}
141
		}
142
		return $aLanguageTable;
143
	}
144
} // end of class TranslateAdaptorWbOldStyle
(10-10/32)