how can I get the I18n.default_locale selected in my Rails app in a JS script ?

26 views
Skip to first unread message

Erwin

unread,
Feb 23, 2010, 1:33:28 PM2/23/10
to rails-i18n
I am not very fluent in JS, so I don't see how to transfer the rails
app current locale ..

I have a JS script able to detect the default browser language ..
running fine

I can also modify the JS language, passing a global var ..

// Create a JSON Object
var myJSON = {
"lang": "fr"
};
// Pass it to the script immediately...
datePickerController.setGlobalVars(myJSON);

and it runs ... but I would like this script to use the language
selected by the rails app user :
the I18n.default_locale being modified in the rails app

any clue ?

thanks a lot ..

erwin

Henrik Nyh

unread,
Feb 23, 2010, 2:45:53 PM2/23/10
to rails...@googlegroups.com
Not sure I understand you correctly, but you could just do something like

<%=javascript_tag "window.locale = #{I18n.locale.to_s.inspect};" %>

in your page, and then do e.g.

alert(locale);

from a script.

> --
> You received this message because you are subscribed to the Google Groups "rails-i18n" group.
> To post to this group, send email to rails...@googlegroups.com.
> To unsubscribe from this group, send email to rails-i18n+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-i18n?hl=en.
>
>

Iain Hecker

unread,
Feb 23, 2010, 2:58:43 PM2/23/10
to rails...@googlegroups.com
Hi,

I sometimes have make a controller (or abuse something like a
pages_controller) to render a js view. In this view I place all the
initializers to other scripts. I keep all serious javascript into
static files which I can minimize and cache the hell out off.

Example:

# app/views/javascripts/show.js.erb
$(function() {
// Add datepickers to every date field
$('.date input').datepicker({
changeMonth : true,
changeYear : true,
dateFormat : '<%= t('.date_format') %>',
maxDate : '0'
});
});

# config/routes.rb
map.resource :javascript

# app/controllers/javascripts_controller.rb
class JavascriptsController < ApplicationController
def show; end # just render the view
end

# app/views/layouts/application.html.haml
= javascript_include_tag "jquery", "jquery.date-picker", :cache => true
= javascript_include_tag javascript_path(:format => :js)

This might be a bit more than you asked for, but this is how I pass
translations and such to javascript.

Iain


On Tue, Feb 23, 2010 at 19:33, Erwin <yves_...@mac.com> wrote:

Erwin

unread,
Feb 23, 2010, 5:55:55 PM2/23/10
to rails-i18n
very smart , thanks !

Justin MacCarthy

unread,
Feb 24, 2010, 3:08:21 AM2/24/10
to rails...@googlegroups.com

grimen

unread,
Feb 24, 2010, 8:26:55 AM2/24/10
to rails-i18n
ALmost identical to "Babilu" plugin, but this one is a bit more
customizable (say, for security reasons) and fully test-suit for the
paranoia:

http://github.com/fnando/i18n-js

grimen

On Feb 24, 9:08 am, Justin MacCarthy <jus...@maccarthy.org> wrote:
> Check out this rails plugin,
>

> http://tore.darell.no/posts/introducing_babilu_rails_i18n_for_your_ja...
>
> Justin

Justin MacCarthy

unread,
Feb 24, 2010, 8:55:12 AM2/24/10
to rails...@googlegroups.com
Adding that to my watchlist ! Thanks

Justin

Reply all
Reply to author
Forward
0 new messages