Magento :: Translate text from javascript files - javascript

Magento uses a system for translating text in the template files using:
$this->__('text to be translated.');
or
Mage::helper('modulename')->__('text to be translated.');.
This works quite well.
But when I add text to a javascript file I can't use these two methods.
Is there a way I could do a similar thing with the translations for javascript files?

You can do it in a template file yourfile.phtml. The JavaScript js/mage/translate.js file must be included in your HTML header (Magento does it by default).
<script type="text/javascript">
Translator.add('You should take care of this confirmation message!','<?php echo Mage::helper('yourmodule')->__('You should take care of this confirmation message!')?>');
</script>
Since Magento 1.7, you can add a file jstranslator.xml into your module under the etc/ folder and set the following string like that:
<jstranslator>
<!-- validation.js -->
<validate-no-html-tags translate="message" module="core">
<message>HTML tags are not allowed</message>
</validate-no-html-tags>
<validate-select translate="message" module="core">
<message>Please select an option.</message>
</validate-select>
</jstranslator>
Then translate the string as you do it for PHP thanks to the CSV file. This will add the translation to the JavaScript code like the following var Translator = new Translate(...).

Just use the following method in your scripts:
Translator.translate('Some phrase');

I just made the simplest way:
let sometext = '<?php echo $this->__('text to be translated.'); ?>' + someVarData;

This is the correct way for translating JavaScript strings withing .phtml file
Translator.add({"To be translated":"<?php echo $this->_('To be translated'); ?>"});

Use this is in js file:
Translator.translate('Some phrase');
But to make it work you should define this translation in phtml:
Translator.add('Some phrase', "<?php echo $this->__('Some phrase'); ?>");

Related

use multiple files in category.tpl with smarty

