Translate module

Post to discuss about the extension translate

More info in the extension link

Any doubts/bugs report here

Great!

This will really come in handy when I get around to translating my app.

Thanks. :)

I have added a default language to the module to speed it up. Maybe you want to add that feature. Almost all messages to translate are written in english and don’t have to be translated:




<?php


// module




    static $defaultLanguage;


    // ...


    static function missingTranslation($sender){


    	if (self::getLanguage() === self::$defaultLanguage)

    		return;


        // ....

    }


// usage:


TranslateModule::$acceptedLanguages = array('de' => 'German');

TranslateModule::$defaultLanguage = 'en';

TranslateModule::setLanguage('en');




Changed isset -> !empty in line 17 of the controller (if(isset($_POST[‘MissingTranslations’])){) because $_POST[‘MissingTranslations’] contains an empty string when I clicked the link and nothing had to be translated.

By default, if no language is set by the user or the one set is the same as the source language, it sets to null, that speed up the application (it wont try to translate)

you could also use forceTranslation setting to translate from sourceMessage to language even if its the same

As for the other issue, thank you for the fix, soon I’ll be uploading it with the corrections and probably a couple new functionalities.

Anyway, thanks for the reports

Cheers

Hi all,

This is my idea, when its default language set in english so it will be automatically insert into MessageSource table if doesn’t exist and it will be automatically translated by google translate, so you don’t need to translate it manually one by one. To do that you must edit TranslateModule.php and add this code




static function google_translated( $text, $destLang , $srcLang = 'en' ) {

		

		$text = urlencode( $text );

		$destLang = urlencode( $destLang );

		$srcLang = urlencode( $srcLang );

		

		$trans = @file_get_contents( 

		"http*s://www.googleapis.com/language/translate/v2?key=[PUT_YOUR_GOOGLE_TRANSLATE_KEY_HERE]&q=$text&source=$srcLang&target=$destLang"

		); //remove * mark

		$json = json_decode( $trans );

		return $json->data->translations[0]->translatedText;

	}



and edit on static function missingTranslation with this code




static function missingTranslation($sender){

        Yii::import('translate.models.MessageSource');

        $attributes=array('category'=>$sender->category,'message'=>$sender->message);

        if(($model=MessageSource::model()->find('message=:message AND category=:category',$attributes))===null){

            $model=new MessageSource();

            $model->attributes=$attributes;

            if(!$model->save()){

                return;

            }

        }

		

        if($model->id) {

			Yii::import('translate.models.Message');

			//save in english

			$attributes=array('id'=>$model->id,'language'=>'en','translation'=>$sender->message);

			if(($model1=Message::model()->find('id=:id AND translation=:translation AND language=:language',$attributes))===null){

				$model1=new Message();

				$model1->attributes=$attributes;

				if(!$model1->save()){

					return;

				}

			}

			

			//save in dest lang

			if ($sender->language<>'en') {

				$translated=self::google_translated($sender->message,$sender->language);

				$attributes=array('id'=>$model->id,'language'=>$sender->language,'translation'=>$translated);

				if(($model2=Message::model()->find('id=:id AND translation=:translation AND language=:language',$attributes))===null){

					$model2=new Message();

					$model2->attributes=$attributes;

					if(!$model2->save()){

						return;

					}

				}

			}

		}

    }



Thanks Toshi!

I was thinking to do something using google translate but didn’t have time to work on it yet

I’ll definitely add it to the next release

Cheers,

Gustavo

EDIT: Soon I’ll be posting a new version with a complete rewrite of the code

[size="4"] [b]Version 0.2 released

[/b][/size]Complete rewrite of the code to allow better customization

A lot of new funcionalities

Added google translate support

Check it out

Hi Gustavo

I liked the old version but this one gave me the smile right away :) thx for the refactor.

Unfortunetly , I’m here to share that I keep getting the ‘error’ alert

when trying to launch the googleQuery?

http://127.0.0.1:8888/myapp/translate/translate/googletranslate

is this also supposed to work on any localhost set up ? (localhost,127.0.0.1,…)

I couldn’t see anything obvious, so I hope you know about this

Thanks

Hi Tibor

Thanks! I’m glad you liked and was useful to you.

As for the error, it works in localhost normally. I believe that the error is because google can’t translate some text, therefore it returns an error message that is treated as an exception by the translate component.

To avoid the exception, I will soon update the extension . From now on, it will just log the message as an error.

If you have any suggestion or find any bug, let me know.

Cheers,

Gustavo

EDIT:

Just uploaded the new version

Hi Gustavo thanks for the new version

I can see you cleared the Dialog translation , cool.

here’s some feedback after installing the new version, cause I still get the Google translate error alert + something new

on /translate/translate/index

on click "Translate all with google translate" return an alert error for words like "Visibility" and "share your ideas"


On each word translation there is a ‘translate’ Button, when I click that it returns an alert containing this :


<h1>PHP Error [8]</h1>

<p>Trying to get property of non-object (X:\X_Dev\perfony\perfony\iperfony\protected\modules\translate\components\MPTranslate.php:274)</p>

<pre>#0 X:\X_Dev\perfony\perfony\iperfony\protected\modules\translate\components\MPTranslate.php(274): CWebApplication->handleError()

#1 X:\X_Dev\perfony\perfony\iperfony\protected\modules\translate\controllers\TranslateController.php(51): MPTranslate->googleTranslate()

#2 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\web\actions\CInlineAction.php(50): TranslateController->actionGoogletranslate()

#3 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\web\CController.php(300): CInlineAction->runWithParams()

#4 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\web\CController.php(278): TranslateController->runAction()

#5 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\web\CController.php(257): TranslateController->runActionWithFilters()

#6 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\web\CWebApplication.php(277): TranslateController->run()

#7 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\web\CWebApplication.php(136): CWebApplication->runController()

#8 X:\X_Dev\perfony\perfony\yii-1.1.8.r3324\framework\base\CApplication.php(158): CWebApplication->processRequest()

#9 X:\X_Dev\perfony\perfony\iperfony\index.php(13): CWebApplication->run()

</pre>

Thanks for you help

Tibor

Hey Tibor

The new error was caused when updating to new version and is now fixed in github.

Soon I’ll post the new download file.

As for the other error, I spent some time trying but I can’t reproduce it. Please provide more details of your configuration and I’ll run some tests.

Thanks

Hey Tibor

The new error was caused when updating to new version and is now fixed in github.

Soon I’ll post the new download file.

As for the other error, I spent some time trying but I can’t reproduce it. Please provide more details of your configuration and I’ll run some tests.

Also, try calling the queryGoogle method directly by Yii::app()->translate->queryGoogle(‘some message’);

Thanks

Hello,

brand new, I’ve installed “translate” module on a clean 1.1.9 yii framework (+ userGroup module).

1st remak : the auto_increment in table “sourcemessage” wasn’t set. Once corrected, it works fine

2nd : basic pages works fine but after login problems comes. I’ve received :

Include(TranslateBaseController.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

After copying the TranslateBaseController.php into protected/controllers, working fine …

Is that the right way to installe this module ?

Is it a side effect of 1.1.9 version of the framework ? or is it the presence of userGroup module ?

thx for explanations

Shama

Hi Shama

I had the same problem when working with userGroups

It’s that to get all application controllers it imports the controllers folder of each module, but doesn’t import the components folder, where TranslateBaseController is located.

Gustavo

Thanks Gustavo, for the explanation.

that means that every controller in component (sub)directory must be copied into general controller folder ?

another question :

After model generation, I use the giix CRUD generator.

I select the ‘yii user management access control’ option to generate access control code into controller. The goal is to use the userGroups functionnality with the user access system. Generated code for as simple table (no relation …) is :


public function filters() {

	return array(

			'accessControl', 

			);

}


public function accessRules() {

	return array(

			array('allow',

				'actions'=>array('index','view'),

				'roles'=>array('admin', 'core', 5), 

				),

			array('allow', 

				'actions'=>array('minicreate', 'create','update'),

				'roles'=>array('UserCreator'),

				),

			array('allow', 

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

				),

			array('deny', 

				'users'=>array('*'),

				),

			);

}

as written in userGroups user guide, I change the rules by :


array('allow', 

  'actions'=>array('update','admin'), 

  'groups'=>array('admin', 'core', 5), 

),

when running the answer is :

Property « CAccessRule.groups » is undefined.

???

I’ve missed something ?

Shama

Shama was sleeping … (RTFM)

just declare with correct filter’s string :


	

public function filters() {

	return array(

		'userGroupsAccessControl', 

	);

}

:-X

for other candidate, I’ve patched the default Giix rules template. So Giix generates directly the right code ;D . The file is in :


BASEYIIDIR/protected/extensions/giix-core/giixCrud/templates/default/auth/auth_filter_default.php

enjoy !

Shama

hi!

I have yii-user module, and until I setup translate module it was shown in my language. Now it seems translate somehow forces other modules to appear in default english language. can it be somehow adjusted that modules that have their own separate translation, appear in the selected language?

Wow! I’m going check this out when I’m on my pc.

Question: why don’t you use variables for the base instead of straight language sentences in the views?

I’m thinking that too many apps have English that other language speakers have to deal with.

Hi guys,

I wonder why this line


'onMissingTranslation' => array('TranslateModule', 'missingTranslation'),

will cause all the pages become blank, But the pages ok once remove the line??


  'components'=>array(

        //define the class and its missingTranslation event

        'messages'=>array(

            'class'=>'CDbMessageSource',

            'onMissingTranslation' => array('TranslateModule', 'missingTranslation'),  <--- this line

        ),

thanks man.