Revision 1873
Added by darkviper over 11 years ago
Translate.php | ||
---|---|---|
22 | 22 |
* |
23 | 23 |
* @category Core |
24 | 24 |
* @package Core_Translation |
25 |
* @copyright Werner v.d.Decken <wkl@isteam.de> |
|
25 | 26 |
* @author Werner v.d.Decken <wkl@isteam.de> |
26 |
* @copyright Werner v.d.Decken <wkl@isteam.de> |
|
27 | 27 |
* @license http://www.gnu.org/licenses/gpl.html GPL License |
28 | 28 |
* @version 0.0.1 |
29 | 29 |
* @revision $Revision$ |
30 | 30 |
* @link $HeadURL$ |
31 | 31 |
* @lastmodified $Date$ |
32 | 32 |
* @since File available since 12.01.2013 |
33 |
* @description |
|
33 |
* @description basic class of the Translate package
|
|
34 | 34 |
*/ |
35 | 35 |
class Translate { |
36 | 36 |
|
37 |
// @object hold the Singleton instance
|
|
37 |
/** holds the active singleton instance */
|
|
38 | 38 |
private static $_oInstance = null; |
39 |
|
|
39 |
/** name of the default adaptor */ |
|
40 | 40 |
protected $sAdaptor = 'WbOldStyle'; |
41 |
/** setting for the system language (default: en) */ |
|
41 | 42 |
protected $sSystemLanguage = 'en'; |
43 |
/** setting for the default runtime language (default: en) */ |
|
42 | 44 |
protected $sDefaultLanguage = 'en'; |
45 |
/** setting for the user defined language (default: en) */ |
|
43 | 46 |
protected $sUserLanguage = 'en'; |
47 |
/** switch cache on/off */ |
|
44 | 48 |
protected $bUseCache = true; |
45 |
/** TranslationTable objects of all loaded addons */ |
|
49 |
/** defines if the keys of undefines translation should be preserved */ |
|
50 |
protected $bRemoveMissing = false; |
|
51 |
/** can hold up to 31 boolean option settings */ |
|
52 |
protected $iOptions = 0; |
|
53 |
/** List of loaded translation tables */ |
|
46 | 54 |
protected $aLoadedAddons = array(); |
47 | 55 |
/** TranslationTable object of the core and additional one activated addon */ |
48 | 56 |
protected $aActiveTranslations = array(); |
57 |
/** possible option flags */ |
|
58 |
const CACHE_DISABLED = 1; // ( 2^0 ) |
|
59 |
const KEEP_MISSING = 2; // ( 2^1 ) |
|
49 | 60 |
|
50 |
const CACHE_ENABLED = true; |
|
51 |
const CACHE_DISABLED = false; |
|
52 |
|
|
53 | 61 |
/** prevent class from public instancing and get an object to hold extensions */ |
54 | 62 |
protected function __construct() {} |
55 | 63 |
/** prevent from cloning existing instance */ |
... | ... | |
67 | 75 |
} |
68 | 76 |
/** |
69 | 77 |
* Initialize the Translations |
78 |
* @param string SystemLanguage code ('de' || 'de_CH' || 'de_CH_uri') |
|
70 | 79 |
* @param string DefaultLanguage code ('de' || 'de_CH' || 'de_CH_uri') |
71 | 80 |
* @param string UserLanguage code ('de' || 'de_CH' || 'de_CH_uri') |
81 |
* @param string Name of the adaptor to use |
|
82 |
* @param integer possible options (DISABLE_CACHE | KEEP_MISSING) |
|
72 | 83 |
* @throws TranslationException |
73 | 84 |
*/ |
74 |
public function initialize($sSystemLanguage, $sDefaultLanguage, $sUserLanguage = '', |
|
75 |
$sAdaptor = 'WbOldStyle', $bUseCache = self::CACHE_ENABLED) |
|
85 |
public function initialize($sSystemLanguage, |
|
86 |
$sDefaultLanguage, |
|
87 |
$sUserLanguage = '', |
|
88 |
$sAdaptor = 'WbOldStyle', |
|
89 |
$iOptions = 0) |
|
76 | 90 |
{ |
77 | 91 |
if(!class_exists('TranslateAdaptor'.$sAdaptor)) { |
78 | 92 |
throw new TranslationException('unable to load adaptor: '.$sAdaptor); |
79 | 93 |
} |
80 |
$this->bUseCache = $bUseCache; |
|
81 |
$this->sAdaptor = 'TranslateAdaptor'.$sAdaptor; |
|
94 |
$this->sAdaptor = 'TranslateAdaptor'.$sAdaptor; |
|
95 |
$this->bUseCache = (($iOptions & self::CACHE_DISABLED) == 0); |
|
96 |
$this->bRemoveMissing = (($iOptions & self::KEEP_MISSING) == 0); |
|
82 | 97 |
// if no system language is set then use language 'en' |
83 | 98 |
$this->sSystemLanguage = (trim($sSystemLanguage) == '' ? 'en' : $sSystemLanguage); |
84 | 99 |
// if no default language is set then use system language |
... | ... | |
87 | 102 |
: $sDefaultLanguage); |
88 | 103 |
// if no user language is set then use default language |
89 | 104 |
$this->sUserLanguage = (trim($sUserLanguage) == '' |
90 |
? $this->sDefaultLanguage
|
|
105 |
? $this->sDefaultLanguage |
|
91 | 106 |
: $sUserLanguage); |
92 |
$sPattern = '/^[a-z]{2,3}(?:(?:\_[a-z]{2})?(?:\_[a-z0-9]{2,4})?)$/siU';
|
|
107 |
$sPattern = '/^[a-z]{2,3}(?:(?:[_-][a-z]{2})?(?:[_-][a-z0-9]{2,4})?)$/siU';
|
|
93 | 108 |
// validate language codes |
94 | 109 |
if(preg_match($sPattern, $this->sSystemLanguage) && |
95 | 110 |
preg_match($sPattern, $this->sDefaultLanguage) && |
... | ... | |
177 | 192 |
*/ |
178 | 193 |
public function __get($sKey) |
179 | 194 |
{ |
180 |
$sRetval = '';
|
|
195 |
$sRetval = ($this->bRemoveMissing ? '' : '#'.$sKey.'#');
|
|
181 | 196 |
foreach($this->aActiveTranslations as $oAddon) { |
182 | 197 |
if(isset($oAddon->$sKey)) { |
183 | 198 |
// search the last matching translation (Core -> Addon) |
Also available in: Unified diff
some small fixes in packet Translate
added option to disable cache
added option to keep placeholders if no translation was found