Internationalization - how to handle gender? - cakephp

I have a cakephp 3 application I'm currently internationalizing. The app is written in french, I'm adding english.
I'm using the built in tools of cakephp 3 to do it. There is a sentence in my app that must be translated differently in english whether the user is a man or a woman. In french, the sentence remains the same.
French sentence: "xxx n'a pas indiqué ses disponibilités"
English translation: "xxx did not give his availabilities" for a man, "xxx give her availabilities" for a woman.
At first, my app looked like this:
<?= __("{0} n'a pas indiqué ses disponibilités", $user->name); ?>
I changed it so that two different translations could be provided:
<?php if ($user->gender == 'M'): ?>
<?= __("{0} n'a pas indiqué ses disponibilités", $user->name); ?>
<?php else: ?>
<?= __("{0} n'a pas indiqué ses disponibilités", $user->name); ?>
<?php endif; ?>
But when generating the pot file with bin/cake i18n extract only one sentence was extracted. I've then tried this:
<?php if ($user->gender == 'M'): ?>
<?= __x("speaking of a man", "{0} n'a pas indiqué ses disponibilités", $user->name); ?>
<?php else: ?>
<?= __x("speaking of a woman", "{0} n'a pas indiqué ses disponibilités", $user->name); ?>
<?php endif; ?>
Still, only one msgid is recorded in the pot file. How to handle this kind of issue?

Using the context translation function is the way to go, your last example is the correct way of handling such cases.
The problem is the I18N shells extract task, it uses only the message as the identifier to store the extracted values, resulting in the previous contexts to be overwritten. I would consider this to be a bug, please report it over at GitHub.
Until this is fixed you'll have to manually add the missing messages to your .pot file.

Related

cakePHP 3.3 internationalization

How can I set multiple language in case text is in array?
I know that if I use this
<?= __('username')?>
and in directory /src/Locale/de_DE/default.po
I have written following
msgid "username"
msgstr "benutzer"
It's gonna change username to benutzer if I set language to de_DE (german)
But what to do if I have this
<?= $this->Form->input('password',['label' =>'Password']); ?>
and I would like to change label Password
Simple:
$this->Form->input('password', ['label' => __('Password')]);
The __() function simply returns the translated string (more info). In your example you used
<?= ... ?>
which is equivalent to
<?php echo ... ?>

Meta noindex in post and page untranslated (unavailable) with Yoast

Is possible enter in the of posts and pages untranslated a meta robots noindex?
Of course, versions already translated, must instead stay indexable ...
I have Yoast SEO plugin compatible but I can not change meta robots per single language.
Just put this in the header of your theme. Language plugin that I use is qtranslate:
<!--Begin add noindex and nofolllow when not translated post in current lang-->
<?php if ( have_posts() && is_single() ) : while ( have_posts() ) : the_post();
$lang_current =qtranxf_getLanguage();
$id = get_the_id();
$exist_translate = qtranxf_isAvailableIn($id, $lang_current);
if (! ($exist_translate) ) : ?>
<meta name=“robots” content=“noindex,nofollow”>
<?php endif;?>
<?php endwhile;?>
<?php endif; ?>
<!--End add noindex and nofolllow when not translate post in current language-->

How to add two calendars in a page - Joomla

I am new to joomla, want to implement two calendars in a page ...
From :
<?php
echo JHTML::calendar('sch_from', 'sch_from', '%Y-%m-%d');
?>
To :
<?php
echo JHTML::calendar('sch_to', 'sch_to', '%Y-%m-%d');
?>
But first calendar only working... is there any issue in code?
syntax is:
JHtml::calendar($value,'nameOfField','idOfField', '%Y-%m-%d');
located at libraries/cms/html/html.php
try
From :
<?php
echo JHTML::calendar('','sch_from', 'sch_from', '%Y-%m-%d');
?>
To :
<?php
echo JHTML::calendar('','sch_to', 'sch_to', '%Y-%m-%d');
?>

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