简体   繁体   中英

cakephp multilingual dynamic content from database

I want to develop website with option to select language
at the time I do not know about how to structure my database tables ie either I should add separate fields for each language eg
tbl_posts id, title_en,title_fr,description_en,description_fr,.... or should I get help of google translate at run time
or there is something else easy to do this

secondly I will need to have URLs like
www.domain.com/en/posts/ & www.domain.com/fr/posts/

third what other things should I keep in mind to develop multilingual website. looking for standardized, more optimized, easy manageable and fully dynamic solution.

Cakephp
Step 1: In your lib/Cake/basic.php add function

if (!function_exists('__dbt')) {
    function __dbt($text, $args = null) {
            if($text==null){
                return null;
            }else{
                $languageUse =  Configure::read('Config.language');
                if($languageUse!='en-us' && $languageUse!='eng'){
                    $modelName =  ucfirst($languageUse)."Translation";
                    $model = ClassRegistry::init($modelName);
                    $data = $model->find('first',array('fields'=>array('translation'),'conditions'=>array("text"=>"$text")));
                    if(!empty($data[$modelName]) && $data[$modelName]['translation']!=''){
                        return $data[$modelName]['translation'];//die('1');
                    }else{
                    // Please copy & paste below code from your basic.php __() function   
                    App::uses('I18n', 'I18n');
                    $translated = I18n::translate($text);
                    $arguments = func_get_args();
                    return I18n::insertArgs($translated, array_slice($arguments, 1));
                    }
                }else{
                    // Please copy & paste below code from your basic.php __() function   
                    App::uses('I18n', 'I18n');
                    $translated = I18n::translate($text);
                    $arguments = func_get_args();
                    return I18n::insertArgs($translated, array_slice($arguments, 1));
                }
            }
}

}

Step 2: create table on basis of language you want to use
Note: table name should be prefix locale + _tranlations
example: hin_translations, tha_translations etc.

CREATE TABLE IF NOT EXISTS `hin_translations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `text` text NOT NULL,
  `translation` text NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

In above table add english string in your text column and its translation in translation column.

Step 3: where ever you want to change language string either from database or locale po file just use

__dbt("Write your string here");

:) enjoy your multilingual site

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM