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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2010-02-23 19:35
(Received via mailing list)
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...

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 ..

4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2010-02-23 20:47
(Received via mailing list)
Not sure I understand you correctly, but you could just do something

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

in your page, and then do e.g.


from a script.
Iain Hecker (Guest)
on 2010-02-23 21:00
(Received via mailing list)

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.


# 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

# app/views/layouts/application.html.haml
= javascript_include_tag "jquery", "", :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.

Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2010-02-23 23:57
(Received via mailing list)
very smart , thanks !
Justin MacCarthy (Guest)
on 2010-02-24 09:09
(Received via mailing list)
Check out this rails plugin,

grimen (Guest)
on 2010-02-24 14:28
(Received via mailing list)
ALmost identical to "Babilu" plugin, but this one is a bit more
customizable (say, for security reasons) and fully test-suit for the

Justin MacCarthy (Guest)
on 2010-02-24 14:56
(Received via mailing list)
Adding that to my watchlist ! Thanks

This topic is locked and can not be replied to.