]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Localization/Localization.php
Release 6.1.4
[Github/sugarcrm.git] / include / Localization / Localization.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42  * Reserved. Contributor(s): ______________________________________..
43  * *******************************************************************************/
44
45
46 class Localization {
47         var $availableCharsets = array(
48                 'BIG-5',        //Taiwan and Hong Kong
49                 /*'CP866'                         // ms-dos Cyrillic */
50                 /*'CP949'                         //Microsoft Korean */
51                 'CP1251',       //MS Cyrillic
52                 'CP1252',       //MS Western European & US
53                 'EUC-CN',       //Simplified Chinese GB2312
54                 'EUC-JP',       //Unix Japanese
55                 'EUC-KR',       //Korean
56                 'EUC-TW',       //Taiwanese
57                 'ISO-2022-JP',  //Japanese
58                 'ISO-2022-KR',  //Korean
59                 'ISO-8859-1',   //Western European and US
60                 'ISO-8859-2',   //Central and Eastern European
61                 'ISO-8859-3',   //Latin 3
62                 'ISO-8859-4',   //Latin 4
63                 'ISO-8859-5',   //Cyrillic
64                 'ISO-8859-6',   //Arabic
65                 'ISO-8859-7',   //Greek
66                 'ISO-8859-8',   //Hebrew
67                 'ISO-8859-9',   //Latin 5
68                 'ISO-8859-10',  //Latin 6
69                 'ISO-8859-13',  //Latin 7
70                 'ISO-8859-14',  //Latin 8
71                 'ISO-8859-15',  //Latin 9
72                 'KOI8-R',       //Cyrillic Russian
73                 'KOI8-U',       //Cyrillic Ukranian
74                 'SJIS',         //MS Japanese
75                 'UTF-8',        //UTF-8
76                 );
77         var $localeNameFormat;
78         var $localeNameFormatDefault;
79         var $default_export_charset = 'CP1252'; // not camel hump to match sugar_config's
80         var $default_email_charset = 'ISO-8859-1';
81         var $currencies = array(); // array loaded with current currencies
82
83
84         /**
85          * sole constructor
86          */
87         function Localization() {
88                 global $sugar_config;
89                 $this->localeNameFormatDefault = empty($sugar_config['locale_name_format_default']) ? 's f l' : $sugar_config['default_name_format'];
90                 $this->loadCurrencies();
91         }
92
93         /**
94          * returns an array of Sugar Config defaults that are determined by locale settings
95          * @return array
96          */
97         function getLocaleConfigDefaults() {
98                 $coreDefaults = array(
99                         'currency'                                                              => '',
100                         'datef'                                                                 => 'm/d/Y',
101                         'timef'                                                                 => 'H:i',
102                         'default_currency_significant_digits'   => 2,
103                         'default_currency_symbol'                               => '$',
104                         'default_export_charset'                                => $this->default_export_charset,
105                         'default_locale_name_format'                    => 's f l',
106                         'default_number_grouping_seperator'             => ',',
107                         'default_decimal_seperator'                             => '.',
108                         'export_delimiter'                                              => ',',
109                         'default_email_charset'                                 => $this->default_email_charset,
110                 );
111
112                 return $coreDefaults;
113         }
114
115         /**
116          * abstraction of precedence
117          * @param string prefName Name of preference to retrieve based on overrides
118          * @param object user User in focus, default null (current_user)
119          * @return string pref Most significant preference
120          */
121         function getPrecedentPreference($prefName, $user=null, $sugarConfigPrefName = '') {
122                 global $current_user;
123                 global $sugar_config;
124
125                 $userPref = '';
126                 $coreDefaults = $this->getLocaleConfigDefaults();
127                 $pref = isset($coreDefaults[$prefName]) ? $coreDefaults[$prefName] : ''; // defaults, even before config.php
128
129                 if($user != null) {
130                         $userPref = $user->getPreference($prefName);
131                 } elseif(!empty($current_user)) {
132                         $userPref = $current_user->getPreference($prefName);
133                 }
134                 // Bug 39171 - If we are asking for default_email_charset, check in emailSettings['defaultOutboundCharset'] as well
135                 if ( $prefName == 'default_email_charset' ) {
136                     if($user != null) {
137                 $emailSettings = $user->getPreference('emailSettings', 'Emails');
138             } elseif(!empty($current_user)) {
139                 $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
140             }
141             if ( isset($emailSettings['defaultOutboundCharset']) ) {
142                 $userPref = $emailSettings['defaultOutboundCharset'];
143             }
144                 }
145
146                 // set fallback defaults defined in this class
147                 if(isset($this->$prefName)) {
148                         $pref = $this->$prefName;
149                 }
150                 //rrs: 33086 - give the ability to pass in the preference name as stored in $sugar_config.
151                 if(!empty($sugarConfigPrefName)){
152                         $prefName = $sugarConfigPrefName;
153                 }
154                 // cn: 9549 empty() call on a value of 0 (0 significant digits) resulted in a false-positive.  changing to "isset()"
155                 $pref = (!isset($sugar_config[$prefName]) || (empty($sugar_config[$prefName]) && $sugar_config[$prefName] !== '0')) ? $pref : $sugar_config[$prefName];
156                 $pref = (empty($userPref) && $userPref !== '0') ? $pref : $userPref;
157                 return $pref;
158         }
159
160         ///////////////////////////////////////////////////////////////////////////
161         ////    CURRENCY HANDLING
162         /**
163          * wrapper for whatever currency system we implement
164          */
165         function loadCurrencies() {
166                 // doing it dirty here
167                 global $db;
168                 global $sugar_config;
169
170                 if(empty($db)) {
171                         return array();
172                 }
173
174         $load = sugar_cache_retrieve('currency_list');
175         if ( !is_array($load) ) {
176             $q = "SELECT id, name, symbol, conversion_rate FROM currencies WHERE status = 'Active' and deleted = 0";
177             $r = $db->query($q);
178             
179             while($a = $db->fetchByAssoc($r)) {
180                 $load = array();
181                 $load['name'] = $a['name'];
182                 $load['symbol'] = $a['symbol'];
183                 $load['conversion_rate'] = $a['conversion_rate'];
184                 
185                 $this->currencies[$a['id']] = $load;
186             }
187             sugar_cache_put('currency_list',$this->currencies);
188         } else {
189             $this->currencies = $load;
190         }
191
192                 // load default from config.php
193                 $this->currencies['-99'] = array(
194                         'name'          => $sugar_config['default_currency_name'],
195                         'symbol'        => $sugar_config['default_currency_symbol'],
196                         'conversion_rate' => 1
197                         );
198
199         }
200
201         /**
202          * getter for currencies array
203          * @return array $this->currencies returns array( id => array(name => X, etc
204          */
205         function getCurrencies() {
206                 return $this->currencies;
207         }
208
209         /**
210          * retrieves default OOTB currencies for sugar_config and installer.
211          * @return array ret Array of default currencies keyed by ISO4217 code
212          */
213         function getDefaultCurrencies() {
214                 $ret = array(
215                         'AUD' => array( 'name'          => 'Australian Dollars',
216                                                         'iso4217'       => 'AUD',
217                                                         'symbol'        => '$'),
218                         'BRL' => array( 'name'          => 'Brazilian Reais',
219                                                         'iso4217'       => 'BRL',
220                                                         'symbol'        => 'R$'),
221                         'GBP' => array( 'name'          => 'British Pounds',
222                                                         'iso4217'       => 'GBP',
223                                                         'symbol'        => '£'),
224                         'CAD' => array( 'name'          => 'Canadian Dollars',
225                                                         'iso4217'       => 'CAD',
226                                                         'symbol'        => '$'),
227                         'CNY' => array( 'name'          => 'Chinese Yuan',
228                                                         'iso4217'       => 'CNY',
229                                                         'symbol'        => 'ï¿¥'),
230                         'EUR' => array( 'name'          => 'Euro',
231                                                         'iso4217'       => 'EUR',
232                                                         'symbol'        => '€'),
233                         'HKD' => array( 'name'          => 'Hong Kong Dollars',
234                                                         'iso4217'       => 'HKD',
235                                                         'symbol'        => '$'),
236                         'INR' => array( 'name'          => 'Indian Rupees',
237                                                         'iso4217'       => 'INR',
238                                                         'symbol'        => '₨'),
239                         'KRW' => array( 'name'          => 'Korean Won',
240                                                         'iso4217'       => 'KRW',
241                                                         'symbol'        => 'â‚©'),
242                         'YEN' => array( 'name'          => 'Japanese Yen',
243                                                         'iso4217'       => 'JPY',
244                                                         'symbol'        => 'Â¥'),
245                         'MXM' => array( 'name'          => 'Mexican Pesos',
246                                                         'iso4217'       => 'MXM',
247                                                         'symbol'        => '$'),
248                         'SGD' => array( 'name'          => 'Singaporean Dollars',
249                                                         'iso4217'       => 'SGD',
250                                                         'symbol'        => '$'),
251                         'CHF' => array( 'name'          => 'Swiss Franc',
252                                                         'iso4217'       => 'CHF',
253                                                         'symbol'        => 'SFr.'),
254                         'THB' => array( 'name'          => 'Thai Baht',
255                                                         'iso4217'       => 'THB',
256                                                         'symbol'        => '฿'),
257                         'USD' => array( 'name'          => 'US Dollars',
258                                                         'iso4217'       => 'USD',
259                                                         'symbol'        => '$'),
260                 );
261
262                 return $ret;
263         }
264         ////    END CURRENCY HANDLING
265         ///////////////////////////////////////////////////////////////////////////
266
267
268         ///////////////////////////////////////////////////////////////////////////
269         ////    CHARSET TRANSLATION
270         /**
271          * returns a mod|app_strings array in the target charset
272          * @param array strings $mod_string, et.al.
273          * @param string charset Target charset
274          * @return array Translated string pack
275          */
276         function translateStringPack($strings, $charset) {
277                 // handle recursive
278                 foreach($strings as $k => $v) {
279                         if(is_array($v)) {
280                                 $strings[$k] = $this->translateStringPack($v, $charset);
281                         } else {
282                                 $strings[$k] = $this->translateCharset($v, 'UTF-8', $charset);
283                         }
284                 }
285                 ksort($strings);
286                 return $strings;
287         }
288
289         /**
290          * translates the passed variable for email sending (export)
291          * @param       mixed the var (array or string) to translate
292          * @return      mixed the translated variable
293          */
294         function translateForEmail($var) {
295                 if(is_array($var)) {
296                         foreach($var as $k => $v) {
297                                 $var[$k] = $this->translateForEmail($v);
298                         }
299                         return $var;
300                 } elseif(!empty($var)) {
301                         return $this->translateCharset($var, 'UTF-8', $this->getOutboundEmailCharset());
302                 }
303         }
304
305         /**
306          * prepares a bean for export by translating any text fields into the export
307          * character set
308          * @param bean object A SugarBean
309          * @return bean object The bean with translated strings
310          */
311     function prepBeanForExport($bean) {
312         foreach($bean->field_defs as $k => $field) {
313                         $bean->$k = $this->translateCharset($bean->$k, 'UTF-8', $this->getExportCharset());
314         }
315
316         return $bean;
317     }
318
319         /**
320          * translates a character set from one encoding to another encoding
321          * @param string string the string to be translated
322          * @param string fromCharset the charset the string is currently in
323          * @param string toCharset the charset to translate into (defaults to UTF-8)
324          * @return string the translated string
325          */
326         function translateCharset($string, $fromCharset, $toCharset='UTF-8') {
327                 $GLOBALS['log']->debug("Localization: translating [ {$string} ] into {$toCharset}");
328                 if(function_exists('mb_convert_encoding')) {
329                         return mb_convert_encoding($string, $toCharset, $fromCharset);
330                 } elseif(function_exists('iconv')) { // iconv is flakey
331                         return iconv($fromCharset, $toCharset, $string);
332                 } else {
333                         return $string;
334                 } // end else clause
335         }
336
337         /**
338          * translates a character set from one to another, and the into MIME-header friendly format
339          */
340         function translateCharsetMIME($string, $fromCharset, $toCharset='UTF-8', $encoding="Q") {
341                 $previousEncoding = mb_internal_encoding();
342             mb_internal_encoding($toCharset);
343                 $result = mb_encode_mimeheader($string, $toCharset, $encoding);
344                 mb_internal_encoding($previousEncoding);
345                 return $result;
346         }
347
348         function normalizeCharset($charset) {
349                 $charset = strtolower(preg_replace("/[\-\_]*/", "", $charset));
350                 return $charset;
351         }
352
353         /**
354          * returns an array of charsets with keys for available translations; appropriate for get_select_options_with_id()
355          */
356         function getCharsetSelect() {
357     //jc:12293 - the "labels" or "human-readable" representations of the various charsets
358     //should be translatable
359     $translated = array();
360     foreach($this->availableCharsets as $key)
361     {
362          //$translated[$key] = translate($value);
363          $translated[$key] = translate($key);
364     }
365
366                 return $translated;
367     //end:12293
368         }
369
370         /**
371          * returns the charset preferred in descending order: User, Sugar Config, DEFAULT
372          * @param string charset to override ALL, pass a valid charset here
373          * @return string charset the chosen character set
374          */
375         function getExportCharset($charset='', $user=null) {
376                 $charset = $this->getPrecedentPreference('default_export_charset', $user);
377                 return $charset;
378         }
379
380         /**
381          * returns the charset preferred in descending order: User, Sugar Config, DEFAULT
382          * @return string charset the chosen character set
383          */
384         function getOutboundEmailCharset($user=null) {
385                 $charset = $this->getPrecedentPreference('default_email_charset', $user);
386                 return $charset;
387         }
388         ////    END CHARSET TRANSLATION
389         ///////////////////////////////////////////////////////////////////////////
390
391         ///////////////////////////////////////////////////////////////////////////
392         ////    NUMBER DISPLAY FORMATTING CODE
393         function getDecimalSeparator($user=null) {
394                 $dec = $this->getPrecedentPreference('default_decimal_separator', $user);
395                 return $dec;
396         }
397
398         function getNumberGroupingSeparator($user=null) {
399                 $sep = $this->getPrecedentPreference('default_number_grouping_seperator', $user);
400                 return $sep;
401         }
402
403         function getPrecision($user=null) {
404                 $precision = $this->getPrecedentPreference('default_currency_significant_digits', $user);
405                 return $precision;
406         }
407
408         /**
409          * returns a number formatted by user preference or system default
410          * @param string number Number to be formatted and returned
411          * @param string currencySymbol Currency symbol if override is necessary
412          * @param bool is_currency Flag to also return the currency symbol
413          * @return string Formatted number
414          */
415         function getLocaleFormattedNumber($number, $currencySymbol='', $is_currency=true, $user=null) {
416                 $fnum                   = $number;
417                 $majorDigits    = '';
418                 $minorDigits    = '';
419                 $dec                    = $this->getDecimalSeparator($user);
420                 $thou                   = $this->getNumberGroupingSeparator($user);
421                 $precision              = $this->getPrecision($user);
422                 $symbol                 = empty($currencySymbol) ? $this->getCurrencySymbol() : $currencySymbol;
423
424                 $exNum = explode($dec, $number);
425                 // handle grouping
426                 if(is_array($exNum) && count($exNum) > 0) {
427                         if(strlen($exNum) > 3) {
428                                 $offset = strlen($exNum[0]) % 3;
429                                 if($offset > 0) {
430                                         for($i=0; $i<$offset; $i++) {
431                                                 $majorDigits .= $exNum[0]{$i};
432                                         }
433                                 }
434
435                                 $tic = 0;
436                                 for($i=$offset; $i<strlen($exNum[0]); $i++) {
437                                         if($tic % 3 == 0 && $i != 0) {
438                                                 $majorDigits .= $thou; // add separator
439                                         }
440
441                                         $majorDigits .= $exNum[0]{$i};
442                                         $tic++;
443                                 }
444                         } else {
445                                 $majorDigits = $exNum[0]; // no formatting needed
446                         }
447                         $fnum = $majorDigits;
448                 }
449
450                 // handle decimals
451                 if($precision > 0) { // we toss the minor digits otherwise
452                         if(is_array($exNum) && isset($exNum[1])) {
453
454                         }
455                 }
456
457
458                 if($is_currency) {
459                         $fnum = $symbol.$fnum;
460                 }
461                 return $fnum;
462         }
463
464         /**
465          * returns Javascript to format numbers and currency for ***DISPLAY***
466          */
467         function getNumberJs() {
468                 $out = <<<eoq
469
470                         var exampleDigits = '123456789.000000';
471
472                         // round parameter can be negative for decimal, precision has to be postive
473                         function formatNumber(n, sep, dec, precision) {
474                                 var majorDigits;
475                                 var minorDigits;
476                                 var formattedMajor = '';
477                                 var formattedMinor = '';
478
479                                 var nArray = n.split('.');
480                                 majorDigits = nArray[0];
481                                 if(nArray.length < 2) {
482                                         minorDigits = 0;
483                                 } else {
484                                         minorDigits = nArray[1];
485                                 }
486
487                                 // handle grouping
488                                 if(sep.length > 0) {
489                                         var strlength = majorDigits.length;
490
491                                         if(strlength > 3) {
492                                                 var offset = strlength % 3; // find how many to lead off by
493
494                                                 for(j=0; j<offset; j++) {
495                                                         formattedMajor += majorDigits[j];
496                                                 }
497
498                                                 tic=0;
499                                                 for(i=offset; i<strlength; i++) {
500                                                         if(tic % 3 == 0 && i != 0)
501                                                                 formattedMajor += sep;
502
503                                                         formattedMajor += majorDigits.substr(i,1);
504                                                         tic++;
505                                                 }
506                                         }
507                                 } else {
508                                         formattedMajor = majorDigits; // no grouping marker
509                                 }
510
511                                 // handle decimal precision
512                                 if(precision > 0) {
513                                         for(i=0; i<precision; i++) {
514                                                 if(minorDigits[i] != undefined)
515                                                         formattedMinor += minorDigits[i];
516                                                 else
517                                                         formattedMinor += '0';
518                                         }
519                                 } else {
520                                         // we're just returning the major digits, no decimal marker
521                                         dec = ''; // just in case
522                                 }
523
524                                 return formattedMajor + dec + formattedMinor;
525                         }
526
527                         function setSigDigits() {
528                                 var sym = document.getElementById('symbol').value;
529                                 var thou = document.getElementById('default_number_grouping_seperator').value;
530                                 var dec = document.getElementById('default_decimal_seperator').value;
531                                 var precision = document.getElementById('sigDigits').value;
532                                 //umber(n, num_grp_sep, dec_sep, round, precision)
533                                 var newNumber = sym + formatNumber(exampleDigits, thou, dec, precision, precision);
534                                 document.getElementById('sigDigitsExample').value = newNumber;
535                         }
536 eoq;
537                 return $out;
538         }
539
540         ////    END NUMBER DISPLAY FORMATTING CODE
541         ///////////////////////////////////////////////////////////////////////////
542
543         ///////////////////////////////////////////////////////////////////////////
544         ////    NAME DISPLAY FORMATTING CODE
545         /**
546          * get's the Name format macro string, preferring $current_user
547          * @return string format Name Format macro for locale
548          */
549         function getLocaleFormatMacro($user=null) {
550                 $returnFormat = $this->getPrecedentPreference('default_locale_name_format', $user);
551                 return $returnFormat;
552         }
553
554         /**
555          * returns formatted name according to $current_user's locale settings
556          *
557          * @param string firstName
558          * @param string lastName
559          * @param string salutation
560          * @param string title
561          * @param string format If a particular format is desired, then pass this optional parameter as a simple string.
562          * sfl is "Salutation FirstName LastName", "l, f s" is "LastName[comma][space]FirstName[space]Salutation"
563          * @param object user object
564          * @param bool returnEmptyStringIfEmpty true if we should return back an empty string rather than a single space
565          * when the formatted name would be blank             
566          * @return string formattedName
567          */
568         function getLocaleFormattedName($firstName, $lastName, $salutationKey='', $title='', $format="", $user=null, $returnEmptyStringIfEmpty = false) {
569                 global $current_user;
570                 global $app_list_strings;
571                 
572                 if ( $user == null ) {
573                     $user = $current_user;
574                 }
575
576                 $salutation = $salutationKey;
577                 if(!empty($salutationKey) && !empty($app_list_strings['salutation_dom'][$salutationKey])) {
578                         $salutation = (!empty($app_list_strings['salutation_dom'][$salutationKey]) ? $app_list_strings['salutation_dom'][$salutationKey] : $salutationKey);
579                 }
580
581         //check to see if passed in variables are set, if so, then populate array with value,
582         //if not, then populate array with blank ''
583                 $names = array();
584                 $names['f'] = (empty($firstName)        && $firstName   != 0) ? '' : $firstName;
585                 $names['l'] = (empty($lastName) && $lastName    != 0) ? '' : $lastName;
586                 $names['s'] = (empty($salutation)       && $salutation  != 0) ? '' : $salutation;
587                 $names['t'] = (empty($title)            && $title               != 0) ? '' : $title;
588
589                 if(empty($format)) {
590                         $this->localeNameFormat = $this->getLocaleFormatMacro($user);
591                 } else {
592                         $this->localeNameFormat = $format;
593                 }
594
595                 // parse localeNameFormat
596                 $formattedName = '';
597                 for($i=0; $i<strlen($this->localeNameFormat); $i++) {
598                         $formattedName .= array_key_exists($this->localeNameFormat{$i}, $names) ? $names[$this->localeNameFormat{$i}] : $this->localeNameFormat{$i};
599                 }
600
601                 $formattedName = trim($formattedName);
602         if (strlen($formattedName)==0) {
603             return $returnEmptyStringIfEmpty ? '' : ' ';
604         }
605
606                 if(strpos($formattedName,',',strlen($formattedName)-1)) { // remove trailing commas
607                         $formattedName = substr($formattedName, 0, strlen($formattedName)-1);
608                 }
609                 return trim($formattedName);
610         }
611
612         /**
613          * outputs some simple Javascript to show a preview of Name format in "My Account" and "Admin->Localization"
614          * @param string first First Name, use app_strings default if not specified
615          * @param string last Last Name, use app_strings default if not specified
616          * @param string salutation Saluation, use app_strings default if not specified
617          * @return string some Javascript
618          */
619         function getNameJs($first='', $last='', $salutation='', $title='') {
620                 global $app_strings;
621
622                 $salutation     = !empty($salutation) ? $salutation : $app_strings['LBL_LOCALE_NAME_EXAMPLE_SALUTATION'];
623                 $first          = !empty($first) ? $first : $app_strings['LBL_LOCALE_NAME_EXAMPLE_FIRST'];
624                 $last           = !empty($last) ? $last : $app_strings['LBL_LOCALE_NAME_EXAMPLE_LAST'];
625                 $title          = !empty($title) ? $title : $app_strings['LBL_LOCALE_NAME_EXAMPLE_TITLE'];
626
627                 $ret = "
628                 function setPreview() {
629                         format = document.getElementById('default_locale_name_format').value;
630                         field = document.getElementById('nameTarget');
631
632                         stuff = new Object();
633
634                         stuff['s'] = '{$salutation}';
635                         stuff['f'] = '{$first}';
636                         stuff['l'] = '{$last}';
637                         stuff['t'] = '{$title}';
638
639                         var name = '';
640                         for(i=0; i<format.length; i++) {
641                 if(stuff[format.substr(i,1)] != undefined) {
642                     name += stuff[format.substr(i,1)];
643                                 } else {
644                     name += format.substr(i,1);
645                                 }
646                         }
647
648                         //alert(name);
649                         field.value = name;
650                 }
651
652                 setPreview();";
653
654                 return $ret;
655         }
656         ////    END NAME DISPLAY FORMATTING CODE
657         ///////////////////////////////////////////////////////////////////////////
658     
659     /** 
660      * Attempts to detect the charset used in the string
661      *
662      * @param  $str string
663      * @return string
664      */
665     public function detectCharset(
666         $str
667         )
668     {
669         if ( function_exists('mb_convert_encoding') )
670             return mb_detect_encoding($str,'ASCII,JIS,UTF-8,EUC-JP,SJIS,ISO-8859-1');
671         
672         return false;
673     }
674 } // end class def
675
676 ?>