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
 * @author       Werner v.d.Decken <wkl@isteam.de>
26
 * @copyright    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: 1860 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/TranslateAdaptorWbOldStyle.php $
31
 * @lastmodified $Date: 2013-02-04 10:51:05 +0100 (Mon, 04 Feb 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 {
37

    
38
	protected $sAppPath   = '';
39
	protected $sAddon     = '';
40
	protected $aLangTable = array();
41
	protected $sFilePath  = '';
42
/**
43
 * Constructor
44
 * @param string descriptor of the Addon (i.e. '' || 'modules\myAddon'
45
 */
46
	public function __construct($sAddon = '')
47
	{
48
		$this->sAddon = $sAddon;
49
		$this->sAppPath = dirname(dirname(__FILE__)).'/';
50
		$this->sFilePath = $this->sAppPath
51
		                 . trim(str_replace('\\', '/', $sAddon), '/').'/languages/';
52
	}
53
/**
54
 * Load languagefile
55
 * @param string $sLangCode
56
 * @return array|bool an array of translations or FALSE on error
57
 */
58
	public function loadLanguage($sLangCode)
59
	{
60
		$aTranslations = array();
61
		// sanitize the language code
62
		$aLangCode = explode('_', preg_replace('/[^a-z0-9]/i', '_', strtolower($sLangCode)));
63
		$sConcatedLang = '';
64
		foreach($aLangCode as $sLang)
65
		{ // iterate all segments of the language code
66
			$sLangFile = '';
67
			$sLang = strtolower($sLang);
68
			// seek lowerchars file
69
			if(is_readable($this->sFilePath.$sConcatedLang.$sLang.'.php')) {
70
				$sLangFile = $this->sFilePath.$sConcatedLang.$sLang.'.php';
71
			}else {
72
				// seek upperchars file
73
				$sLang = strtoupper($sLang);
74
				if(is_readable($this->sFilePath.$sConcatedLang.$sLang.'.php')) {
75
					$sLangFile = $this->sFilePath.$sConcatedLang.$sLang.'.php';
76
				}
77
			}
78
			if($sLangFile) {
79
			// import the fond file
80
				$sConcatedLang .= $sLangFile.'_';
81
				$aTmp = $this->_importArrays($sLangFile);
82
				$aTranslations = array_merge($aTranslations, $aTmp);
83
			}
84
		}
85
		return (sizeof($aTranslations) > 0 ? $aTranslations : false);
86
	}
87
/**
88
 * Find first existing language
89
 * @return string Code of first found language
90
 */
91
	public function findFirstLanguage()
92
	{
93
	// search for first available and readable language file
94
		$sRetval = '';
95
		if(is_readable($this->sFilePath)) {
96
			$iterator = new DirectoryIterator($this->sFilePath);
97
			foreach ($iterator as $oFileInfo) {
98
				$sPattern = '/^[a-z0-9]{2}(?:(?:\_[a-z0-9]{2})?(?:\_[a-z0-9]{2,4})?)\.php/siU';
99
				if(!preg_match($sPattern, $oFileInfo->getBasename())) { continue; }
100
				if($oFileInfo->isReadable()) {
101
					$sRetval = $oFileInfo->getBasename('.php');
102
					break;
103
				}
104
			}
105
		}
106
		return $sRetval;
107
	}
108
/**
109
 * Import language definitions into array
110
 * @param string load language from filename
111
 * @return array contains all found translations
112
 */
113
	private function _importArrays($sLanguageFile)
114
	{
115
		// include the file
116
		include($sLanguageFile);
117
		// get all available loaded vars of this method
118
		$aAllVars = get_defined_vars();
119
		$aLangSections = array();
120
		$aLanguageTable = array();
121
		foreach($aAllVars as $key=>$value) {
122
		// extract the names of arrays from language file
123
			if(is_array($value)) {
124
				$aLangSections[] = $key;
125
			}
126
		}
127
		foreach($aLangSections as $sSection) {
128
		// walk through all arrays
129
			foreach(${$sSection} as $key => $value) {
130
			// and import all found translations
131
				$aLanguageTable[$sSection.'_'.$key] = $value;
132
			}
133
		}
134
		return $aLanguageTable;
135
	}
136
} // end of class TranslateAdaptorWbOldStyle
(8-8/29)