Transliteration - Cyrillic to Latin and vice versa - Joomla! Forum - community, help and support


hi,

some languages ​​use both - latin , cyrillic alphabets, sites required have transliteration (cyrillic/latin schwitcher links), articles , menus can transliterated.

for example, site has 2 links: cyrillic|latin - and, when click on cyrillic - articles , menus transliterated.

i have 1 plugin doing this. operate on mos buffer modifying before shown. not requires joomfish, sr-rs (cyrillic) & sr-yu (latin) versions of languages installed. if site language serbian cyrillic transliterate latin cyrillic. if site language serbian latin transliterate cyrillic latin.

the problem plugin works on joomla! 1.5 , there no version 2.5. it simple plugin, don't know how adapt plugin joomla! 2.5.


transliteration.php:

code: select all

<?php
defined( '_jexec' ) or die( 'restricted access' );
jimport( 'joomla.plugin.plugin' );
class plgsystemtransliteration extends jplugin
    function plgsystemtransliteration( &$subject, $config )
    {
        parent::__construct( $subject, $config );
    }
function onafterrender()
   {
    //replace src links
          $base   = juri::base(true).'/';
    $buffer = jresponse::getbody();
         $app =& jfactory::getapplication();
         if($app->getname() != 'site') {
    return true;
    }
     require_once ( jpath_base .ds.'plugins'.ds.'system'.ds.'transliteration.class.php' );
    $lang = $app->getcfg('lang');
         $this->_db = jfactory::getdbo();
    $plugin =& jpluginhelper::getplugin('system', 'transliteration');   
    $this->_params = new jparameter( $plugin->params );
    $this->_params->set( 'variant', 'lat2cyr' );
          $base   = juri::base(true).'/';
    $buffer = jresponse::getbody();
    jimport('joomla.mail.helper');
    $lang = &jfactory::getlanguage();
         if( $lang->get('tag') != 'sr-rs' ){
      $buffer = str_replace( '{l}', '', $buffer );
      $buffer = str_replace( '{/l}', '', $buffer );
    }
    else{
                $buffer = transliteration::preveditext( $buffer, $this->_params);
         }
    jresponse::setbody($buffer);
    return true;
    }
}


transliteration.class.php:

code: select all

<?php
defined( '_jexec' ) or die( 'restricted access' );
class transliteration{

