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