prestashop $this->l - php

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.

Related

Is using echo rather than printf a trouble maker for string translation using .pot file and Poedit?

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

Echo'ing a "{FORUM_NAME}" and Ignoring the "{}"

I'm looking for something that Is really hard for me to do.. I really tried to search all over the net for Solution, But I couldn't seem to find any. I also tried doing this for hours.
What I'm doing: Making a theme for PHPBB2, Installed a MOD that can include PHP in themes.
What is the problem: When I'm doing {} tags in php, It just can't echo those tags.
Let's say I have a function that creates a Table for me, like that:
CreateMyTable(Name,Size,Color);
I put in the function those strings:
CreateMyTable("{FORUM_NAME}",1000,red);
The title stays blank, I actually want it to echo {FORUM_NAME}.
How can I do this?
P.S: I can't do this
CreateMyTable(?>{FORUM_NAME}<?php , 1000, red);
It's not going to work becuase <? = <!-- PHP --> , ?> = <!-- ENDPHP -->.
Thanks for your help :)
If you look in the PHPbb2 template class, you'll find that the template is simply an evaluated set of PHP using the eval() function. You can either print the contents of the PHP before it is parsed using eval() and then use the variable name that the template gives, IE something like (which may not work depending how your template is setup):
CreateMyTable(((isset($this->_tpldata['.'][0]['FORUM_NAME'])) ? $this->_tpldata['.'][0]['FORUM_NAME'] : '' ),1000,randomcolor());
Please note, in order to do it similar to the way above you'd actually have to insert this into your template class.
An much better solution is to avoid using the mod that allows PHP in templates and use JavaScript in the templates to create the function, then print a call to that JavaScript function.
This will work:
CreateMyTable(FORUM_NAME,1000,red);
I also noticed that red is used without quotes - is this also a constant? If it's a variable it needs to have a $ in front of it. If it's a string it should be between quotes.
CreateMyTable(FORUM_NAME,1000,"red");

Setting language for user

I would like my php website to be able to be multilinguistic. I thought of using:
echo $lang[$_SESSION['lang']]['WellcomeMessage'];
but I found that I will be needing to format the text, say for example male/female or putting some values from the DB. So I thought that simple strings might not do the trick for formatting?
I know #define might have worked in C as the string translates to code, but I don't know how php does that. For example:
define ($lang['en']['credit_left'],'you have $credits_left');
define ($lang['sp']['credit_left'],'tienes $credits_left creditos mas');
Any suggestions?

What does the WordPress "_e()" function do?

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.

How does gettext handle dynamic content?

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"

Categories