Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
17
// |          Naoki Shima <naoki@avantexchange.com>                       |
18
// +----------------------------------------------------------------------+
19
//
20
//  $Id: Common.php 110339 2003-01-04 11:55:29Z mj $
21
//
22
 
23
/**
24
*   this class provides language functionality, such as
25
*   determining the language of a given string, etc.
26
*   iso639-1 compliant, 2 letter code is used
27
*   iso639-1 http://www.loc.gov/standards/iso639-2/langcodes.html
28
*
29
*   @package  Language
30
*   @access   public
31
*   @author   Wolfram Kriesing <wolfram@kriesing.de>
32
*   @version  2001/12/29
33
*/
34
class I18N_Messages_Common
35
{
36
 
37
    // {{ properties
38
 
39
    /**
40
    * Holds messageID to corresponding message mapping
41
    *
42
    * @type  : array
43
    * @access: private
44
    */
45
    var $_message = array();
46
 
47
    // {{ constructor
48
 
49
    /**
50
    *
51
    *
52
    *   @access     public
53
    *   @author
54
    *   @version
55
    */
56
    function __construct( )
57
    {
58
# FIXXME pass a resource to the constructor which can be used to determine the
59
# language of a string, it should be possible to use XML, DB, or whatever
60
# this can then be used as a replacement for the array as used now
61
    }
62
 
63
    // }}
64
    // {{ I18N_Messages_Common()
65
 
66
    /**
67
    *   for pre-ZE2 compatibility
68
    *
69
    *   @access     public
70
    *   @author
71
    *   @version
72
    */
73
    function I18N_Messages_Common( )
74
    {
75
        return $this->__construct();
76
    }
77
 
78
    // }}
79
    // {{ determineLanuguage()
80
 
81
    /**
82
    *   trys to get the language of a given string
83
    *
84
    *   @access     public
85
    *   @author     Wolfram Kriesing <wolfram@kriesing.de>
86
    *   @version    01/12/29
87
    *   @param      string  $string     the string which is used to try and determine its language
88
    *   @return     string  iso-string for the language
89
    *
90
    */
91
    function determineLanguage( $string , $source='default' )
92
    {
93
 
94
        // include a file here, so one can provide its own file,
95
        // and to reduce compile time for php, since it will only be included when needed
96
        // the file that gets included might become very big
97
        if( $source=='default' )
98
            include('I18N/Messages/determineLanguage.inc.php');
99
        else
100
            include($source);  // include the file given as parameter, it only needs to provide an array, as in the above included file
101
 
102
        // replace all non word-characters by a space, i hope that is ok for all languages
103
        $string = preg_replace( '/[\W\s]/' , ' ' ,$string);
104
 
105
        $splitString = explode(' ',$string);        // get each single word in a field
106
        foreach( $splitString as $key=>$aString )   // remove spaces around the word and make it lower case
107
            $splitString[$key] = strtolower(trim($aString));
108
 
109
        // simply intersect each language array with the array that we created by splitting the string
110
        // and the result that's size is the biggest is our language
111
        foreach( $languages as $lang=>$aLanguage )
112
            $results[$lang] = sizeof(array_intersect($splitString,$aLanguage));
113
 
114
        arsort($results);
115
        reset ($results);
116
        list($lang,) = each($results);
117
 
118
        return $lang;
119
 
120
    }
121
 
122
    // }}
123
    // {{ get()
124
 
125
    /**
126
    * Look for and return the message corresponds to the messageID passed.
127
    * Returns messageID when the corresponding message is not found
128
    *
129
    * @return: string
130
    * @access: public
131
    * @author: Naoki Shima <naoki@avantexchange.com>
132
    */
133
    function get($messageID = "")
134
    {
135
        return ($messageID !== "" && is_array($this->_message) && in_array($messageID, array_keys($this->_message))) ? $this->_message[$messageID] :$messageID;
136
    }
137
 
138
    // }}
139
    // {{ _()
140
 
141
    /**
142
    * Alias for get(). Function name might not be appropriate because it conflicts PEAR coding standard
143
    * that this is meant to be public function
144
    *
145
    * @param : string        messageID
146
    * @return: string        corresponding message
147
    * @access: public
148
    * @author: Naoki Shima <naoki@avantexchange.com>
149
    */
150
    function _($messageID = "")
151
    {
152
        return $this->get($messageID);
153
    }
154
 
155
    // }}
156
    // {{ set()
157
 
158
    /**
159
    * Set message ID to corresponding string
160
    *
161
    * @return: boolean
162
    * @access: public
163
    * @author: Naoki Shima <naoki@avantexchange.com>
164
    */
165
    function set($messageID = "",$str = "")
166
    {
167
        if($messageID === "") {
168
            return false;
169
        }
170
        if($str === "" && is_array($messageID)) {
171
            // user is passing an array
172
            $this->_message = $messageID;
173
        } else {
174
            $this->_message[$messageID] = $str;
175
        }
176
        return true;
177
    }
178
 
179
    // }}
180
    // {{ setCharset()
181
 
182
    /**
183
    * Set charset of message
184
    *
185
    * @param : string        Charset
186
    *
187
    * @return: void
188
    * @access: public
189
    * @author: Naoki Shima <naoki@avantexchange.com>
190
    */
191
    function setCharset($charset  = I18N_MESSAGES_DEFAULT_CHARSET)
192
    {
193
        $this->_charset = $charset;
194
    }
195
 
196
    // }}
197
    // {{ getCharset()
198
 
199
    /**
200
    * Returns charset of message. Returns null if it's not set.
201
    *
202
    * @return: mixed         Charset
203
    * @access: public
204
    * @author: Naoki Shima <naoki@avantexchange.com>
205
    */
206
    function getCharset()
207
    {
208
        return ($this->_charset ? $this->_charset: false);
209
    }
210
 
211
    // }}
212
    // {{ bindDomain()
213
 
214
    /**
215
    * Bind domain to use
216
    * If domain is not passed and there's already a value set to domain,
217
    * then this method returns current domain.
218
    *
219
    * @param : string
220
    *
221
    * @return: string       Current domain
222
    * @access: public
223
    * @author: Naoki Shima
224
    */
225
    function bindDomain($domain = '')
226
    {
227
        if($domain === '') {
228
            return ($this->_domain ? $this->_domain : '');
229
        }
230
        $this->_domain = $domain;
231
        return $domain;
232
    }
233
 
234
    // }}
235
    // {{ bindLanguage()
236
 
237
    /**
238
    * Bind language to use
239
    * If language is not passed and there's already a value set to domain,
240
    * then this method returns current domain.
241
    *
242
    * @param : string
243
    *
244
    * @return: string       Current language
245
    * @access: public
246
    * @author: Naoki Shima <naoki@avantexchange.com>
247
    */
248
    function bindLanguage($lang = '')
249
    {
250
        if($lang === '') {
251
            return ($this->_lang ? $this->_lang : '');
252
        }
253
        $this->_lang = $lang;
254
        return $lang;
255
    }
256
 
257
    // }}
258
} // end of class
259
?>