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: 2070 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/TranslateAdaptorWbOldStyle.php $
31
 * @lastmodified $Date: 2014-01-03 02:21:42 +0100 (Fri, 03 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
		$sLanguagePath = $this->_getAddonPath();
56
		$aTranslations = array();
57
		$sLangFile = strtolower($sLangCode.'.php');
58
		if( ($aDirContent = scandir($sLanguagePath)) !== false) {
59
			foreach($aDirContent as $sFile) {
60
				if($sLangFile === strtolower($sFile)) {
61
					$sLangFile = $sLanguagePath.$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
		$sLanguagePath = $this->_getAddonPath();
79
	// search for first available and readable language file
80
		$sRetval = '';
81
		if(is_readable($sLanguagePath)) {
82
			$iterator = new DirectoryIterator($sLanguagePath);
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
	// set environment
101
		$sAddon   = trim(str_replace('\\', '/', $this->sAddon), '/');
102
		$sAppDirname = str_replace('\\', '/', dirname(dirname(__FILE__))).'/';
103
		$sLanguagePath = $sAppDirname.$sAddon.'/languages/';
104
		if(is_dir($sLanguagePath) && is_readable($sLanguagePath)) {
105
		// valid directory found
106
			return $sLanguagePath;
107
		}
108
		if(preg_match('/^admin.*/sU', $sAddon))
109
		{
110
		// get used acp dirname
111
			$sTmp = trim(WbAdaptor::getInstance()->AcpDir, '/');
112
		// if path starts with 'admin/' then replace with used acp dirname
113
			$sLanguagePath = $sAppDirname.preg_replace('/^admin/sU', $sTmp, $sAddon).'/languages/';
114
			if(is_dir($sLanguagePath) && is_readable($sLanguagePath)) {
115
			// valid directory found
116
				return $sLanguagePath;
117
			}
118
		}
119
		throw new TranslationException('\''.$sAddon.'/languages\' is not a direcory or not readable!');
120
	}
121
/**
122
 * Import language definitions into array
123
 * @param string load language from filename
124
 * @return array contains all found translations
125
 */
126
	private function _importArrays($sLanguageFile)
127
	{
128
		// include the file
129
		include($sLanguageFile);
130
		// get all available loaded vars of this method
131
		$aAllVars = get_defined_vars();
132
		$aLangSections = array();
133
		$aLanguageTable = array();
134
		foreach($aAllVars as $key=>$value) {
135
		// extract the names of arrays from language file
136
			if(is_array($value)) {
137
				$aLangSections[] = $key;
138
			}
139
		}
140
		foreach($aLangSections as $sSection) {
141
		// walk through all arrays
142
			foreach(${$sSection} as $key => $value) {
143
			// and import all found translations
144
				if(!is_array($value)) {
145
				// skip all multiarray definitions from compatibility mode
146
					$aLanguageTable[$sSection.'_'.$key] = $value;
147
				}
148
			}
149
		}
150
		return $aLanguageTable;
151
	}
152
} // end of class TranslateAdaptorWbOldStyle
(13-13/36)