~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Zend/View/Helper/Translate.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Zend Framework
 
5
 *
 
6
 * LICENSE
 
7
 *
 
8
 * This source file is subject to the new BSD license that is bundled
 
9
 * with this package in the file LICENSE.txt.
 
10
 * It is also available through the world-wide-web at this URL:
 
11
 * http://framework.zend.com/license/new-bsd
 
12
 * If you did not receive a copy of the license and are unable to
 
13
 * obtain it through the world-wide-web, please send an email
 
14
 * to license@zend.com so we can send you a copy immediately.
 
15
 *
 
16
 * @category   Zend
 
17
 * @package    Zend_View
 
18
 * @subpackage Helper
 
19
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
20
 * @version    $Id: Translate.php 8420 2008-02-26 16:53:53Z darby $
 
21
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
22
 */
 
23
 
 
24
/** Zend_Locale */
 
25
require_once 'Zend/Locale.php';
 
26
 
 
27
/**
 
28
 * Translation view helper
 
29
 *
 
30
 * @category   Zend
 
31
 * @package    Zend_View
 
32
 * @subpackage Helper
 
33
 */
 
34
class Zend_View_Helper_Translate
 
35
{
 
36
    /**
 
37
     * Translation object
 
38
     * @var Zend_Translate_Adapter
 
39
     */
 
40
    protected $_translator;
 
41
 
 
42
    /**
 
43
     * Constructor for manually handling
 
44
     *
 
45
     * @param Zend_Translate|Zend_Translate_Adapter $translate
 
46
     */
 
47
    public function __construct($translate = null)
 
48
    {
 
49
        if (!empty($translate)) {
 
50
            $this->setTranslator($translate);
 
51
        }
 
52
    }
 
53
 
 
54
    /**
 
55
     * Translate a message
 
56
     * You can give multiple params or an array of params.
 
57
     * If you want to output another locale just set it as last single parameter
 
58
     * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
 
59
     * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
 
60
     *
 
61
     * @param string           $messageid
 
62
     * @return string  Translated message
 
63
     */
 
64
    public function translate($messageid = null)
 
65
    {
 
66
        if (null === $messageid) {
 
67
            return $this;
 
68
        }
 
69
 
 
70
        if (null === ($translate = $this->getTranslator())) {
 
71
            return $messageid;
 
72
        }
 
73
 
 
74
        $options = func_get_args();
 
75
        array_shift($options);
 
76
 
 
77
        $count   = count($options);
 
78
        $locale  = null;
 
79
        if ($count > 0) {
 
80
            if (Zend_Locale::isLocale($options[$count - 1])) {
 
81
                $locale = array_pop($options);
 
82
            }
 
83
        }
 
84
        if ((count($options) == 1) and (is_array($options[0]))) {
 
85
            $options = $options[0];
 
86
        }
 
87
        $message = $translate->translate($messageid, $locale);
 
88
        return vsprintf($message, $options);
 
89
    }
 
90
 
 
91
    /**
 
92
     * Sets a translation Adapter for translation
 
93
     *
 
94
     * @param  Zend_Translate|Zend_Translate_Adapter $translate
 
95
     * @return Zend_View_Helper_Translate
 
96
     */
 
97
    public function setTranslator($translate)
 
98
    {
 
99
        if ($translate instanceof Zend_Translate_Adapter) {
 
100
            $this->_translator = $translate;
 
101
        } elseif ($translate instanceof Zend_Translate) {
 
102
            $this->_translator = $translate->getAdapter();
 
103
        } else {
 
104
            require_once 'Zend/View/Exception.php';
 
105
            throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
 
106
        }
 
107
        return $this;
 
108
    }
 
109
 
 
110
    /**
 
111
     * Retrieve translation object
 
112
     *
 
113
     * If none is currently registered, attempts to pull it from the registry
 
114
     * using the key 'Zend_Translate'.
 
115
     *
 
116
     * @return Zend_Translate_Adapter|null
 
117
     */
 
118
    public function getTranslator()
 
119
    {
 
120
        if (null === $this->_translator) {
 
121
            require_once 'Zend/Registry.php';
 
122
            if (Zend_Registry::isRegistered('Zend_Translate')) {
 
123
                $this->setTranslator(Zend_Registry::get('Zend_Translate'));
 
124
            }
 
125
        }
 
126
        return $this->_translator;
 
127
    }
 
128
 
 
129
    /**
 
130
     * Set's an new locale for all further translations
 
131
     *
 
132
     * @param  string|Zend_Locale $locale
 
133
     * @return Zend_View_Helper_Translate
 
134
     */
 
135
    public function setLocale($locale = null)
 
136
    {
 
137
        if (null === ($translate = $this->getTranslator())) {
 
138
            require_once 'Zend/View/Exception.php';
 
139
            throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
 
140
        }
 
141
        $translate->setLocale($locale);
 
142
        return $this;
 
143
    }
 
144
 
 
145
    /**
 
146
     * Returns the set locale for translations
 
147
     *
 
148
     * @return string|Zend_Locale
 
149
     */
 
150
    public function getLocale()
 
151
    {
 
152
        if (null === ($translate = $this->getTranslator())) {
 
153
            require_once 'Zend/View/Exception.php';
 
154
            throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
 
155
        }
 
156
        return $translate->getLocale();
 
157
    }
 
158
}