Translation cakephp not working - cakephp

I'm trying to implement a translation in Cakephp but isn't working and don't show me any erros.
I have this HTML in a element
<a href="/sites/pages/servicos" target="_blank">
<span class="title">Serviços</span>
<div class="description"><?php __('o que fazemos') ?></div>
</a>
In App Controller inside beforeFilter():
Configure::write('Config.language', 'eng');
In my folder locale/eng/LC_MESSAGES/default.po I have this:
msgid "o que fazemos"
msgstr "What we do"
But isn't working...
Thanks

I think you're just forgot "echo"
<?php echo __('o que fazemos'); ?>

Have you correctly generated the i18n files with the ./cake i18n command?
Use PoEdit to edit your translate files, instead of doing it by hand if you've done so.
http://poedit.net/

First of all, Did you generate the default.pot file by typing
app\console\cake followed by i18n extract after having the full file in _() format?
Second, why did you put the Configure::write('Config.language',
'eng'); in App Controller instead of app\Config\core.php
(recommendation).
And as +JazzCat said as it is a .po file it is highly recommended to use poedit.
PS: you can set the language in AppController using session:
$this->Session->write('Config.language', 'en');

Related

cakephp custom flash layout error

i have looked at the documentatioin of cakephp 2.5.4 about custom flash messages and some other tutorials on how to make this but its not working
i have error.ctp and success.ctp under elements folder
error.ctp
<div id="error-flash">
<?php echo h($message); ?>
</div>
success.ctp
<div id="success-flash">
<?php echo h($message); ?>
</div>
and in controller i call them like this
$this->Session->setFlash(__(' file successfully uploaded.','success',array('class'=>"flash_msg_ok")));
$this->Session->setFlash(__('Upload Failed','error',
array("class" => "flash_msg_error")));
my chrome developer tools still shows the default flash generated.
its not rendering the element or applying the specified classes
i've also tried this
$this->Session->setFlash('success','default',array('class'=> 'success'))
still nothing.
of note is that am using custom layout and css for the page.
what am i missing here?
There are a couple of issues..
First, you've accidentally put the other function variables inside the translation string. You can fix the call as follows (note closing bracket moved to end of message string):
$this->Session->setFlash(__(' file successfully uploaded.'),'success',array('class'=>"flash_msg_ok"));
$this->Session->setFlash(__('Upload Failed'),'error',
array("class" => "flash_msg_error"));
Second, you'll need to add the class to your custom error element file (the cakephp docs neglect to mention this!). For example:
<div id="error-flash" class="<?php echo $class; ?>">
<?php echo h($message); ?>
</div>
Hope that helps!

Cakephp 2.4 isn't reading .po files

I created the following file :
/Locale/fra/LC_MESSAGES/default.po
Content:
msgid "delete"
msgstr "effacer"
I set the the default language to fra in core.php
Configure::write('Config.language', 'fra');
when i use
<h3><?php echo __("delete"); ?></h3>
the output should be "effacer" which isn't the case
I tried importing i18n class and setting i10n manually but the issue persist
App::import('I18n', 'i18n');
$I18n =& I18n::getInstance();
$I18n->l10n->get($this->params['lang']);
When i check the persistent cache "myapp_cake_core_default_fra" i find an empty array
Set this language code with language parameter
Configure::write('Config.language','en_Us');

CakePHP 2: Localization&Internalization for function timeAgoInWords()

My problem is that I can not translate a date from English to Russian.
I followed this guides:
Internationalization & Localization CakePHP
I18N shell
I wrote in AppController.php this code:
function beforeFilter() {
parent::beforeFilter();
setlocale(LC_ALL, "ru_RU.utf8");
Configure::write('Config.language', 'rus');
CakeSession::write('Config.language', 'rus');
}
I added a folder "Locale\rus\LC_MESSAGES" and place inside the file default.po.
BTW i extracted all messages to the single file.
But in fact:
<?php echo $this->Time->timeAgoInWords($feedback['Feedback']['created']); ?>
Did nothing.
<?php echo __d('default', $this->Time->timeAgoInWords($feedback['Feedback']['created']), true); ?>
Translates only string 'just now' to russian language, but other time formats still in English.
Examples of translates from default.po you can see below:
success
#: Lib\Cake\Utility\CakeTime.php:842
#: Utility\CakeTime.php:842
msgid "just now"
msgstr "translate"
fail
#: Lib\Cake\Utility\CakeTime.php:829
#: Utility\CakeTime.php:829
msgid "%d week"
msgid_plural "%d weeks"
msgstr[0] "%d translate"
msgstr[1] "%d translate"
I can't understand what i'm doing wrong.
For translating CakePHP core strings like those in Lib\Cake\Utility folder you need to use a different translation domain. The default domain for your views would be 'default' but for CakePHP core strings it is 'cake'.
Instead of 'default.po' you need to provide 'cake.po'. Otherwise your translations will have no effect.

