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