On 19 September 2011 08:11, Ben Taylor <[email protected]> wrote:
> I'm about to read through http://guides.rubyonrails.org/i18n.html which I
> think will help, but my bigger problem is that I have strings hard coded
> into the JS. Any ideas/thoughts/pro tips?
One approach is to pass in i18n'ed strings into your js functions, like so;
(e.g. in haml)
:javascript
doSomethingAwesome("#{i18.t :awesome}");
The quotes there are part of the JS, haml allows you to interpolate
integers too by just not putting the quotes there:
:javascript:
multiplySomethingAwesome(#{awesomeness_factor})
What I generally do is something like this, to make it all a bit cleaner:
:javascript
$(document).ready(function() {
var options = {
localised_message: "#{I18n.t :awesome}"
};
Awesome.init(options);
Awesome.doSomethingAwesome();
});
... and then store the options in the init function and access them
later in from other functions.
One of my coworkers has set up this, but I haven't had any experience
with it myself. It apparently allows you to use your i18n labels from
JS, by exporting them to a js file and giving you a Rails-like API to
use them.
https://github.com/fnando/i18n-js
Robert
--
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rails-oceania?hl=en.