Including an html element in a Yii2 language string - internationalization

Currently we have a string which is translated into several languages. The string requires some formatting around a specific word and as such we're including a span in the translation string.
In the language file...
return [
'signup.success.reminder' => 'This is <span>very</span> important!'
];
...and then in the template...
<p><?php echo \Yii::t('app', 'signup.success.reminder'); ?></p>
Given the above requirement is there a better way to include the span tag?

I believe the best way if you're not using variables inside the code is putting it all together in the translated string
View
<p><?php echo \Yii::t('app', 'This is <span>very</span> important!'); ?></p>
Message File
return [
'This is <span>very</span> important!' => 'Esto es <span>muy</span> importante!'
];
At least this approach was the one that worked best for me
If you're using complex html, you could replace it like this
View
<p><?php echo \Yii::t('app', 'This is {tag} important!', ['tag'=>Html::tag('span'), \Yii::t('app', 'Very'))]; ?></p>

As of 2021 a way to doing this is - also mentioned by #nico-savini:
<?= Yii::t(
'app',
'Welcome to {appName}',
['appName' => Html::tag('em', Html::encode(Yii::$app->name))]
)
?>

Related

Laravel blade syntax for #if/#endif spacing issueif

what is the similar blade syntax for the following code?
<?php if(...):?>abc<?php else:?>cde<?php endif;?>
the code
#if(...) abc #else .... is no good as it ads a space before and after the "abc" the code #if(...)abc#endif (no spacing before and after html string) generates error ...
Thanks
Solution
The correct solution for this problem would be following:
#if(1==1){{ '1' }}#endif
This happens often and makes problem with "space sensitive" parts of code like values in <option> tags.
Having the #if(1==1) 1 #endif would be compiled to following which shows empty spaces around the number:
<?php if(1==1): ?> 1 <?php endif; ?>
Solution code would be compiled to this code:
<?php if(1==1): ?><?php echo e('1'); ?><?php endif; ?>
Which pretty much explains why this won't make additional spaces.
Did a bit more research and it looks like there is no solution as there is no spaceless tag in blade. I found a solution from someone who wrapping his string in extra hml tags (so that is easy as he ads spaces before and after the tag and the string inside tag si spaceless) but in my case I just need a string no spaces before and after ... I will go the old fashion php way ...
Try this:
#if(...) {{ 'abc' }} #else
I've run into similar problems with spaces.
A workaround I use is this:
Put your if statement into php tags
#php
$myVar = 'abc';
if(...) $myVar = 'cde';
#endphp
and then echo the defined variable
{{$myVar}}
It's a workaround, but I still think we always should remember that it's a PHP environment...
So, simply:
#php
if(...) echo "abc";
else echo "cde";
#endphp
I have found the solution in case there is space issue between #if()#endif and #if.
I have replaced the #if() #endif with the {{$wardname}} variable to be printed using {{$wardname}}#if and its removed conflict with #endif & #if
and applied logic like:
if($city->wardname!="") {
$wardname = "(".$city->wardname.")";
}else{
$wardname = "";
}
Implemented it as:
{{$wardname}}#if
The correct syntax is:
#if(...)
abc
#else
cde
#endif
I never saw unwanted spaces with this one.

Drupal 7 (Print render image on page tpl)

I can't seem to print render an image on my page.tpl.php.
This is the code I use:
<?php print render($content['field_image']); ?>
I can get the title to show up though: <?php print $title; ?>
So what am I doing wrong?
Because you're in a page.tpl not a node.tpl, field_image isn't in $content. The only thing in $content should be regions, which contain blocks.
Use this instead: https://www.drupal.org/project/fieldblock
Here you can use fields as a block. Very easy :)

How to get Joomla Tag list in a module

I am using Joomla 3.4.8 and it's Article - Latest Module. That module showed only latest article's title but I want to show introtext and tags also.
I was able to show the introtext but didn't able to show tags. Can anyone help me to figure it out? Here is my code given below.
<pre><code>foreach($list as $item):
echo $item->title;
echo $item->introText;
echo $item->tags->itemTag;
endforeach;</code></pre>
When I run this code get the error message:
Notice: Array to string conversion in F:\xampp\htdocs\rnd\joomla\tx_quicx\modules\mod_articles_latest\tmpl\default.php
on line 37
Which corresponds to echo $item->tags->itemTag;
Thanks in advance.
The tags field is an array.
You have to reference the element number you want from that field and then reference its itemTag.
foreach($list as $item):
echo $item->title;
echo $item->introText;
echo $item->tags[0]->itemTag;
endforeach;
Will get the first (0th) item tag. If you want all item tags for all items, you'll have to iterate through all elements of the tags array.
This code solve my issue.
foreach($list as $item){
echo $item->title;
echo $item->introText;
foreach($item->tags->itemTags as $tag){
echo $tag->title;
}
}
Thanks Brandon

