PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/i18n_v2/inc/class.I18NformatString.inc.php

http://flaimo-php.googlecode.com/
PHP | 140 lines | 60 code | 15 blank | 65 comment | 6 complexity | 422544244fa537ed02637eabfcd0b664 MD5 | raw file
  1. <?php
  2. /**
  3. * load base class which takes care of all the other includes via it's autoload function
  4. */
  5. require_once 'class.I18Nbase.inc.php';
  6. /**
  7. * formats strings based on the current locale
  8. * @author Michael Wimmer <flaimo@gmail.com>
  9. * @category flaimo-php
  10. * @example ../www_root/i18n_example_script.php i18n example script
  11. * @license GNU General Public License v3
  12. * @link http://code.google.com/p/flaimo-php/
  13. * @package i18n
  14. * @version 2.3.1
  15. */
  16. class I18NformatString extends I18Nbase {
  17. /**
  18. * @var object holds a I18Ntranslator object
  19. */
  20. protected $translator;
  21. /**
  22. * @var string namespaces needed for translating strings in this class
  23. */
  24. protected $namespace = '';
  25. /**
  26. * @var array holds an array with bad words
  27. */
  28. protected $bad_words;
  29. /**
  30. * @var array holds an array with special words to highlight
  31. */
  32. protected $special_words;
  33. /**
  34. * @param object $locale I18Nlocale
  35. * @return void
  36. * @uses I18Nbase::getI18NfactoryLocale()
  37. * @uses I18Ntranslator
  38. * @uses I18NformatString::$translator
  39. */
  40. public function __construct(&$locale = NULL) {
  41. if (!($locale instanceOf I18Nlocale)) {
  42. $locale =& parent::getI18NfactoryLocale();
  43. } // end if
  44. $this->translator = new I18Ntranslator($this->namespace, $locale);
  45. } // end constructor
  46. /**
  47. * fetches the bad word string from the l10n file
  48. * @return boolean
  49. * @uses I18NformatString::$translator
  50. * @uses I18Nbase::getI18Nsetting()
  51. */
  52. protected function fetchBadWords() {
  53. $tmp = explode(',',$this->translator->getTranslatorLocale()->getL10Nsetting('badwords'));
  54. $tmp = array_map('trim', $tmp);
  55. foreach ($tmp as $word) {
  56. $this->bad_words[$word] = str_repeat(parent::getI18Nsetting('replace_char'), mb_strlen($word));
  57. } // end foreach
  58. unset($tmp);
  59. return (boolean) TRUE;
  60. } // end function
  61. /**
  62. * replaces all bad words in a string with replace-chars
  63. * @param string $text
  64. * @param boolean $exact whether only hole words should be replaced or parts of a string
  65. * @return string
  66. * @uses I18NformatString::$bad_words
  67. * @uses I18NformatString::fetchBadWords()
  68. */
  69. public function wordFilter($text = '', $exact = TRUE) {
  70. if (!isset($this->bad_words)) {
  71. $this->fetchBadWords();
  72. } // end if
  73. if ($exact === TRUE) {
  74. return strtr($text, $this->bad_words);
  75. } // end if
  76. reset($this->bad_words);
  77. while(list($word, $replace) = each($this->bad_words)) {
  78. $text = mb_ereg_replace('/(^|\b)' . $word . '(\b|!|\?|\.|,|$)/i', $replace, $text);
  79. } // end while
  80. return $text;
  81. } // end function
  82. /**
  83. * fetches the special words from the ini file
  84. * @return boolean
  85. * @uses I18NformatString::$special_words
  86. * @uses I18NformatString::$translator
  87. * @uses I18Nbase::getI18Nsetting()
  88. */
  89. protected function fetchSpecialWords() {
  90. $path = parent::getI18Nsetting('locales_path') . '/' . $this->translator->getTranslatorLocale()->getI18Nlocale() . '/words.ini' ;
  91. $this->special_words = parse_ini_file($path, TRUE);
  92. return (boolean) TRUE;
  93. } // end function
  94. /**
  95. * "highlights" special words in a string with abbr/dfn/acronym tags
  96. * @param string $text
  97. * @param string $what abbr/dfn/acronym (if empty all 3 will be highlighted)
  98. * @return string
  99. * @uses I18NformatString::$special_words
  100. * @uses I18NformatString::fetchSpecialWords()
  101. * @uses I18Nbase::isFilledString()
  102. * @uses I18Nbase::getI18Nuser()
  103. */
  104. public function highlightSpecialWords($string = '', $what = '') {
  105. if (parent::isFilledString($string) == FALSE ||
  106. parent::getI18Nuser()->getHighlightSpecialWords() === 0) {
  107. return $string;
  108. } // end if
  109. if (!isset($this->special_words)) {
  110. $this->fetchSpecialWords();
  111. } // end if
  112. $loop = (array_key_exists($what, $this->special_words)) ? array($what) : array_keys($this->special_words);
  113. foreach ($loop as $what) {
  114. foreach ($this->special_words[$what] as $list_name => $list_value) {
  115. $valuearray = explode(' | ', $list_value);
  116. $list_name = quotemeta($list_name);
  117. //$valuearray[0] = htmlentities($valuearray[0]);
  118. $string = mb_ereg_replace($list_name, '<'.$what.' title="' . $valuearray[0] . '" xml:lang="' . $valuearray[1] . '">' . $list_name . '</'.$what.'>', $string);
  119. //$string = preg_replace('*' . $list_name . '*', '<'.$what.' title="' . $valuearray[0] . '" xml:lang="' . $valuearray[1] . '">' . $list_name . '</'.$what.'>', $string , 1);
  120. } // end foreach
  121. } // end foreach
  122. return $string;
  123. } // end function
  124. } // end class I18NformatString
  125. ?>