   function preveditext( $text, $params){
      global $database;
      $variant = $params->get( 'variant', 'lat2cyr' );
      $special = str_replace( "\r", '', $params->get( 'special' ) );
      $specialwords = explode( "\n", $special );
      //      $specialwords = array();
      $specialwords[]='windows';
      $specialwords[]='microsoft';
      $specialwords[]='linux';
      $specialwords[]='philips';
      $specialwords[]='[youtube]';
      $brojac = 0;
      $regex = '%\{l\}(.+)\{/l\}%imu';
      if( preg_match_all( $regex, $text, $mmatches, preg_set_order ) ){
         foreach( $mmatches $match ){
            $find[] = $match[0];
            $replace[] = "***** manual signatures not allowed *****".sprintf( "[%9100d]", ($brojac++) );
         }
      }
      foreach( $specialwords $word ){
            $find[] = $word;            
            $replace[] = "***** manual signatures not allowed *****".sprintf( "[%9100d]", ($brojac++) );
      }
      $text = str_replace( $find, $replace, $text );
      if( $variant == 'auto' ){
         $variant = transliteration::prepoznajvarijantu( $text );
      }
      if( $variant == 'cyr2lat' ){
         $text = transliteration::prevedicirilicu( $text );
      }
      else{
         $text = transliteration::prevedilatinicu( $text );
      }
      $text =  str_replace( $replace, $find, $text );
      $text = str_replace( '{l}', '', $text );
      $text = str_replace( '{/l}', '', $text );
      return $text;
   }
   function prepoznajvarijantu( $text ){
      foreach( transliteration::$cirilica $cyr ){
         if( !in_array( $cyr, transliteration::$latinica ) && mb_strpos( $text, $cyr ) !== false ){
            return 'cyr2lat';
         }
      }
      return 'lat2cyr';
   }
   function prevedicirilicu( $text ){
      return transliteration::cyr2latin( $text );
   }
   function prevedilatinicu( $text ){
      if( strpos( $text, '<' ) === false ){
         return transliteration::latin2cyr( $text );
      }
      $patterns['tag']='<[^>]+>';
      $patterns['text']='[^<]+';
      $pattern = '/('.join('|', $patterns).')/';
      $tagpatterns = '%(title|alt)\s*=\s*"([^"]+)"%i';
      $linkpatterns = '%href\s*=\s*"([^"]+)"%i';
      preg_match_all( $pattern, $text, $matches, preg_set_order);
      $ret = '';
      $previous = '';
      $next = '';
      $ukupno = count( $matches );
      for( $tekuci=0; $tekuci < $ukupno; $tekuci++ ){
         $match = &$matches[$tekuci];
         if( stripos( $match[0], '<style') === 0  ){
            while( $tekuci < $ukupno && stripos( $match[0], '</style' ) !== 0 ){
               $ret .= $match[0];
               $tekuci++;
               $match = &$matches[$tekuci];
            }
            $ret .= $match[0];
         }
         else if( stripos( $match[0], '<script') === 0  ){
            while( $tekuci < $ukupno && stripos( $match[0], '</script' ) !== 0 ){
               $ret .= $match[0];
               $tekuci++;
               $match = &$matches[$tekuci];
            }
            $ret .= $match[0];
         }
         else if( stripos( $match[0], '<textarea') === 0  ){
            while( $tekuci < $ukupno && stripos( $match[0], '</textarea' ) !== 0 ){
               $ret .= $match[0];
               $tekuci++;
               $match = &$matches[$tekuci];
            }
            $ret .= $match[0];
         }
         else if( stripos( $match[0], '<options') === 0  ){
            while( $tekuci < $ukupno && stripos( $match[0], '</options' ) !== 0 ){
               $ret .= $match[0];
               $tekuci++;
               $match = &$matches[$tekuci];
            }
            $ret .= $match[0];
         }
         else if( strpos( $match[0], '<' ) === 0 ){ //tag
            preg_match_all( $tagpatterns, $match[0], $attribs, preg_set_order );
            if( count( $attribs ) ){
               foreach( $attribs $str ){
                  //                  print_r( $str );
                  $match[0] = str_replace( $str[0], $str[1].'="'.transliteration::latin2cyr( $str[2] ).'"', $match[0] );
               }
            }
            $ret .= $match[0];
         }
         else if( jmailhelper::isemailaddress( $match[0] ) ){
            $ret .= $match[0];
         }
         else if( transliteration::isurl( $match[0] ) ){
            $ret .= transliteration::zameniurl( $match[0] );
         }
         else{
            $ret .= transliteration::latin2cyr( $match[0] );
         }
      }
      return $ret;
   }
   function latin2cyr( $text ){
      $brojac = 0;
      foreach( $ttable =  get_html_translation_table( html_entities ) $key=>$value ){
         if( $key != '<' && $key != '>' && $key != '"' && $key != '&' ){
            $find[] = $value;
            $replace[] = "***** manual signatures not allowed *****".sprintf( "[%0100d]", ($brojac++) );
         }
      }
      $text = str_replace( $find, $replace, $text );
      $text = html_entity_decode( $text, ent_compat, "utf-8" );
      $text = str_replace( transliteration::$latinica, transliteration::$cirilica, $text );
      $text =  str_replace( $replace, $find, $text );
      return $text;
   }
   function cyr2latin( $text ){
      if( is_object( $text ) ){
         return $text;
      }
      $text = str_replace( transliteration::$cirilica, transliteration::$latinica, $text );
      return $text;
   }
   function html2latin( $text ){
      $htmlentity[]='&scaron;'; $latinica[]='š';
      $htmlentity[]='&scaron;'; $latinica[]='Š';
      $htmlentity[]='&quot;'; $latinica[]='"';
      return str_replace( $htmlentity, $latinica, $text );
   }
   function linkdonas( $text ){
      global $variant, $url;
      if( strpos( $text, 'mailto:' ) === 0 ){
         return $text;
      }
      else if( strpos( $text, 'javascript:' ) === 0 ){
         return $text;
      }
      if( strpos( $text, 'http' ) !== 0 ){
         if( $url[strlen( $url )] != '/' ){
            $text = $url . '/' . $text;
         }
         else{
            $text = $url . $text;
         }
      }
      return 'http://www.transliteration.com/?act=prevedi&variant='.$variant.'&url='.urldecode( $text );
   }
   public static  $latinica = array(/*'dj','dj','dj',*/'lj','nj','dž','lj','lj','nj','nj','dž','dž','a','b','v','g','d','đ','e','ž','z','i','j','k','l','m','n','o','p','r','s','t','ć','u','f','h','c','č','š','a','b','v','g','d','dj','Đ','e','Ž','z','i','j','k','l','m','n','o','p','r','s','t','Ć','u','f','h','c','Č','Š');
   public static  $cirilica = array (/*'Ђ','Ђ','ђ',*/'љ','њ','џ','Љ','Љ','Њ','Њ','Џ','Џ','а','б','в','г','д','ђ','е','ж','з', 'и', 'ј', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'ћ', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'А', 'Б', 'В', 'Г', 'Д', 'Ђ','Ђ', 'Е', 'Ж', 'З', 'И', 'Ј', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'Ћ', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш');
   public static function isurl( $string ){
      $regex = '(https?://|www)([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\s+)?)?)?';
      return preg_match( "@$regex@", $string );
   }
   function zameniurl( $string ){
      $regex = '(https?://|www)([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\s+)?)?)?';
      if( preg_match_all( "@$regex@", $string, $matches, preg_set_order) ){
         foreach( $matches $match ){
            $kontra[] = transliteration::latin2cyr( $match[0] );
            $rekontra[] = $match[0];
         }
      }
      $string = transliteration::latin2cyr( $string );
      $string = str_replace( $kontra, $rekontra, $string );
      return $string;
   }
  }
?>



transliteration.xml:

code: select all

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5.2" type="plugin" group="system">
   <name>system - transliteration</name>
   <author>jsx</author>
   <creationdate>januar 2010</creationdate>
   <license>gnu/gpl http://www.gnu.org/copyleft/gpl.html</license>
   <version>1.0.1</version>
   <description>transliterate latin cyrillic, , cyrillic latin</description>
   <files>
      <filename>transliteration.class.php</filename>
      <filename plugin="transliteration">transliteration.php</filename>
   </files>
   <params>
        <param name="special"
        type="textarea" cols="40" rows="10"
        default=""
        label="specijalne reci"
        description="reci koje se ne preslovljavaju" />
   </params>
</install>

language packs can use custom transliteration function in localise.php.
http://docs.joomla.org/making_a_languag ... calise.php

if packs use not contain any, please contact translation team coordinator these packs.

http://community.joomla.org/translation ... tions.html





Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support