Internationalization: Poll: Use Smarty or Moodle

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply

Internationalization

Go With Smarty
1
33%
Go with Moodle
2
67%
 
Total votes: 3

User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Internationalization: Poll: Use Smarty or Moodle

Post by NewfieBilko »

Moodle has internationization build into it already.. Smarty, it isn't too hard to find how to internationalize.

My boss wants me to intergrate Smarty INTO mooodle. How should i go about internationizion? Should I use the dynamic string swapper of moodler, or build in smarty templates for internationization?

PS. i see that the creator of moodle, is buildling moodle 2.0 which is supposed to be built with smartyfeatures included, some templating? I am working on the same ideal... Looking for guidence
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

If people are getting confused by moodle and smarty , and know of better internationization ideals then go ahead and rant
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

C'mon guys.. there must be some way to internationalize Smarty.. I've got IntSmarty which seems to do some work, but is this a truly internationization system?

Code: Select all

<?php
<?php
    
    require_once("Smarty.class.php");

    /* This class is (c) John Coggeshall, all rights reserved. It may be
       used in accordance to the FreeBSD license. You can find a copy
       of this license at:
       
       http://www.freebsd.org/copyright/freebsd-license.html
       
       Or by e-mailing john@coggeshall.org
       
       for the latest version of IntSmarty, visit http://www.coggeshall.org/
    */

        
    class IntSmarty extends Smarty {

        var $default_lang = "en-us";
        var $lang_path    = "/tmp/languages/";
        var $version       = "0.7";

        var $a_languages;
        var $cur_language;
        var $translation;
        var $translation_size;
        
        function IntSmarty($lang = NULL) {
            
             parent::Smarty();
            
             $this->register_prefilter("smarty_lang_prefilter");
             $this->register_function("i18nfile", "smarty_function_i18nfile");
            
             $this->a_languages = $this->_determineLangs();
            
            if(! is_null($lang)) {
                 array_unshift($this->a_languages , $lang);    
            }
            
            if(isset( $this->a_languages[0])) {
                 $this->cur_language = $this->a_languages[0];
            } else {
                 $this->cur_language = NULL;
            }
            
            foreach( $this->a_languages as $lang) {
                
                 $this->cur_language = $lang;
                if( $this->loadLanguageTable($this->cur_language)) {
                    break;
                }
                
            }
                        
        }   
        
        function fetch($file, $cache = NULL, $compile_id = "", $display = NULL) {
            
             $cid = $this->cur_language .$compile_id;
            return parent::fetch($file, $cache, $cid, $display);
            
        }
        
        function _determineLangs() {
                        
            @ preg_match_all("/([a-z\-]*)?[,;]+/i",
                            $_SERVER['HTTP_ACCEPT_LANGUAGE'],
                            $matches);
        
            
            return (count($matches[1])) ? $matches[1] : array($this->default_lang);
            
        }
        
        function loadLanguageTable($language) {
           
             $filename = "{$this->lang_path}$language.php";
            
            if( file_exists($filename)) {
             
                @require_once( $filename);
                
                if( is_array($__LANG)) {
                    
                     $this->translation = $__LANG;
                     $this->cur_language = $language;
                     $this->translation_size = count($this->translation);
                     $this->assign("lang", $this->cur_language );
                    
                    unset( $__LANG);
                    return true;  
                }
                       
            }
            
            return false;
            
        }
        
        function saveLanguageTable() {
           
            if( count($this->translation ) != $this->translation_size) {

                 $filename = "{$this->lang_path}{$this->cur_language}.php" ;
                 $code = '<?php $__LANG = '.var_export($this->translation, true).'; ?>';
                 $fr = fopen($filename, "w");
                
                if(! $fr) {
                    return false;
                }
                    
                 fputs($fr, $code);
                 fclose($fr);
            
            }
            
            return true;
        }
        
        function assignLang($var, $value = NULL) {
         
            if( is_string($value)) {
                
                 $hash = md5($value);   
                
                if( key_exists($hash, $this->translation )) {
                
                     $value = utf8_decode($this->translation[$hash]);
                    
                }  else {
                    
                     $this->translation[$hash] = $value;
                    
                }
                
                 $this->assign($var, $value);
            
            }
            
        }
        
        /* For Smarty 2.6.x */
        function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path=null) {
        
            if (file_exists(SMARTY_DIR . $this->compiler_file )) {
            
                require_once( SMARTY_DIR . $this->compiler_file );
            
            } else {
            
                 // use include_path
                
                 require_once($this->compiler_file);
            
            }
        
             $smarty_compiler = new $this->compiler_class;
            
             $smarty_compiler->template_dir       = $this->template_dir;
             $smarty_compiler->compile_dir        = $this->compile_dir;
             $smarty_compiler->plugins_dir        = $this->plugins_dir;
             $smarty_compiler->config_dir         = $this->config_dir;
             $smarty_compiler->force_compile      = $this->force_compile;
             $smarty_compiler->caching            = $this->caching;
             $smarty_compiler->php_handling       = $this->php_handling;
             $smarty_compiler->left_delimiter    = $this->left_delimiter;
             $smarty_compiler->right_delimiter   = $this->right_delimiter;
             $smarty_compiler->_version           = $this->_version;
             $smarty_compiler->security           = $this->security;
             $smarty_compiler->secure_dir         = $this->secure_dir;
             $smarty_compiler->security_settings = $this->security_settings;
             $smarty_compiler->trusted_dir        = $this->trusted_dir;
             $smarty_compiler->_reg_objects       = &$this->_reg_objects;
             $smarty_compiler->_plugins           = &$this->_plugins;
             $smarty_compiler->_tpl_vars          = &$this->_tpl_vars;
             $smarty_compiler->default_modifiers = $this->default_modifiers;
             $smarty_compiler->compile_id         = $this->_compile_id;
             $smarty_compiler->_config            = $this->_config;
             $smarty_compiler->request_use_auto_globals  = $this->request_use_auto_globals;
             $smarty_compiler->parent_inst        = &$this;
            
             $smarty_compiler->_cache_serial = null;
             $smarty_compiler->_cache_include = $cache_include_path;
        
             $_results = $smarty_compiler->_compile_file($resource_name, $source_content, $compiled_content);
        
            if ($smarty_compiler->_cache_serial) {
        
                 $this->_cache_include_info = array(
        
                     'cache_serial'=>$smarty_compiler->_cache_serial,
                     'plugins_code'=>$smarty_compiler->_plugins_code,
                     'include_file_path' => $cache_include_path);
        
            } else {
        
                 $this->_cache_include_info = null;
        
            }
        
            return $_results;
        
        }
        
        /* For Smarty 2.5.x */
        function _compile_template($tpl_file, $template_source, &$template_compiled) {
            
            if( file_exists(SMARTY_DIR.$this->compiler_file)) {
                require_once SMARTY_DIR.$this->compiler_file;            
            } else {
                 // use include_path
                 require_once $this->compiler_file;
            }
    
             $smarty_compiler = new $this->compiler_class;
    
             $smarty_compiler->template_dir       = $this->template_dir;
             $smarty_compiler->compile_dir        = $this->compile_dir;
             $smarty_compiler->plugins_dir        = $this->plugins_dir;
             $smarty_compiler->config_dir         = $this->config_dir;
             $smarty_compiler->force_compile      = $this->force_compile;
             $smarty_compiler->caching            = $this->caching;
             $smarty_compiler->php_handling       = $this->php_handling;
             $smarty_compiler->left_delimiter    = $this->left_delimiter;
             $smarty_compiler->right_delimiter   = $this->right_delimiter;
             $smarty_compiler->_version           = $this->_version;
             $smarty_compiler->security           = $this->security;
             $smarty_compiler->secure_dir         = $this->secure_dir;
             $smarty_compiler->security_settings = $this->security_settings;
             $smarty_compiler->trusted_dir        = $this->trusted_dir;
             $smarty_compiler->_reg_objects       = &$this->_reg_objects;
             $smarty_compiler->_plugins           = &$this->_plugins;
             $smarty_compiler->_tpl_vars          = &$this->_tpl_vars;
             $smarty_compiler->default_modifiers = $this->default_modifiers;
             $smarty_compiler->compile_id         = $this->_compile_id;
             $smarty_compiler->parent_inst        = &$this;
            
            if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled)) {
                return true;
            } else {
                 $this->trigger_error($smarty_compiler->_error_msg);
                return false;
            }
            
        }
        
    }
    
    function smarty_lang_prefilter($content, &$smarty) {
        
        $inst = &$smarty->parent_inst;
        
        $ldq = preg_quote($inst->left_delimiter, '!');
        $rdq = preg_quote($inst->right_delimiter, '!');
        
        /* Grab all of the tagged strings */
        preg_match_all("!{$ldq}l{$rdq}(.*?){$ldq}/l{$rdq}!s", $content, $match);
        
        foreach( $match[1] as $str) {
            
             $q_str = preg_quote($str);
             $hash = md5($str);
            
             /* Do we have a translation for this string? */
            
             if(key_exists($hash, $inst->translation)) {
            
                 /* Replace all occurances of this string with its translation */    
                 $content = preg_replace("!{$ldq}l{$rdq}$q_str{$ldq}/l{$rdq}!s",
                                         utf8_decode($inst->translation [$hash]), $content);  
                
            }  else {
                
                 /* If not, add it as in its current form to our table,
                   which will be saved for a translator to fix */
                 $inst->translation[$hash] = $str;
                
            }
            
            
        }
        
        /* Strip off the tags now that the strings have been replaced */
        $content = preg_replace("!{$ldq}l{$rdq}(.*?){$ldq}/l{$rdq}!s",
                                 "\${1}", $content);
              
        return $content;   
            
    }
    
    function smarty_function_i18nfile($params, &$smarty) {
     
        if(empty( $params['file'])) {
             $smarty->trigger_error("i18nfile: missing 'file' parameter");
            return;
        }
        
        $filepath = $params['file'];
        $filename = basename($filepath);
        $path = dirname($filepath);
        
        if(isset( $params['lang'])) {
             $language = $params['lang'];
        } else {
             $language = $smarty->cur_language;
        }
        
        $newfile = $path . DIR_SEP . $language . DIR_SEP . $filename;
        return $newfile;
    }

?>
?>
And then you have Moodle, which already has a built in STRING selector for internationalization. It assigns parts of the site as strings then swaps. Wheere this IntSmarty assigns limited blocks to a table and then refreshs out? One thing about IntSmarty is that I can't even find where the actual job of Translation is being proformed... arg :twisted:
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

hmmm, would there be any way to take the internationization of moodle and use it for the smarty system?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As with almost anything, there is the potential to do it.. but how much time and effort you can put into it is finite.

It's probably not very difficult to incorporate the international code from Moodle into Smarty..
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

Yea.. I would agree.. just was looking for suggestions


Because I have been implimenting John's IntSmarty , but I can't seem to even get this working yet.... Smarty seems hard to internationalize
Post Reply