Drupal7 : Why t() is not working? - drupal

I'm in charge of an old site on Drupal7, that I know too little. This site is a bilingual site English/French.
So in a template, I added some fields, with strings that need to be translated :
<div class="offer_content">
<span class="title" style="text-transform:uppercase;font-size:18px;"><?php echo t('Our Firma').(': ');?></span>
<?php echo html_entity_decode(t($fields['field_presentation_societe']->content)); ?>
<span class="title" style="text-transform:uppercase;font-size:18px;"><?php echo t('Job presentation').(': ');?></span>
<?php echo html_entity_decode(t($fields['body']->content));?>
<span class="title" style="text-transform:uppercase;font-size:18px;"><?php echo t('Skills').(': ');?></span>
<?php echo html_entity_decode(t($fields['field_profil_recherche']->content)); ?>
However, in French, that keeps me showing "Our Firma" and "Skills" ... instead of the translation ? Why ?
Thanks you :)

Variables ain't translatable by design. See the t-docs:
Translating Variables
You should never use t() to translate variables, such as calling
t($text);
, unless the text that the variable holds has been passed through t()
elsewhere (e.g., $text is one of several translated literal strings in
an array). It is especially important never to call
t($user_text);
, where $user_text is some text that a user entered - doing that can
lead to cross-site scripting and other security problems. However, you
can use variable substitution in your string, to put variable text
such as user names or link URLs into translated text. Variable
substitution looks like this:
$text = t("#name's blog", array(
'#name' => format_username($account),
));
Basically, you can put variables like #name into your string, and t()
will substitute their sanitized values at translation time. (See the
Localization API pages referenced above and the documentation of
format_string() for details about how to define variables in your
string.) Translators can then rearrange the string as necessary for
the language (e.g., in Spanish, it might be "blog de #name").
So what you could do instead, is to use Entity Translation to make these fields translatable. You'll then be able to translate your content and the correct translation will get printed based on what value was added to the field in the matching language.
Or to use i18n_string(), see https://drupal.stackexchange.com/a/184584

Related

Scraping a page to retrieve prices, currency code messing things up

I'm scraping a page using PHP Simple HTML DOM Parser and I want to retrieve the price. It's been going well except for a page that I've encountered, where the html reads:
<p class="was-price">Was: £220.00</p>
I want to scrape the part that reads 220.00 and I am very confused about how to retrieve it. Thus far I have been using preg_replace() with great success to strip out text from a string, yet this is the first time I have come across a currency symbol in numeric format.
Today is the first day I have used preg_replace() and it's confusing to say the least. Can it be used to remove currency symbols in this way? Or should I be looking at another method? Thanks
Use html_entity_decode() to decode encoded html entities. Then you apply preg_replace().
$str = '<p class="was-price">Was: £220.00</p>';
$str = html_entity_decode($str);
echo $str;
preg_replace(...);

Usage of esc_url, esc_html, esc_attr ... functions

When are definitely needed or for a good practice to use escaping functions?
Such as using esc_url(); with:
get_template_directory_uri();
get_permalink();
get_author_posts_url();
get_edit_post_link();
wp_get_attachment_url();
And esc_html(); with:
get_the_title();
get_the_author();
get_the_date();
get_search_query();
Also I think esc_html(); and esc_attr(); are very similar, aren't they? What are the differences?
Part 1
According to the documentation - Validating, Sanitizing, and Escaping by WP VIP team.
Guiding Principles
Never trust user input.
Escape as late as possible.
Escape everything from untrusted sources (like databases and users), third-parties (like Twitter), etc.
Never assume anything.
Never trust user input.
Sanitation is okay, but validation/rejection is better.
Never trust user input.
“Escaping isn’t only about protecting from bad guys. It’s just making our software durable. Against random bad input, against malicious input, or against bad weather.” –nb
Part 2
Codex entry for esc_html
Codex entry for esc_attr
According to the article - Introduction to WordPress Front End Security: Escaping the Things by Andy Adams from CSS-Tricks.
Function: esc_html
Used for: Output that should have absolutely no HTML in the output.
What it does: Converts HTML special characters (such as <, >, &) into their "escaped" entity (<, >, &).
Function: esc_attr
Used for: Output being used in the context of an HTML attribute (think "title", "data-" fields, "alt" text).
What it does: The exact same thing as esc_html. The only difference is that different WordPress filters are applied to each function.

What is the difference between get_the_* and the_* template tags in wordpress?

I am confuse about get_the_* and the_* template tags. I have used those many times to my theme but i am not clear enough when to use get_the_* and when to use the_* . Would you please explain both concept clearly.
Typically, there are two key differences between get_the_* and the_* functions.
get_the_* methods don't echo anything themselves. Instead, they return the value that you're interested in, normally as a string. For example, get_the_time() echoes nothing, and returns a string representation of the posting time of the current post. the_* methods directly output the same value, without you having to echo it; the_time() returns nothing, but directly echoes the posting time.
the_* methods are generally designed to be used inside the Loop, so they often don't take a parameter to specify which post you're asking about; for example, the_title() doesn't take a post_id parameter, and can therefore only act on the "current" post inside the Loop. It doesn't make sense to call it outside the loop—which post would it be getting the title for? However, get_the_title() takes a post ID as a parameter, so you can use it from anywhere to get the title of any post, as long as you've got the post's ID. (Many of the get_the_ methods take an optional post id parameter, and default to returning the value for the current post if they're used from in the Loop, for convenience.)
Because WordPress has been in development for so many years, and things have gradually been added, these aren't guaranteed rules, and you'll find exceptions here and there. You should take this as general advice and check the documentation for each specific instance as you need it.
The difference is that you can only use the_* inside your loop. But get_the* you can use inside or oustide the loop. Outside the loop you should give the post_id as a parameter.
And by default the_* echo's the title for example and get_the* just gets the title for using it in your PHP.
There is something more to it. I just tried the_content() and echo get_the_content() which should the same thing but.. If you add a filter('the_content') it wont work with echo get_the_content() but it works fine with the_content() method.

Extended use of replace filter in twig?

i checked out the documentation for the replace filter in twig. my problem is that, suppose i have a variable say contvariable, and the content passed through that variable from the controller is dynamic
return $this->render('RodasysFormstudyBundle:Default:addclientname.html.twig', array('contvariable' =>$sometext));
this $sometext variable will contain texts like
$sometext='%Sun% rises in the East';
the text inside the %% should be displayed as an input field in the browser. I did not find any examples in the web like to replace the content inside the %% (what ever the content be whether its sun or the moon). Is this possible to do this using replace filter or should i follow some other method such as to replace the content in controller before sending it to twig..
please help..
You could do something like that (with the 'raw' filter) :
{{ "%foo% rises in the East"|replace({'%foo%': "<input type='text' name='"~foo~"' value='"~foo~"'/>"})|raw }}
foo is a variable sent by your controller, with the value of your choice.

Drupal: a question about t() function

I've added the following text in the t() function in page.tpl.php page.
<?php echo t('Some random text to translate.'); ?>
Now when I go to admin/build/translate/search to translate the string drupal cannot find it.
I've refreshed all caches, and also the "Refresh" tab in internationalization module.
thanks
The t() function, to save on performance for single-language sites, won't store the string in the database until it is requested from another language. View the page in one of the non-default languages in order to populate the database (t() calls locale() to do this), thus making the string available for translation.

Resources