Problems Saving Large Number of Attribute Option Labels in Magento

I'm running into a problem in a Magento system where saving a large number of attributes either doesn't work at all, or only partially works. It appears to be a javascript related issue, and I was hoping someone on Stack Overflow had some "known science" for dealing with this situation, or could point me in the right direction.
The basic problem is, the Magento system in question has over 250 color attribute option labels. If an admin user attempts to manage these by doing the following
Navigating to Catalog -> Attributes -> Manage Attributes
Selecting the color attributes
Clicking on the Manage Label/Options tab
Editing the last Label Option
Clicking "Save and Continue Edit"
One of two things happens.
In Google Chrome on OS X, the button sticks in the "depressed" state, and after a period of time Google Chrome's "This page is not responsive" kill dialog comes up.
In a mozilla based browser on OS X, clicking the button makes the browser "think" for a bit, but it eventually submits the form. However, only a partial list of attribute labels is posted to the admin controller. This means the user can only edit the first 75 - 100 labels, since the other labels are never submitted.
I have reports from windows users describing the second behavior as well (browsers are non-specific)
The obvious answers are to either investigate the poorly performing javascript, or (Grouch Marx style) "don't do that". Before I spend the time profiling/excavating the javascript on that page, I was hoping there was some known fix for this, or specific knowledge as to what was causing the problem.
Magento CE 1.7.x, if it maters.
Update: The Javascript performance issues are a red herring. They're caused by the massive number of input fields being iterated through in
js/prototype/validation.js
Specifically in this try catch block
try {
if(this.options.stopOnFirst) {
result = Form.getElements(this.form).all(function(elm) {
if (elm.hasClassName('local-validation') && !this.isElementInForm(elm, this.form)) {
return true;
}
return Validation.validate(elm,{useTitle : useTitles, onElementValidate : callback});
}, this);
} else {
result = Form.getElements(this.form).collect(function(elm) {
if (elm.hasClassName('local-validation') && !this.isElementInForm(elm, this.form)) {
return true;
}
return Validation.validate(elm,{useTitle : useTitles, onElementValidate : callback});
}, this).all();
}
} catch (e) {
}
However, even if I short circuit this and have the function return true, the behavior of not saving all the labels persists.
You can try the variable max_input_vars (introduced in PHP 5.3.9), by default it's 1000 so that should be enough, but maybe your configuration uses a lower amount. But I imagine the form just doesn't get through due to the major performance issues you're experiencing.
About the option labels: do they by any change have an uploader for an attribute-image? We had the exact same problem when we installed the GoMage Advanced Navigation extension on a shop with over 300 manufacturer options (the extension uses Magento's built-in Flash-uploader).
We didn't have the extension for that feature so I disabled the uploader, but the extreme performance decrease was definitely in the 300 Flash-movies being loaded. Maybe you can try lazyloading the uploader on a per-option basis by inserting a button or link instead of the movie.
Hope this points you in the right (or exact) direction.
[Working SOLUTION]
Hello as Alan Storm mentioned, this ussue is related with the JS logic, that handles the validation of the option label inputs. I had this problem on a project of one of my clents and I wrote simple extension, that solves it.
You can dowload the exntesion for here: https://github.com/Jarlssen/Jarlssen_FasterAttributeOptionEdit
Basically the extension replaces the original option template with mine template. In my template I rewrote most of the JS in the bottom of the template and replaced the form inputs with div elements ( pseudo inputs ), so when the administrator click on the pseudo input, then its replaced with the real input. In this way we avoid validating all the inputs and we validate only edited and the new added entries. The extension works well if you add options on chunks, for example 500 entries per attribute save.
Hope, that helps.
Additional information: http://www.jarlssen.de/blog/2014/05/07/magento-timeout-saving-attribute-options-type-multiple-select-and-dropdown
Quick look at the pseudo generation code:
<tr class="option-row">
<?php foreach ($this->getStores() as $_store): ?>
<td>
<div class="replace-content pseudo-input input-text <?php if($_store->getId()==0): ?> required-option<?php endif; ?>" id="option[value][<?php echo $_value->getId() ?>][<?php echo $_store->getId() ?>]"><?php echo $_value->getData('store' . $_store->getId()) ?></div>
</td>
<?php endforeach; ?>
<td>
<div class="replace-content pseudo-input" id="option[order][<?php echo $_value->getId() ?>]"><?php echo $_value->getSortOrder() ?></div>
</td>
<td class="a-center default-checkbox">
<div id="option_<?php echo $_value->getId() ?>" class="checkbox-radio-container replace-content">
<?php if($_value->getChecked()) : ?>
<input class="input-radio" type="<?php echo $defaultChooserInputType; ?>" name="default[]" value="<?php echo $_value->getId() ?>" checked <?php if ($this->getReadOnly()):?> disabled="disabled"<?php endif;?>/>
<?php else : ?>
<?php if('radio' == $defaultChooserInputType) : ?>
<span class="fake-radio"></span>
<?php else : ?>
<span class="fake-checkbox"></span>
<?php endif; ?>
<?php endif; ?>
</div>
</td>
<td class="a-left actions-column" id="delete_button_container_<?php echo $_value->getId() ?>">
<div id="option[delete][<?php echo $_value->getId() ?>]" title="<?php echo $this->__('Delete') ?>" class="scalable left pseudo-delete-option">
<span class="pseudo-delete-button" option_id="<?php echo $_value->getId(); ?>">
<span>
<span><?php echo $this->__('Delete') ?></span>
</span>
</span>
</div>
</td>
</tr>
I had exactly this problem (POST truncated) and it comes from a suhosin patch that has a too little POST limit. (or the standard PHP post_max_size)
In your php.ini check these values and increase their values if needed (and restart apache) :
post_max_size
suhosin.post.max_vars
suhosin.request.max_vars
For your second probleme (JS performance issue), I can't help you
Sorry!!
I went through this same problem , solution follows below.
Explanation of the problem
problem
A customer has a very large base of tires that also have many cars added to the same , thereby to migrate moorings between tires and vehicles only 255 characters of the attributes of ids were inserted into the table with it causing error in the migration.
The magento inserts a string with ids separated by ,
The problem occurred in the table "catalog_product_entity_varchar" in the value field which by default is 255 characters if I put text and resolved .
Note : I know you modify the DB is not nice but unfortunately had to perform this operation.
Example product
Tire 175 / 65R14
-> Make ( + - 200 options)
-> Model (+ - 4mil options)
-> Series (+ - 15mil options)
-> Year
-> ...
att,
Same problem here. I solved with BELVG module "Attribute Pagination" : http://blog.belvg.com/attribute-admin-page-pagination-in-magento.html
It works properly.
Hope that helps.
Open your server's php.ini, search for max_input_vars and set its value to greater than 2500 will solve your problem
; How many GET/POST/COOKIE input variables may be accepted
max_input_vars = 5000

How can I create a simple internationalization (i18n) example in Kohana v3.0.6.2?

I've been trying to make i18n work: Just to display a string with 2 different languages. I followed this tutorial, but I think the author is not using Kohana 3.
It basically consist on creating a langauge file (pl.php) file in the i18n folder, and add the following:
<?php
$translations['Good Morning'] = 'Magandang Umaga';
Then changing the locale to pl.
and finally the output in the view file:
<?php echo __('Good Morning'); // would produce 'Good Morning' ?>
I really got lost in the tutorial.
Can anyone give me a small example of using internationalization (i18n) in Kohana v3.0.6.2?
Thanks in advance!
this is how your i18n/pl.php file should look like:
<?php defined('SYSPATH') or die('No direct script access.');
return array(
'Good Morning' => 'Magandang Umaga',
);
you can't copy everything that you see from a ko2 tutorial
I think you need to use the key from the translations file. i.e.
<?php echo __('good_morning'); // would produce 'Magandang Umaga' if the locale was 'pl' ?>
but you can also use real words as the key, so in your pl.php file you would have:
$translations['Good Morning'] = 'Magandang Umaga';
and then you can use:
<?php echo __('Good Morning'); // would produce 'Magandang Umaga' if the locale was 'pl' ?>
in your view.

Resources