{$category->id|escape:'htmlall':'UTF-8'}
this smarty code is for get id number on category.tpl
we have lot of category id's example 2,6,8,10 etc etc and in my folder lots of files names example 2minimum.txt, 6minimum.txt, 8minimum.txt, 10minimum.txt etc etc
i want to use include file when current category id page with category id number txt file.
{include file='folder/2minimum.txt'} <=- this will work perfect when i put file name but i want to use like this
{include file='folder/{$category->id|escape:'htmlall':'UTF-8'}minimum.txt'}
but it's give error
experts need you help to solve this
You should use smarty cat to concatenate variable to your file name. In your example it should look something like:
{include file='folder/'|cat:{$category->id|escape:'htmlall':'UTF-8'}|cat:'minimum.txt'}
Link to smarty documentation
Hope it helps, have a nice day!
{include file='folder/'|cat:$category.id|cat:'minimum.txt'}
In smarty {} means {this is smarty}.
But you wrote {this is smarty{what's this?}this is smarty}.
It was a syntax error.
And I'm not sure, but I think $category->id is php code style. You could find $category.id is smarty code style in Prestashop template files.
And $category.id returns number. I don't think it needs escape.

Display code snippet in Laravel blade

i need to display as text on the screen a snippet that contains some php variables, but when i load the page Laravel resolve this variables and give me a undefined variable error. Basically i need that when i try to display the snippet inside <code></code> tags it print something like this:
<script type='text/javascript>
var variable = '<?= $variable ?>';
</script>
I have tried already with {{htmlspecialchars}} but not work
In your controller you can assign the code to a variable :
$var = Blade::compileString('<script type=\'text/javascript\'>
var variable = \'<?= $variable ?>\';
</script>');
In your blade, access the variable as :
{{ $var }}
You can use highlight_string($code) to show the snippet. Inspiration from this post: How do I display PHP code in HTML?
Maybe you could try to enclose your snippet on a #verbatim block. This should escape its content of the rendering.
See more here: https://laravel.com/docs/5.5/blade#displaying-data

Embed same web widget multiple times with different data attribute values

I want to create a web widget that can be embedded multiple times on the same page but with different data attribute values so I can display different data according to the data attribute value.
For example, I want to embed mywidget.js file multiple times as follows:
<body>
<div>
<script src="script/mywidget.js" data-sport="soccer" id="widget-soccer">
</script>
</div>
<div>
<script src="script/mywidget.js" data-sport="tennis" id="widget-tennis">
</script>
</div>
</body>
My question is, inside the code in mywidget.js, how do I determine the correct script tag reference and read it's data attribute so I can use that value to fetch the corresponding data from a web service. I am using only jquery and javascript.
I want the widget to be embeddable on other users sites as well so all they do is embed using only the script tag and passing in the desired data attribute value without adding anything extra anywhere they need on their website.
This is not really a very good approach, as it is very inflexible. But given that <script> tags, when not deferred, halt parsing of the document while they execute, the current script tag will be the last in the DOM; so you can get the current sport inside your script by using this:
var sport = $('script').last().data('sport');
However, it would be much better to define a function in your external JavaScript file, and then invoke it when you need to instantiate your widget (EDIT: like in Lee Taylor's answer).
Why don't you do something like:
<head>
<script src="script/mywidget.js"></script>
</head>
<body>
<div><script>createMyWidget({sport : "soccer"} );</div>
<div><script>createMyWidget({sport : "tennis"} );</div>
</body>
I don't think you can. I know it's not that nice, but I would try:
<div><script>sport = "soccer";</script><script src="script/mywidget.js" id="widget-soccer"></script></div>
<div><script>sport = "tennis";</script><script src="script/mywidget.js" id="widget-tennis"></script></div>
and use sport in mywidget.js
Another approach could be that myscript.js is actually a dynamic "page", let's say with php, then you could use src="script/mywidget.js?sport=swimming", and in the php you would print:
sport = "<?php echo addcslashes($_GET['sport'], '"'); ?>";
But even better would be:
<script src="script/mywidget.js"></script>
<div><script>showWidget("soccer");</script></div>
<div><script>showWidget("basketball");</script></div>
I think you can use jQuery to find all script tags with src="script/mywidget.js" or something
$('script[src="script/mywidget.js"]')
And then you'll have an array of scripts tags that you can loop through and access the data property using jQuery's .data() method.

Interpret variable in Javascript file Laravel

I need to include a js file in my views.
But in this js file i need to interpret somes PHP variable.
Actually i do this :
#section('javascript')
<script>
alert("{{{test}}}");
</script>
#stop
But i REALLY need to do this :
#section('javascript')
{!! Html::script('js/test.js') !!}
#stop
test.js :
alert("{{{test}}}");
I need to declare a lot o variable. So my JS file will be very huge. And i don't want to show this directly in the source code.
How can i do ?
Thank you !
You can only pass the variable to the javascript like so:
#section('javascript')
<script>
var test = {{{$test}}};
</script>
#stop
then in your javascript file included at the bottom you can use it:
alert(test);
Let me just mention that this is not a great way of handling the passing variables from php to javascript.
When I need to do something like this, I usually create a meta tag on the page which would contain the alert information.
<meta name="someAlertValue" content="{{{ $test }}}" />
Then you can very easily grab that via jQuery.
var alert_text = $('meta[name=someAlertValue]').attr('content');
I find this approach to be much cleaner and maintainable than trying to drop php variables directly into your javascript.
I had the same problem, and wanted to have a stand alone js which will have bunch of variables taken from config() or even from database, multi-language, and will be configurable, or even will work with query parameters.
Maybe it's a hard way, but i've created a route:
Route::get('/js-modules/test.js',function(){ return view('js-modules.test');})->name('my_js);
So in the view resources/views/js-modules/test.blade.php you can put your js code together with your PHP stuff.
Or you can route it to a controller and have even more background work. it looks a bit slow (in the browser) on the first run , but second request it'll be cashed and retrieved as the regular js file
And now you can link it to any of your pages with
<script src="{{route('my_js')}}"></script>

Tooltips in Joomla containing HTML

I created a component using Component Creator for Joomla 3.X
The problem is that on the site the tooltips show html code. For example:
<strong>Title</strong><br/>Description
Instead, on the administrator they are displayed properly:
Title
Description
I was reviewing the documentation here and here, and it seems that it is possible to define the output format when calling the tooltip function but there are no calls inside the component to that function. The only call I see is JHtml::_('behavior.tooltip'); at the begining of the view file and don't know how to specify the output format.
The site is in this URL:
http://50.87.185.99/colombianadederecho_blank/index.php/administradores-ph/registrarse
Added:
It seems like the code is called when building the label for each field:
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('document'); ?></div>
<div class="controls"><?php echo $this->form->getInput('document'); ?></div>
</div>
And it is stored in the title attribute:
title="<strong>Title</strong><br />Description"
So, I think the problem is in the Javascript function, maybe using a .text() jQuery function instead of a .html().
Inspecting the source code I found this, but don't understand why it is not working properly:
window.addEvent('domready', function() {
$$('.hasTip').each(function(el) {
var title = el.get('title');
if (title) {
var parts = title.split('::', 2);
el.store('tip:title', parts[0]);
el.store('tip:text', parts[1]);
}
});
var JTooltips = new Tips($$('.hasTip'), {"maxTitleChars": 50,"fixed": false});
});
jQuery(document).ready(function() {
jQuery('.hasTooltip').tooltip({"html": true,"container": "body"});
});
Solution:
As #ilias found it was a problem between libraries. I had to disable the bootstrap call in the header using this plugin and call it from the end of the body:
<script src="/site/media/jui/js/bootstrap.min.js" type="text/javascript"></script>
I don't know the code that Component Creator produces, however I assume that the tooltips are like in the administrator views. If this is the case you should check for something like this:
<button type="submit" class="btn hasTooltip" title="<?php echo JHtml::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
and enclose the contents of JHtml::tootlipText() in strip_tags().
Otherwise try to find the line <strong>Title</strong><br/>Description where the tags are shown and enclose it in strip_tags(). For example you might have something that looks like:
echo $this->escape($item->description);
that should be turned into:
echo $this->escape(strip_tags($item->description));

Categories

Resources