I have these all over my theme, and when if I delete them, there nothing happens to the theme. What does it do? Should I leave them in or are they unnecessary? I want to optimize my site to load faster, so this is why I'm asking.
https://developer.wordpress.org/reference/functions/_e/
In WordPress, strings in the php files are marked for translation to other languages, and localization using two “tags” which are actually functions. They are:
__()
_e()
They are used for localization in WordPress themes. If you're only using one language for your theme, you don't need them.
These are for WordPress localization.
Here is their documentation: http://codex.wordpress.org/Function_Reference/_e
Also a few links on localization in general on WordPress to put the _e's in context:
http://make.wordpress.org/docs/plugin-developer-handbook/plugin-components/internationalization/
http://codex.wordpress.org/I18n_for_WordPress_Developers
It is a WordPress Function used for localization.
See the WordPress Docs for localization.
With this function you can output/assign "hardcoded" strings within your theme/plugin/code that are translateable (with .mo / .po files or plugins like WPML String Translation).
The function __( 'My Text', 'my-text-domain' ); assigns a string "My Text" that is translateable. 'my-text-domain' is the text-doamin the string is referenced to. This function does not echo anything!
The function _e( 'My Text', 'my-text-domain' ); is almost the same but it echoes your string directly.
WordPress Offers several other functions for localization, take a look into the Codex (link on top of my answer).
Those are WordPress library function used on localization in Wordpress themes. Its recommended to use escapes function as much as possible in theme and plugins for safety.
__() = Return the translated string
_e() = echo the translated string
esc_html__() = Escapes & return the translation string use in HTML output
esc_html_e() = Escapes & echo the translation string use in HTML output
esc_attr__() = Escapes & return the translation string use in an attribute
esc_attr_e() = Escapes & echo the translation string use in an attribute
_n() = Retrieve the plural or single form based on the amount.
_x() = Retrieve translated string with gettext context
_ex() = echo translated string with gettext context
esc_attr_x() = Escapes & return translated string with gettext context use in an attribute
esc_html_x() = Escapes & return translated string with gettext context use in HTML output
If you want echo the translated string, then you will be using _e and
when you just want to have the translated string, then you will be using __.
Actually, from my experience, I find that _e() is a function. It is similar to:
<?php function _e($txt) {
echo $txt;
}
It seems to me that if you eliminate it, you run the risk of your text not even showing up. From the uses I have seen, though, it is comments to the WordPress user to remind them to add information to the area, like the footer, header, or whatever. So eliminating may only remove all the hints the theme has built in for you.
Related
I was wondering could the use of echo in php code rather than the use of printf() be the cause of a .mo file or in general the reason why the translation files do not work at all?
For Example:
In my php code I have this line of code:
echo __('Twinkle Twinkle little star');
Which has the double underscore identifier __().
When using Poedit, I get my string displayed in the translatable strings column, so that's not an issue.
Sum up:
Could echo cause a problem with the translation even thought Poedit can index and understand that the string is there?
Thanks in advance for your time.
echo and printf are not related to translations. They are merely ways to output a string.
The translating is performed by the __() function. So assuming you have your locale set to French with the proper files loaded:
echo "Hello";
// Hello
echo __("Hello");
// Bonjour
printf("Hello");
// Hello
printf(__("Hello"));
// Bonjour
So I'm importing ExpressionEngine fields into a php array. I want to display one field, called {gearboxx_body}, unless that field has more then 300 characters, in which case I want to display a field called {article_blurb}. I'm pretty sure there isn't a way to do this just in ExpressionEngine fields and conditionals, so I tried some PHP, which I'm just starting to learn:
<?php
$info = array('{gearboxx_body}','{article_blurb}');
if(mb_strlen($info[0]) <= 300)
echo($info[0]);
}
else {
echo($info[1]);
}
?>
So that works well, but there's a problem. If the tag includes any apostrophes or quote marks, it ends the string and the page won't load. So what can I do about this? I've tried to replace the quote marks in the string, but I have to have loaded the string from the fields first, and as soon as I do that the page is already broken.
Hopefully that made sense. Any suggestions?
I would recommend you handle this in an EE plugin rather than in the template:
Faster to render (because you don't need the overhead of PHP in the templates)
More secure and reliable
Faster to develop once you get the basics of EE development down which is a useful life skill
All around best-practice
The plugin I have in mind takes three parameters:
body, blurb and character limit.
Let's say you call your plugin "Blurby". In the template you would just have this:
{exp:blurby body="{gearboxx_body}" blurb="{article_blurb}" char_limit="300"}
It variably returns either of your fields based on the logic you define in the plugin itself.
See plugin developer documentation.
Alternatively you could use the dreaded HEREDOC syntax to set variables before passing them into your array:
$body = <<<EOT
{gearboxx_body}
EOT;
$blurb = <<<EOT
{article_blurb}
EOT;
with prestashop 1.4.8, PHP 5.3 I want to do this in a module.
$myVar = 'Vincent';
echo $this->l($myVar);
I don't know why it doesn't work, and what the 'real' difference with
echo $this->l('Vincent')
I need to do this becose labels comes from XML files from my own modules configuration system.
any idea ?
Thanks you all.
Hi,
When you want to translate something in PrestaShop, you have to use the l function.
This :
$fieldToTranslate = $this->l('My Text to translate');
echo $fieldToTranslate;
Is similar to :
echo $this->l('My Text to translate');
When using echo, you should see the translated string..depending on the selected language..
If it does not work, then you should check if the l function is available for you module...did you inherit from the right class? etc.
Hope this helps,
Br,
Did you try this?
$myVar = 'Vincent';
echo $this->l($myVar, 'your module name');
A little late but here are my 2 cents.
The reason is, that Prestashop expects a literal string. It makes no sense to pass a variable to prestashops translate function. How would the translation module know which translation to use if the word to be translated could be any string possible?
I would guess that this is also the reason why double quoted strings don't work... they could contain variables.
I am trying to make sure that all of the translatable strings are present in the database. Some of them appear very rarely (various form validation errors), therefore it would be a pain to reproduce them all.
Instead, I've created an admin module that, once called, goes through an array of all translatable strings and executes echo t('[the string from the array]').
After this, I expect to be able to translate those strings using admin/config/regional/translate/translate. But not all of them are there.
What am I missing?
If that's for some reason not possible, is there any function that would force entry?
Install and use "Translation template extractor" module as suggested by Vlad Stratulat.
The module let you parse all of your modules and themes and extract all the strings that are used in t() function.
The result is a .po file with all the original/translation string pairs.
You can open the .po (it's a plain text file, so use your favorite text-editor or POEdit software) and check and translate the missing strings.
Finally re-upload the complete .po in Drupal.
Try the following.
foreach(array(t('foo'), t('bar')) as $t) {
echo $t;
}
You should never use t() to translate variables, such as calling t($text). Read about t() function.
But anyway, the best way is to create .po file from your module and export it to Drupal using Translation template extractor.
This module will extract all your translatable strings and will save it into language specific file which you can then use in any other sites where you would like to use your module.
In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content?
I have 2 cases in mind.
1) Let's say I have <?=$user1?> poked John <?=$user2?>. Maybe in some language the order of the words is different. How does gettext handle that? (no, I'm not building facebook, that was just an example)
2) Let's say I store some categories in a database. They rarely, but they are store in a database. What would happen if I do <?php echo gettext($data['name']); ?> ? I would like the translators to translate those category names too, but does it have to be done in the database itself?
Thanks
Your best option is to use sprintf() function. Then you would use printf notation to handle dynamic content in your strings. Here is a function I found on here a while ago to handle this easily for you:
function translate()
{
$args = func_get_args();
$num = func_num_args();
$args[0] = gettext($args[0]);
if($num <= 1)
return $args[0];
return call_user_func_array('sprintf', $args);
}
Now for example 1, you would want to change the string to:
%s poked %s
Which you would input into the translate() function like this:
<?php echo translate('%s poked %s', $user1, $user2); ?>
You would parse out all translate() functions with poEdit. and then translate the string "%s poked %s" into whatever language you wanted, without modifying the %s string placeholders. Those would get replace upon output by the translate() function with user1 and user2 respectively. You can read more on sprintf() in the PHP Manual for more advanced usages.
For issue #2. You would need to create a static file which poEdit could parse containing the category names. For example misctranslations.php:
<?php
_('Cars');
_('Trains');
_('Airplanes');
Then have poEdit parse misctranslations.php. You would then be able to output the category name translation using <?php echo gettext($data['name']); ?>
To build a little on what Mark said... the only problem with the above solution is that the static list must be always maintained by hand and if you add a new string before all the others or you completely change an existing one, the soft you use for translating might confuse the new strings and you could lose some translations.
I'm actually writing an article about this (too little time to finish it anytime soon!) but my proposed answer goes something like this:
Gettext allows you to store the line number that the string appears in the code inside the .po file. If you change the string entirely, the .po editor will know that the string is not new but it is an old one (thanks to the line number).
My solution to this is to write a script that reads the database and creates a static file with all the gettext strings. The big difference to Mark's solution is to have the primary key (let's call it ID) on the database match the line number in the new file. In that case, if you completely change one original translation, the lines are still the same and your translator soft will recognize the strings.
Of course there might be newer and more intelligent .po editors out there but at least if yours is giving you trouble with newer strings then this will solve them.
My 2 cents.
If you have somewhere in your code:
<?=sprintf(_('%s poked %s'), $user1, $user2)?>
and one of your languages needs to swap the arguments it is very simple. Simply translate your code like this:
msgid "%s poked %s"
msgstr "%2$s translation_of_poked %1$s"