You can dynamically create and serve a JS file just like an HTML file, but 
you cannot do so by putting the JS in the /static folder (unless using 
routing to rewrite the URL to point to a web2py controller). Static files 
are served directly without going through the full framework request cycle, 
so there is no opportunity to execute a template.

One approach is to serve the file via a controller (and cache it to avoid 
unnecessarily re-creating the file on every request):

@cache.action(time_expire=60*60*24, cache_model=cache.ram)
def serve_js():
    response.view = 'js/%s' % request.args(0)
    return dict() 

Then put your JS template in /views/js/, and access it via URL('default', 
'serve_js', args='my_js_file.js').

Another option is to define the Javascript variables to be translated 
outside of the JS file (before the file is loaded). For example, in 
layout.html, you could do something like:

<script>
  var days_nonSelectedText = "{{=T('Select days')}}";
  var days_selectAllText = "{{=T('Select all')}}";
</script>
<script src="{{=URL('static', 'js/my_js_file.js')}}"></script>

And then in your JS file:

$('.multiselectDays').multiselect({
  numberDisplayed: 5,
  nonSelectedText: days_nonSelectText,
  includeSelectAllOption: true,
  selectAllText: days_selectAllText
});

Another option would be to handle the translation via Javascript instead of 
the web2py translator.

Anthony

On Tuesday, December 2, 2014 7:00:41 AM UTC-5, Yebach wrote:
>
> Is it possible to include {{=T()}} in my static js file??
>
> Smth like 
> $('.multiselectDays').multiselect({
> numberDisplayed: 5,
> nonSelectedText: 'Izberite dneve', // {{=T('Select days')}}
> includeSelectAllOption: true,
> selectAllText: 'Izberi vse' //{{=T('Select all')}}
>     });
>
> I need it for translation of my bootstrap elements
>
>
>
> On Wednesday, March 12, 2014 2:55:31 PM UTC+1, 黄祥 wrote:
>>
>> thank you so much for your detail explaination, massimo. i've figure it 
>> out why and how it works now.
>> e.g.
>> shortcut.add("Ctrl+F12", function() {
>> window.open("/test/default/index", "_self");
>> });
>>
>> thanks and best regards,
>> stifan
>>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to