Prevent gettext translation - php

A variable $status is assigned the string values 'stop' or 'go' (in php action).
In the view, I would like to display the value of $status in such a way that "stop" is displayed in red and "go" in green.
So I wrote something like:
<span class="<?=$status?>"><?= $status?></span>
and defined classes "stop/go" in my css. Everything works fine until I start making it multi-language by wrapping strings in _() in the action. Now gettext not only translates the actually displayed string but also the class name (which I don't want). Is there something like ungettext, so that I can write:
<span class="<?=ungettext($status)?>"><?= $status?></span>
and get the untranslated class name?
I realize that I could just send a bool from the action and use an if statement in the view to create what I want. Alternatively (more ugly) I could add translated class names in the css. But is there a more elegant way?

I think you're over-thinking the problem. Why not simply create another variable in the action, say $status_stye, that you don't apply the translation to? That way you have both the CSS class you want and the translated version of the text.

Related

Typo3 with extbase - get translation for specific language

I wrote a backend hook so that I can write notification E-mails as soon as an item is set to hidden = 0 in typo3. I managed to access LocalizationUtility to access my translation files, like this:
$localization = $objectManager->get('\TYPO3\CMS\Extbase\Utility\LocalizationUtility');
$localization::translate('tx_extplugin_domain_model_item.email.text1', 'ExtPlugin')
But how do I define which language to use? It doesn't seem like the translation function takes a language parameter, so how do I get the text in a different language?
Thanks in advance!
You can use the readLLfile Method to get specific translation by languagekey. This will return a array of all translated strings in $file.
$fd = GeneralUtility::readLLfile($file, $langKey);
You can't use the Typo3 translation in this way - Typo3 will always translate to the current language scope.
As per this answer I think the only way you could do it would to hold your translations outside of Typo3 (in an array or similar), and then do your own translation, rather than using the Typo3 built in one.

Blade templates - Toggle auto escape on the fly?

I have a template which calls a number of built in macros which I am including this template from several other places.
Sometimes, I need all of the macros to be called like this:
{{ Form::label('foo', 'Foo') }}
Other times I need them all to be called like this:
{{{ Form::label('foo', 'Foo') }}}
At the moment, I have two separate templates which are identical except for the extra { }, which means I have to edit two files every time I want to change anything.
Is there a way to switch the auto escaping on/off, so that I can use the same file for both situations?
Thanks
No, there's no feature in Laravel that would allow you to do that -- additionally, it'd probably be a bad idea from a code maintenance/security-audit point of view. Looking at a template a few weeks later and not knowing which variables were or were not escaped would be madness.
If you need to do this, "The Right" way would be to extend Blade with your own directive -- something like #escapeIsConfigIsOn and then put your logic for when to escape content in there. The top level function e is what blade uses internally for escaping.
#File: login/vendor/laravel/framework/src/Illuminate/Support/helpers.php
function e($value)
{
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}

Get value from the object array and modify it's content before using it

Sorry i don't know under which keywords i should find the answer, but here is the problem:
There is a $data->cart_show;, while doing var_dump() on it, i get this values:
string '<a style ="float:right;" href="/index.php?option=com_virtuemart&view=cart&Itemid=105&lang=en">Cart content</a>' (length=124)
I need to remove style attribute and add a custom class on it, (i need to do it in a view file and not in a controller or model) so it becomes like this:
Cart content
Thanks for any tips
Things like this must be done in model, controllers or in view helpers. Such logic shouldn't be in a view itself. But what you want could be done in two ways:
1) Use a regular expression and replace your 'style' attribute by a 'class' with preg_replace() function
2) Use DOMDocument to modify your existing element

Where does this variable come from in Codeigniter? And are there any more?

In the default Codeigniter installation there is the "welcome" controller which has a "index" action which loads the "welcome" view. This works as expected.
However, upon inspecting the "welcome" view I can see this variable in the footer.
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
From what I understand the variable {elapsed_time} is an example of using the built in template parser with text representations instead of using PHP short tags to echo variables.
But inside the "welcome" controller the only lines in the "index" action are these.
$this->load->view('welcome');
And it doesn't pass $data['elapsed_time']='xxx'; which means that I can't figure out where the variable elapsed_time is coming from!
My question is this.
Where does elapsed_time get defined? Is it built into the template parser class (and therefore available to be used without first defining it)? If so, where is a list of these other pre-defined varaibles? I would like to know what else I have access to as had I known that elapsed_time was available to me it would have been very useful. Does anyone have a list of the template parser pre-defined variables?
Thanks in advance.
elapsed_time defined output class. this class is initialized automatically by CodeIgniter.
more info here
CI will replace the string of "{elapsed_time}" in the final output string with actual "total_execution_time ". You can check it in system/core/Output.php Line 366 of v213

Expression Engine fetch_action_id() not parsing

So I have tried to get the action ID two ways:
$ACT_ID = $this->EE->functions->fetch_action_id("classname", "function");
$ACT_ID = $FNS->fetch_action_id("classname", "function");
but, it still gives me this as the output:
{AID:classname:function}
and it doesn't parse it when its output into the view. Is there something else I need to do?
For EE2, there are 2 ways of getting an ACT ID, depending on where you're going to use it.
If you're using it in the font-end / templates, use $this->EE->functions->fetch_action_id('class', 'method');, which will return {AID:class:method} in the template, which the template parser in turn will replace with the actual ACT ID. This is done for performance reasons; only 1 query for all ACT IDs is needed. If no valid ACT ID is found, the AID string will remain as is in the template.
If you're using it in the back-end / Control Panel, use $this->EE->cp->fetch_action_id('class', 'method');, which returns the actual ACT ID. The $this->EE->cp object is only available in the Control Panel (for example, the mcp.your_module.php file). If no valid ACT ID is found, it will return FALSE.
There is an undocumented method "insert_action_ids" in the Functions library that parses action_id's in the templates. So from your addon, if you want to simply have the action ID number, you can do the following:
$ACT_ID = $this->EE->functions->insert_action_ids($this->EE->functions->fetch_action_id('classname', 'method'));
Update
I should add that this method will work anywhere - frontend as well as in the CP. But as some have mentioned, when in the templates it is best for performance to use the "insert_action_ids" method and have the template parser replace these with the correct action IDs.

Categories