Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate JSON in Rails 3

I'm currently doing this in a controller:

def index
  @orders = Order.all
  respond_to do |format| {
    format.html
    format.js { render json: @orders.as_json()
  }
end

I have a order state field. The values of this field are in English and need to be translated to Dutch. Problem is, the as_json call doesn't automatically translate the data to Dutch. The YML file and current and default locale all are working correctly. ( calling I18n.t works, but how does this work with as_json() ? )

Thanks!

like image 562
Henk de Vries Avatar asked Jul 27 '11 14:07

Henk de Vries


1 Answers

You can override as_json in your model and call i18n from that method.

like image 192
Sohan Avatar answered Oct 13 '22 00:10

Sohan