AppError 404 equivalent in cake 2.0

I've recently upgraded my 1.3 cake app to 2.0, and I'm trying to redo my app_error code.
With Cake 1.3, it was simply a case of creating an app_error.php file, putting it in my app root, and overriding the built-in error404() and missingController() actions.
Here is my old 1.3 /app/app_error.php file: http://pastebin.com/beWZD9PJ
It had some code that kicked in when someone arrived at the site with a predefined 'alias' URL, and then it redirected them accordingly.
I just need this to work in Cake2.0, and I see the manual is telling me its all changed, but I can't find a specific case like this. Can anyone help me out, so the error404 code kicks in?
Many thanks
4xx and 5xx errors in CakePHP 2.0 ar now exceptions, thus you need to customize your Exception renderer or handler. Read from this section to the bottom http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer
You can throw a 404 exception with the following code in your controller in Cake 2:
throw new NotFoundException(__('Your error text here'));
Then, you just need to have APP/View/Errors/error400.ctp present with something relevant to display. Cake includes this by default:
<h2><?php echo $name; ?></h2>
<p class="error">
<strong><?php echo __d('cake', 'Error'); ?>: </strong>
<?php printf(
__d('cake', 'The requested address %s was not found on this server.'),
"<strong>'{$url}'</strong>"
); ?>
</p>
<?php
if (Configure::read('debug') > 0 ):
echo $this->element('exception_stack_trace');
endif;
?>
It's simple.
With cakephp 2, you have to add this function in your AppController:
public function appError($error) {
$this->redirect('/');
}
Here, for example we redirect all errors to index page.

Why doesn't my CakePHP flash message appear?

Background: I'm new to CakePHP. I have a small test site (mostly composted of static views and a contact form at the moment) to play with while I learn. Everything worked fine on localhost (Apache on Ubuntu) so I deployed it to a shared hosting account (provided by Lunarpages). I moved the /cake folder out of the normal directory structure (so I could use it for multiple apps) and I reconfigured my webroot's index.php paths accordingly.
Problems:
setFlash messages do not get displayed. Even making a simple view that does nothing other than $this->Session->setFlash('message');. I don't get any error message, the flash just doesn't get displayed
Redirects don't work. For instance, after the contact form is completed I want to $this->redirect( array( 'action' => 'success' ), null, true); but the server throws an error:
Warning (2): Cannot modify header information - headers already sent by (output started at /routetoapp/config/routes.php:40) [CORE/cake/libs/controller/controller.php, line 742]
Everything else seems to work just as it did on localhost - URL rewriting, component loading, model validation. I don't know if my problems are related or separate issues
Troubleshooting so far:
I've tried both 'cake' and 'php' for Configure::write('Session.save', 'val'); but neither made a difference.
My app/tmp folder is writeable.
My layout template has the correct code for displaying flash messages. (The exact same M, V, C, and Layout objects display the expected flash on localhost)
I assume I'm missing something simple, but I'm new to this framework so I'm not sure where else to look.
See Matt Huggins answer for your flash problem. That's correct
As for your redirect issue,
you might have an extra space or something in your routes.php file. Make sure there are no spaces before the starting <?php tag and remove the closing ?>
$this->Session->setFlash(...) is used to SET the flash message from within the controller. When you're in the view, you should be rendering the flash message like this:
<?php $session->flash(); ?>
You can also make your flash message more elaborate if you need with something like this:
<?php if ($session->check('Message.flash')): ?>
<div class="message">
<?php $session->flash(); ?>
</div>
<?php endif; ?>
That was right but there also have echo add before $session->flash();
so, it should be like:
<?php if ($session->check('Message.flash')): ?>
<div class="message">
<?php echo $session->flash(); ?>
</div> <?php endif; ?>
this worked for me!

Resources