| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Translation, static.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: Translation.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.I18N
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Get the MessageFormat class.
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.I18N.core.MessageFormat');
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Translation class.
|
|
|
21 |
*
|
|
|
22 |
* Provides translation using a static MessageFormatter.
|
|
|
23 |
*
|
|
|
24 |
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
25 |
* @version v1.0, last update on Tue Dec 28 11:54:48 EST 2004
|
|
|
26 |
* @package System.I18N
|
|
|
27 |
*/
|
|
|
28 |
class Translation extends TComponent
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
* The array of formatters. We define 1 formatter per translation catalog
|
|
|
32 |
* This is a class static variable.
|
|
|
33 |
* @var array
|
|
|
34 |
*/
|
|
|
35 |
protected static $formatters=array();
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Initialize the TTranslate translation components
|
|
|
39 |
*/
|
|
|
40 |
public static function init($catalogue='messages')
|
|
|
41 |
{
|
|
|
42 |
static $saveEventHandlerAttached=false;
|
|
|
43 |
|
|
|
44 |
//initialized the default class wide formatter
|
|
|
45 |
if(!isset(self::$formatters[$catalogue]))
|
|
|
46 |
{
|
|
|
47 |
$app = Prado::getApplication()->getGlobalization();
|
|
|
48 |
$config = $app->getTranslationConfiguration();
|
|
|
49 |
$source = MessageSource::factory($config['type'],
|
|
|
50 |
$config['source'],
|
|
|
51 |
$config['filename']);
|
|
|
52 |
|
|
|
53 |
$source->setCulture($app->getCulture());
|
|
|
54 |
|
|
|
55 |
if($config['cache'])
|
|
|
56 |
$source->setCache(new MessageCache($config['cache']));
|
|
|
57 |
|
|
|
58 |
self::$formatters[$catalogue] = new MessageFormat($source, $app->getCharset());
|
|
|
59 |
|
|
|
60 |
//mark untranslated text
|
|
|
61 |
if($ps=$config['marker'])
|
|
|
62 |
self::$formatters[$catalogue]->setUntranslatedPS(array($ps,$ps));
|
|
|
63 |
|
|
|
64 |
//save the message on end request
|
|
|
65 |
// Do it only once !
|
|
|
66 |
if (!$saveEventHandlerAttached)
|
|
|
67 |
{
|
|
|
68 |
Prado::getApplication()->attachEventHandler(
|
|
|
69 |
'OnEndRequest', array('Translation', 'saveMessages'));
|
|
|
70 |
$saveEventHandlerAttached=true;
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Get the static formatter from this component.
|
|
|
77 |
* @return MessageFormat formattter.
|
|
|
78 |
* @see localize()
|
|
|
79 |
*/
|
|
|
80 |
public static function formatter($catalogue='messages')
|
|
|
81 |
{
|
|
|
82 |
return self::$formatters[$catalogue];
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Save untranslated messages to the catalogue.
|
|
|
87 |
*/
|
|
|
88 |
public static function saveMessages()
|
|
|
89 |
{
|
|
|
90 |
static $onceonly = true;
|
|
|
91 |
|
|
|
92 |
if($onceonly)
|
|
|
93 |
{
|
|
|
94 |
foreach (self::$formatters as $catalogue=>$formatter)
|
|
|
95 |
{
|
|
|
96 |
$app = Prado::getApplication()->getGlobalization();
|
|
|
97 |
$config = $app->getTranslationConfiguration();
|
|
|
98 |
if(isset($config['autosave']))
|
|
|
99 |
{
|
|
|
100 |
$formatter->getSource()->setCulture($app->getCulture());
|
|
|
101 |
$formatter->getSource()->save($catalogue);
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
$onceonly = false;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|