# How can I get the I18n.default\_locale selected in my Rails app in a JS script?

**URL:** <https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002>\
**Category:** Rails I18n\
**Created:** [February 23, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002 "2010-02-23T19:35:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 23, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/1 "2010-02-23T19:35:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 23, 2010, 8:47pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/2 "2010-02-23T20:47:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 23, 2010, 9:00pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/3 "2010-02-23T21:00:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 23, 2010, 11:57pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/4 "2010-02-23T23:57:01Z")

</div>

very smart , thanks !

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 24, 2010, 9:09am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/5 "2010-02-24T09:09:31Z")

</div>

Check out this rails plugin,

[http://tore.darell.no/posts/introducing\_babilu\_rails\_i18n\_for\_your\_javascript](http://tore.darell.no/posts/introducing_babilu_rails_i18n_for_your_javascript)

Justin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 24, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/6 "2010-02-24T14:28:06Z")

</div>

ALmost identical to “Babilu” plugin, but this one is a bit more  
customizable (say, for security reasons) and fully test-suit for the  
paranoia:

> **[GitHub - fnando/i18n-js: It's a small library to provide the I18n...](https://github.com/fnando/i18n-js)**
>
> It's a small library to provide the I18n translations on the Javascript. It comes with Rails support. - GitHub - fnando/i18n-js: It's a small library to provide the I18n translations on the...

grimen

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 24, 2010, 2:56pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-i18n-default-locale-selected-in-my-rails-app-in-a-js-script/184002/7 "2010-02-24T14:56:12Z")

</div>

Adding that to my watchlist ! Thanks

Justin
